use of org.onosproject.net.meter.MeterRequest in project onos by opennetworkinglab.
the class MeterAddCommand method doExecute.
@Override
protected void doExecute() {
MeterService service = get(MeterService.class);
CoreService coreService = get(CoreService.class);
DeviceId deviceId = DeviceId.deviceId(uri);
checkOptions();
MeterRequest.Builder builder = DefaultMeterRequest.builder().forDevice(deviceId).fromApp(coreService.registerApplication(appId)).withUnit(unit).withBands(bands);
if (isBurst) {
builder = builder.burst();
}
// the user defined mode being active.
if (scope != null) {
builder = builder.withScope(scope);
}
if (index != null) {
builder = builder.withIndex(index);
}
MeterRequest request = builder.add();
Meter m = service.submit(request);
log.info("Requested meter with cellId {}: {}", m.meterCellId().toString(), m.toString());
print("Requested meter with cellId %s: %s", m.meterCellId().toString(), m.toString());
}
use of org.onosproject.net.meter.MeterRequest in project onos by opennetworkinglab.
the class MeterRequestCodec method decode.
@Override
public MeterRequest decode(ObjectNode json, CodecContext context) {
if (json == null || !json.isObject()) {
return null;
}
final JsonCodec<Band> meterBandCodec = context.codec(Band.class);
// parse device id
DeviceId deviceId = DeviceId.deviceId(nullIsIllegal(json.get(DEVICE_ID), DEVICE_ID + MISSING_MEMBER_MESSAGE).asText());
// application id
if (applicationId == null) {
CoreService coreService = context.getService(CoreService.class);
applicationId = coreService.registerApplication(REST_APP_ID);
}
// parse burst
boolean burst = false;
JsonNode burstJson = json.get("burst");
if (burstJson != null) {
burst = burstJson.asBoolean();
}
// parse unit type
String unit = nullIsIllegal(json.get(UNIT), UNIT + MISSING_MEMBER_MESSAGE).asText();
Meter.Unit meterUnit = null;
switch(unit) {
case "KB_PER_SEC":
meterUnit = Meter.Unit.KB_PER_SEC;
break;
case "PKTS_PER_SEC":
meterUnit = Meter.Unit.PKTS_PER_SEC;
break;
case "BYTES_PER_SEC":
meterUnit = Meter.Unit.BYTES_PER_SEC;
break;
default:
nullIsIllegal(meterUnit, "The requested unit " + unit + " is not defined for meter.");
}
// parse meter bands
List<Band> bandList = new ArrayList<>();
JsonNode bandsJson = json.get(BANDS);
checkNotNull(bandsJson);
if (bandsJson != null) {
IntStream.range(0, bandsJson.size()).forEach(i -> {
ObjectNode bandJson = get(bandsJson, i);
bandList.add(meterBandCodec.decode(bandJson, context));
});
}
// parse scope and index
JsonNode scopeJson = json.get(SCOPE);
MeterScope scope = null;
if (scopeJson != null && !isNullOrEmpty(scopeJson.asText())) {
scope = MeterScope.of(scopeJson.asText());
}
JsonNode indexJson = json.get(INDEX);
Long index = null;
if (indexJson != null && !isNullOrEmpty(indexJson.asText()) && scope != null) {
index = indexJson.asLong();
}
// build the final request
MeterRequest.Builder meterRequest = DefaultMeterRequest.builder();
if (scope != null) {
meterRequest.withScope(scope);
}
if (index != null) {
meterRequest.withIndex(index);
}
meterRequest.fromApp(applicationId).forDevice(deviceId).withUnit(meterUnit).withBands(bandList);
if (burst) {
meterRequest.burst();
}
return meterRequest.add();
}
use of org.onosproject.net.meter.MeterRequest in project onos by opennetworkinglab.
the class MeterRequestCodecTest method getMeterRequest.
/**
* Reads in a meter from the given resource and decodes it.
*
* @param resourceName resource to use to read the JSON for the rule
* @return decoded meterRequest
* @throws IOException if processing the resource fails
*/
private MeterRequest getMeterRequest(String resourceName) throws IOException {
InputStream jsonStream = MeterRequestCodecTest.class.getResourceAsStream(resourceName);
JsonNode json = context.mapper().readTree(jsonStream);
assertThat(json, notNullValue());
MeterRequest meterRequest = meterRequestCodec.decode((ObjectNode) json, context);
assertThat(meterRequest, notNullValue());
return meterRequest;
}
use of org.onosproject.net.meter.MeterRequest in project onos by opennetworkinglab.
the class MeterRequestCodecTest method testMeterRequestDecode.
/**
* Test decoding of a MeterRequest object.
*/
@Test
public void testMeterRequestDecode() throws IOException {
MeterRequest meterRequest = getMeterRequest("simple-meter-request.json");
checkCommonData(meterRequest);
assertThat(meterRequest.bands().size(), is(1));
Band band = meterRequest.bands().iterator().next();
assertThat(band.type().toString(), is("REMARK"));
assertThat(band.rate(), is(10L));
assertThat(band.dropPrecedence(), is((short) 20));
assertThat(band.burst(), is(30L));
}
use of org.onosproject.net.meter.MeterRequest in project onos by opennetworkinglab.
the class MeterRequestProtoTranslator method translate.
/**
* Translates gRPC MeterRequest to {@link MeterRequest}.
*
* @param meterRequest gRPC message
* @return {@link MeterRequest}
*/
public static MeterRequest translate(MeterRequestProtoOuterClass.MeterRequestProto meterRequest) {
DeviceId deviceid = DeviceId.deviceId(meterRequest.getDeviceId());
ApplicationId appId = ApplicationIdProtoTranslator.translate(meterRequest.getApplicationId());
Meter.Unit unit = MeterEnumsProtoTranslator.translate(meterRequest.getUnit()).get();
boolean burst = meterRequest.getIsBurst();
Collection<Band> bands = BandProtoTranslator.translate(meterRequest.getBandsList());
MeterRequest.Type type = (MeterRequest.Type) translate(meterRequest.getType()).get();
if (type == MeterRequest.Type.ADD) {
return DefaultMeterRequest.builder().forDevice(deviceid).fromApp(appId).withUnit(unit).withBands(bands).add();
} else {
return DefaultMeterRequest.builder().forDevice(deviceid).fromApp(appId).withUnit(unit).withBands(bands).remove();
}
}
Aggregations