Search in sources :

Example 1 with MeterScope

use of org.onosproject.net.meter.MeterScope 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 2 with MeterScope

use of org.onosproject.net.meter.MeterScope in project onos by opennetworkinglab.

the class DistributedMeterStore method purgeMeter.

@Override
public void purgeMeter(Meter m) {
    // Once we receive the ack from the sb, create the key
    // remove definitely the meter and free the id
    MeterKey key = MeterKey.key(m.deviceId(), m.meterCellId());
    try {
        if (Versioned.valueOrNull(meters.remove(key)) != null) {
            MeterScope scope;
            if (m.meterCellId().type() == PIPELINE_INDEPENDENT) {
                PiMeterCellId piMeterCellId = (PiMeterCellId) m.meterCellId();
                scope = MeterScope.of(piMeterCellId.meterId().id());
            } else {
                scope = MeterScope.globalScope();
            }
            MeterTableKey meterTableKey = MeterTableKey.key(m.deviceId(), scope);
            freeMeterId(meterTableKey, m.meterCellId());
        }
    } catch (StorageException e) {
        log.error("{} thrown a storage exception: {}", e.getStackTrace()[0].getMethodName(), e.getMessage(), e);
    }
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) MeterTableKey(org.onosproject.net.meter.MeterTableKey) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) MeterScope(org.onosproject.net.meter.MeterScope) StorageException(org.onosproject.store.service.StorageException)

Example 3 with MeterScope

use of org.onosproject.net.meter.MeterScope in project onos by opennetworkinglab.

the class MetersWebResource method getMetersByDeviceIdAndScope.

/**
 * Returns a collection of meters by the device id and meter scope.
 *
 * @param deviceId device identifier
 * @param scope scope identifier
 * @return 200 OK with array of meters which belongs to specified device
 * @onos.rsModel Meters
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("scope/{deviceId}/{scope}")
public Response getMetersByDeviceIdAndScope(@PathParam("deviceId") String deviceId, @PathParam("scope") String scope) {
    DeviceId did = DeviceId.deviceId(deviceId);
    MeterScope meterScope = MeterScope.of(scope);
    MeterService meterService = get(MeterService.class);
    final Iterable<Meter> meters = meterService.getMeters(did, meterScope);
    if (meters != null) {
        meters.forEach(meter -> metersNode.add(codec(Meter.class).encode(meter, this)));
    }
    return ok(root).build();
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) MeterScope(org.onosproject.net.meter.MeterScope) MeterService(org.onosproject.net.meter.MeterService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with MeterScope

use of org.onosproject.net.meter.MeterScope in project onos by opennetworkinglab.

the class MetersWebResource method deleteMeterByDeviceIdAndMeterCellId.

/**
 * Removes the meter by the device id and meter cell id.
 *
 * @param deviceId device identifier
 * @param scope scope identifier
 * @param index index
 * @return 204 NO CONTENT
 */
@DELETE
@Path("{deviceId}/{scope}/{index}")
public Response deleteMeterByDeviceIdAndMeterCellId(@PathParam("deviceId") String deviceId, @PathParam("scope") String scope, @PathParam("index") String index) {
    DeviceId did = DeviceId.deviceId(deviceId);
    MeterScope meterScope = MeterScope.of(scope);
    long meterIndex = Long.parseLong(index);
    MeterCellId meterCellId;
    if (meterScope.equals(MeterScope.globalScope())) {
        meterCellId = MeterId.meterId(meterIndex);
    } else {
        meterCellId = PiMeterCellId.ofIndirect(PiMeterId.of(meterScope.id()), meterIndex);
    }
    MeterRequest meterRequest = deleteRequest(did);
    MeterService meterService = get(MeterService.class);
    meterService.withdraw(meterRequest, meterCellId);
    return Response.noContent().build();
}
Also used : DeviceId(org.onosproject.net.DeviceId) MeterScope(org.onosproject.net.meter.MeterScope) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest) MeterService(org.onosproject.net.meter.MeterService) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) MeterCellId(org.onosproject.net.meter.MeterCellId) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE)

Example 5 with MeterScope

use of org.onosproject.net.meter.MeterScope in project onos by opennetworkinglab.

the class MetersWebResource method getMeterByDeviceIdAndMeterCellId.

/**
 * Returns a meter by the meter cell id.
 *
 * @param deviceId device identifier
 * @param scope scope identifier
 * @param index index
 * @return 200 OK with a meter, return 404 if no entry has been found
 * @onos.rsModel Meter
 */
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{deviceId}/{scope}/{index}")
public Response getMeterByDeviceIdAndMeterCellId(@PathParam("deviceId") String deviceId, @PathParam("scope") String scope, @PathParam("index") String index) {
    DeviceId did = DeviceId.deviceId(deviceId);
    MeterScope meterScope = MeterScope.of(scope);
    long meterIndex = Long.parseLong(index);
    MeterCellId meterCellId;
    if (meterScope.equals(MeterScope.globalScope())) {
        meterCellId = MeterId.meterId(meterIndex);
    } else {
        meterCellId = PiMeterCellId.ofIndirect(PiMeterId.of(meterScope.id()), meterIndex);
    }
    MeterService meterService = get(MeterService.class);
    final Meter meter = nullIsNotFound(meterService.getMeter(did, meterCellId), METER_NOT_FOUND + meterCellId);
    metersNode.add(codec(Meter.class).encode(meter, this));
    return ok(root).build();
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) MeterScope(org.onosproject.net.meter.MeterScope) MeterService(org.onosproject.net.meter.MeterService) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) MeterCellId(org.onosproject.net.meter.MeterCellId) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

MeterScope (org.onosproject.net.meter.MeterScope)7 DeviceId (org.onosproject.net.DeviceId)5 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)5 DefaultMeterRequest (org.onosproject.net.meter.DefaultMeterRequest)4 MeterRequest (org.onosproject.net.meter.MeterRequest)4 MeterService (org.onosproject.net.meter.MeterService)4 Path (javax.ws.rs.Path)3 Meter (org.onosproject.net.meter.Meter)3 MeterCellId (org.onosproject.net.meter.MeterCellId)3 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 CoreService (org.onosproject.core.CoreService)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 ArrayList (java.util.ArrayList)1 DELETE (javax.ws.rs.DELETE)1 Band (org.onosproject.net.meter.Band)1 MeterKey (org.onosproject.net.meter.MeterKey)1 MeterTableKey (org.onosproject.net.meter.MeterTableKey)1 StorageException (org.onosproject.store.service.StorageException)1