use of org.onosproject.net.meter.DefaultMeter 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;
}
use of org.onosproject.net.meter.DefaultMeter in project onos by opennetworkinglab.
the class P4RuntimeMeterProgrammable method forgeMeter.
private Meter forgeMeter(PiMeterCellConfig config, PiMeterCellHandle handle) {
final Optional<PiTranslatedEntity<Meter, PiMeterCellConfig>> translatedEntity = translator.lookup(handle);
final TimedEntry<PiMeterCellConfig> timedEntry = meterMirror.get(handle);
// is default configuration.
if (translatedEntity.isEmpty()) {
if (!config.isDefaultConfig()) {
log.warn("Meter Cell Config obtained from device {} is different from " + "one in in translation store: device={}, store=Default", deviceId, config);
} else {
log.debug("Configs for {} obtained from device: {} and from the store are default, " + "skipping the forge section", config.cellId(), deviceId);
}
return null;
}
// that config from devices can be default which means no band
if (!isSimilar(translatedEntity.get().translated(), config)) {
log.warn("Meter Cell Config obtained from device {} is different from " + "one in in translation store: device={}, store={}", deviceId, config, translatedEntity.get().translated());
return null;
}
// the eventual consistent maps used in the AbstractDistributedP4RuntimeMirror
if (timedEntry == null) {
log.warn("Meter entry handle not found in device mirror: {}", handle);
return null;
}
Meter original = translatedEntity.get().original();
// Forge a meter with MeterCellId, Bands and DeviceId using the original value.
DefaultMeter meter = (DefaultMeter) DefaultMeter.builder().withBands(original.bands()).withCellId(original.meterCellId()).forDevice(deviceId).build();
meter.setState(MeterState.ADDED);
return meter;
}
use of org.onosproject.net.meter.DefaultMeter in project onos by opennetworkinglab.
the class P4RuntimeMeterProgrammable method getMeters.
@Override
public CompletableFuture<Collection<Meter>> getMeters() {
if (!setupBehaviour("getMeters()")) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
Collection<PiMeterCellConfig> piMeterCellConfigs;
Set<PiMeterId> meterIds = new HashSet<>();
for (PiMeterModel mode : pipelineModel.meters()) {
meterIds.add(mode.id());
}
piMeterCellConfigs = client.read(p4DeviceId, pipeconf).meterCells(meterIds).submitSync().all(PiMeterCellConfig.class).stream().filter(piMeterCellConfig -> !piMeterCellConfig.isDefaultConfig()).collect(Collectors.toList());
meterMirror.sync(deviceId, piMeterCellConfigs);
if (piMeterCellConfigs.isEmpty()) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
List<PiMeterCellId> inconsistentOrDefaultCells = Lists.newArrayList();
List<Meter> meters = Lists.newArrayList();
// Check the consistency of meter config
for (PiMeterCellConfig config : piMeterCellConfigs) {
PiMeterCellHandle handle = PiMeterCellHandle.of(deviceId, config);
DefaultMeter meter = (DefaultMeter) forgeMeter(config, handle);
if (meter == null) {
// A default config cannot be used to forge meter
// because meter has at least 1 band while default config has no band
inconsistentOrDefaultCells.add(config.cellId());
} else {
meters.add(meter);
}
}
// Reset all inconsistent meter cells to the default config
if (!inconsistentOrDefaultCells.isEmpty()) {
WriteRequest request = client.write(p4DeviceId, pipeconf);
for (PiMeterCellId cellId : inconsistentOrDefaultCells) {
PiMeterCellHandle handle = PiMeterCellHandle.of(deviceId, cellId);
appendEntryToWriteRequestOrSkip(request, handle, PiMeterCellConfig.reset(cellId));
}
request.submit().whenComplete((response, ex) -> {
if (ex != null) {
log.error("Exception resetting inconsistent meter entries", ex);
} else {
log.debug("Successfully removed {} out of {} inconsistent meter entries", response.success().size(), response.all().size());
}
response.success().forEach(entity -> meterMirror.remove((PiMeterCellHandle) entity.handle()));
});
}
return CompletableFuture.completedFuture(meters);
}
use of org.onosproject.net.meter.DefaultMeter in project onos by opennetworkinglab.
the class DistributedMeterStore method updateMeterState.
@Override
public Meter updateMeterState(Meter meter) {
// Update meter if present (stats workflow)
MeterKey key = MeterKey.key(meter.deviceId(), meter.meterCellId());
Versioned<MeterData> value = meters.computeIfPresent(key, (k, v) -> {
DefaultMeter m = (DefaultMeter) v.meter();
MeterState meterState = m.state();
if (meterState == MeterState.PENDING_ADD) {
m.setState(meter.state());
}
m.setProcessedPackets(meter.packetsSeen());
m.setProcessedBytes(meter.bytesSeen());
m.setLife(meter.life());
// TODO: Prune if drops to zero.
m.setReferenceCount(meter.referenceCount());
return new MeterData(m, null);
});
return value != null ? value.value().meter() : null;
}
use of org.onosproject.net.meter.DefaultMeter in project onos by opennetworkinglab.
the class DistributedMeterStore method deleteMeter.
@Override
public CompletableFuture<MeterStoreResult> deleteMeter(Meter meter) {
CompletableFuture<MeterStoreResult> future = new CompletableFuture<>();
MeterKey key = MeterKey.key(meter.deviceId(), meter.meterCellId());
futures.put(key, future);
// that it has been removed from the dataplane.
try {
Versioned<MeterData> versionedData = meters.computeIfPresent(key, (k, v) -> {
DefaultMeter m = (DefaultMeter) v.meter();
MeterState meterState = m.state();
if (meterState == MeterState.PENDING_REMOVE) {
return v;
}
m.setState(meter.state());
return new MeterData(m, v.reason().isPresent() ? v.reason().get() : null);
});
// If it does not exist in the system, completes immediately
if (versionedData == null) {
futures.remove(key);
future.complete(MeterStoreResult.success());
}
} catch (StorageException e) {
log.error("{} thrown a storage exception: {}", e.getStackTrace()[0].getMethodName(), e.getMessage(), e);
futures.remove(key);
future.completeExceptionally(e);
}
return future;
}
Aggregations