use of org.onosproject.net.meter.Meter in project onos by opennetworkinglab.
the class SimpleVirtualMeterStore method deleteMeter.
@Override
public CompletableFuture<MeterStoreResult> deleteMeter(NetworkId networkId, Meter meter) {
ConcurrentMap<MeterKey, MeterData> meters = getMetersByNetwork(networkId);
ConcurrentMap<MeterKey, CompletableFuture<MeterStoreResult>> futures = getFuturesByNetwork(networkId);
CompletableFuture<MeterStoreResult> future = new CompletableFuture<>();
MeterKey key = MeterKey.key(meter.deviceId(), meter.id());
futures.put(key, future);
MeterData data = new MeterData(meter, null, local);
// that it has been removed from the dataplane.
try {
if (meters.computeIfPresent(key, (k, v) -> data) == null) {
future.complete(MeterStoreResult.success());
}
} catch (StorageException e) {
future.completeExceptionally(e);
}
return future;
}
use of org.onosproject.net.meter.Meter 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.Meter in project onos by opennetworkinglab.
the class MetersListCommand method doExecute.
@Override
protected void doExecute() {
if (!isNullOrEmpty(scopeString)) {
meterScope = MeterScope.of(scopeString);
}
if (!isNullOrEmpty(indexString)) {
index = Long.parseLong(indexString);
if (meterScope == null) {
meterScope = MeterScope.globalScope();
}
}
if (index != null) {
if (meterScope != null && meterScope.equals(MeterScope.globalScope())) {
meterCellId = MeterId.meterId(index);
} else if (meterScope != null) {
meterCellId = PiMeterCellId.ofIndirect(PiMeterId.of(meterScope.id()), index);
}
}
MeterService service = get(MeterService.class);
Collection<Meter> meters;
if (isNullOrEmpty(uri)) {
meters = service.getAllMeters();
printMeters(meters);
} else {
if (meterCellId != null) {
Meter m = service.getMeter(DeviceId.deviceId(uri), meterCellId);
if (m != null) {
print("%s", m);
} else {
error("Meter %s not found for device %s", meterCellId, uri);
}
} else if (meterScope != null) {
meters = service.getMeters(DeviceId.deviceId(uri), meterScope);
printMeters(meters);
} else {
meters = service.getMeters(DeviceId.deviceId(uri));
printMeters(meters);
}
}
}
use of org.onosproject.net.meter.Meter 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.Meter in project onos by opennetworkinglab.
the class MeterManager method submit.
@Override
public Meter submit(MeterRequest request) {
checkNotNull(request, "request cannot be null.");
MeterCellId cellId;
if (request.index().isPresent()) {
checkArgument(userDefinedIndex, "Index cannot be provided when userDefinedIndex mode is disabled");
// User provides index
if (request.scope().isGlobal()) {
cellId = MeterId.meterId(request.index().get());
} else {
cellId = PiMeterCellId.ofIndirect(PiMeterId.of(request.scope().id()), request.index().get());
}
} else {
checkArgument(!userDefinedIndex, "Index cannot be allocated when userDefinedIndex mode is enabled");
// Allocate an id
cellId = allocateMeterId(request.deviceId(), request.scope());
}
Meter.Builder mBuilder = DefaultMeter.builder().forDevice(request.deviceId()).fromApp(request.appId()).withBands(request.bands()).withCellId(cellId).withUnit(request.unit());
if (request.isBurst()) {
mBuilder.burst();
}
if (request.annotations() != null && !request.annotations().keys().isEmpty()) {
mBuilder.withAnnotations(request.annotations());
}
DefaultMeter m = (DefaultMeter) mBuilder.build();
// Meter installation logic (happy ending case)
// PENDING -> stats -> ADDED -> future completes
m.setState(MeterState.PENDING_ADD);
store.addOrUpdateMeter(m).whenComplete((result, error) -> onComplete.accept(request, result, error));
return m;
}
Aggregations