Search in sources :

Example 1 with MeterRequest

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());
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) CoreService(org.onosproject.core.CoreService) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest) MeterService(org.onosproject.net.meter.MeterService)

Example 2 with MeterRequest

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();
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) ArrayList(java.util.ArrayList) CoreService(org.onosproject.core.CoreService) Band(org.onosproject.net.meter.Band) JsonNode(com.fasterxml.jackson.databind.JsonNode) MeterScope(org.onosproject.net.meter.MeterScope) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest)

Example 3 with MeterRequest

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;
}
Also used : InputStream(java.io.InputStream) JsonNode(com.fasterxml.jackson.databind.JsonNode) MeterRequest(org.onosproject.net.meter.MeterRequest)

Example 4 with 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));
}
Also used : Band(org.onosproject.net.meter.Band) MeterRequest(org.onosproject.net.meter.MeterRequest) Test(org.junit.Test)

Example 5 with MeterRequest

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();
    }
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) Band(org.onosproject.net.meter.Band) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest) ApplicationId(org.onosproject.core.ApplicationId)

Aggregations

MeterRequest (org.onosproject.net.meter.MeterRequest)11 DefaultMeterRequest (org.onosproject.net.meter.DefaultMeterRequest)9 DeviceId (org.onosproject.net.DeviceId)6 Meter (org.onosproject.net.meter.Meter)5 MeterService (org.onosproject.net.meter.MeterService)5 MeterScope (org.onosproject.net.meter.MeterScope)4 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 Path (javax.ws.rs.Path)3 CoreService (org.onosproject.core.CoreService)3 Band (org.onosproject.net.meter.Band)3 MeterCellId (org.onosproject.net.meter.MeterCellId)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 DELETE (javax.ws.rs.DELETE)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1