Search in sources :

Example 1 with Meter

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;
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) StorageException(org.onosproject.store.service.StorageException) DefaultMeter(org.onosproject.net.meter.DefaultMeter) MeterFeatures(org.onosproject.net.meter.MeterFeatures) CompletableFuture(java.util.concurrent.CompletableFuture) Collections2(com.google.common.collect.Collections2) MeterStoreDelegate(org.onosproject.net.meter.MeterStoreDelegate) ConcurrentMap(java.util.concurrent.ConcurrentMap) Component(org.osgi.service.component.annotations.Component) MeterStoreResult(org.onosproject.net.meter.MeterStoreResult) NetworkId(org.onosproject.incubator.net.virtual.NetworkId) Activate(org.osgi.service.component.annotations.Activate) MeterFailReason(org.onosproject.net.meter.MeterFailReason) NodeId(org.onosproject.cluster.NodeId) Meter(org.onosproject.net.meter.Meter) MeterEvent(org.onosproject.net.meter.MeterEvent) Logger(org.slf4j.Logger) MeterFeaturesKey(org.onosproject.net.meter.MeterFeaturesKey) MeterOperation(org.onosproject.net.meter.MeterOperation) Deactivate(org.osgi.service.component.annotations.Deactivate) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Maps(com.google.common.collect.Maps) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) VirtualNetworkMeterStore(org.onosproject.incubator.net.virtual.VirtualNetworkMeterStore) TIMEOUT(org.onosproject.net.meter.MeterFailReason.TIMEOUT) LoggerFactory.getLogger(org.slf4j.LoggerFactory.getLogger) MeterKey(org.onosproject.net.meter.MeterKey) ClusterService(org.onosproject.cluster.ClusterService) DeviceId(org.onosproject.net.DeviceId) Reference(org.osgi.service.component.annotations.Reference) MeterStoreResult(org.onosproject.net.meter.MeterStoreResult) CompletableFuture(java.util.concurrent.CompletableFuture) StorageException(org.onosproject.store.service.StorageException)

Example 2 with Meter

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());
}
Also used : Meter(org.onosproject.net.meter.Meter) DeviceId(org.onosproject.net.DeviceId) CoreService(org.onosproject.core.CoreService) MeterRequest(org.onosproject.net.meter.MeterRequest) DefaultMeterRequest(org.onosproject.net.meter.DefaultMeterRequest) MeterService(org.onosproject.net.meter.MeterService)

Example 3 with Meter

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

Example 4 with Meter

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();
}
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 5 with Meter

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

Aggregations

Meter (org.onosproject.net.meter.Meter)43 DefaultMeter (org.onosproject.net.meter.DefaultMeter)30 Test (org.junit.Test)21 DeviceId (org.onosproject.net.DeviceId)12 MeterService (org.onosproject.net.meter.MeterService)8 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)8 MeterCellId (org.onosproject.net.meter.MeterCellId)7 MeterKey (org.onosproject.net.meter.MeterKey)7 Produces (javax.ws.rs.Produces)6 GET (javax.ws.rs.GET)5 Path (javax.ws.rs.Path)5 Band (org.onosproject.net.meter.Band)5 DefaultBand (org.onosproject.net.meter.DefaultBand)4 DefaultMeterRequest (org.onosproject.net.meter.DefaultMeterRequest)4 MeterOperation (org.onosproject.net.meter.MeterOperation)4 MeterRequest (org.onosproject.net.meter.MeterRequest)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Maps (com.google.common.collect.Maps)3 Collection (java.util.Collection)3 HashSet (java.util.HashSet)3