Search in sources :

Example 6 with DefaultMeter

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;
}
Also used : DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter) DefaultMeter(org.onosproject.net.meter.DefaultMeter) MeterCellId(org.onosproject.net.meter.MeterCellId) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId)

Example 7 with DefaultMeter

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;
}
Also used : PiTranslatedEntity(org.onosproject.net.pi.service.PiTranslatedEntity) DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter) PiMeterCellConfig(org.onosproject.net.pi.runtime.PiMeterCellConfig) DefaultMeter(org.onosproject.net.meter.DefaultMeter)

Example 8 with DefaultMeter

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);
}
Also used : PiMeterId(org.onosproject.net.pi.model.PiMeterId) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter) PiMeterCellConfig(org.onosproject.net.pi.runtime.PiMeterCellConfig) PiMeterModel(org.onosproject.net.pi.model.PiMeterModel) WriteRequest(org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest) DefaultMeter(org.onosproject.net.meter.DefaultMeter) PiMeterCellHandle(org.onosproject.net.pi.runtime.PiMeterCellHandle) HashSet(java.util.HashSet)

Example 9 with DefaultMeter

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;
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) DefaultMeter(org.onosproject.net.meter.DefaultMeter) MeterState(org.onosproject.net.meter.MeterState)

Example 10 with DefaultMeter

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;
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) MeterStoreResult(org.onosproject.net.meter.MeterStoreResult) CompletableFuture(java.util.concurrent.CompletableFuture) DefaultMeter(org.onosproject.net.meter.DefaultMeter) MeterState(org.onosproject.net.meter.MeterState) StorageException(org.onosproject.store.service.StorageException)

Aggregations

DefaultMeter (org.onosproject.net.meter.DefaultMeter)17 Meter (org.onosproject.net.meter.Meter)13 MeterKey (org.onosproject.net.meter.MeterKey)9 Test (org.junit.Test)8 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)6 MeterCellId (org.onosproject.net.meter.MeterCellId)5 ExecutionException (java.util.concurrent.ExecutionException)2 MeterState (org.onosproject.net.meter.MeterState)2 PiMeterCellConfig (org.onosproject.net.pi.runtime.PiMeterCellConfig)2 HashSet (java.util.HashSet)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 MeterId (org.onosproject.net.meter.MeterId)1 MeterStoreResult (org.onosproject.net.meter.MeterStoreResult)1 MeterTableKey (org.onosproject.net.meter.MeterTableKey)1 PiMeterId (org.onosproject.net.pi.model.PiMeterId)1 PiMeterModel (org.onosproject.net.pi.model.PiMeterModel)1 PiMeterCellHandle (org.onosproject.net.pi.runtime.PiMeterCellHandle)1 PiTranslatedEntity (org.onosproject.net.pi.service.PiTranslatedEntity)1 WriteRequest (org.onosproject.p4runtime.api.P4RuntimeWriteClient.WriteRequest)1 StorageException (org.onosproject.store.service.StorageException)1