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();
}
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);
}
}
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();
}
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();
}
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();
}
Aggregations