Search in sources :

Example 1 with MeterCellId

use of org.onosproject.net.meter.MeterCellId 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 2 with MeterCellId

use of org.onosproject.net.meter.MeterCellId in project onos by opennetworkinglab.

the class DistributedMeterStore method firstReusableMeterId.

// Implements reuse strategy of the meter cell ids
private MeterCellId firstReusableMeterId(MeterTableKey meterTableKey) {
    DistributedSet<MeterKey> keySet = availableMeterIds.get(meterTableKey);
    if (keySet == null) {
        log.warn("Reusable Key set for device: {} scope: {} not found", meterTableKey.deviceId(), meterTableKey.scope());
        return null;
    }
    Set<MeterCellId> localAvailableMeterIds = keySet.stream().filter(meterKey -> meterKey.deviceId().equals(meterTableKey.deviceId())).map(MeterKey::meterCellId).collect(Collectors.toSet());
    MeterCellId meterId = getNextAvailableId(localAvailableMeterIds);
    while (meterId != null) {
        if (updateMeterIdAvailability(meterTableKey, meterId, false)) {
            return meterId;
        }
        localAvailableMeterIds.remove(meterId);
        meterId = getNextAvailableId(localAvailableMeterIds);
    }
    // there are no available ids that can be reused
    return null;
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) MeterCellId(org.onosproject.net.meter.MeterCellId) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId)

Example 3 with MeterCellId

use of org.onosproject.net.meter.MeterCellId in project onos by opennetworkinglab.

the class DistributedMeterStore method allocateMeterId.

@Override
public MeterCellId allocateMeterId(DeviceId deviceId, MeterScope meterScope) {
    if (userDefinedIndexMode) {
        log.warn("Unable to allocate meter id when user defined index mode is enabled");
        return null;
    }
    MeterTableKey meterTableKey = MeterTableKey.key(deviceId, meterScope);
    MeterCellId meterCellId;
    long id;
    // First, search for reusable key
    meterCellId = firstReusableMeterId(meterTableKey);
    if (meterCellId != null) {
        return meterCellId;
    }
    // If there was no reusable meter id we have to generate a new value
    // using start and end index as lower and upper bound respectively.
    long startIndex = getStartIndex(meterTableKey);
    long endIndex = getEndIndex(meterTableKey);
    // If the device does not give us MeterFeatures fallback to queryMeters
    if (startIndex == -1L || endIndex == -1L) {
        // Only meaningful for OpenFlow today
        long maxMeters = queryMaxMeters(deviceId);
        if (maxMeters == 0L) {
            return null;
        } else {
            // OpenFlow meter index starts from 1, ends with max
            startIndex = 1L;
            endIndex = maxMeters;
        }
    }
    do {
        id = meterIdGenerators.getAndIncrement(meterTableKey);
    } while (id < startIndex);
    if (id > endIndex) {
        return null;
    }
    // return a MeterId, otherwise we create a PiMeterCellId
    if (meterScope.isGlobal()) {
        return MeterId.meterId(id);
    } else {
        return PiMeterCellId.ofIndirect(PiMeterId.of(meterScope.id()), id);
    }
}
Also used : MeterTableKey(org.onosproject.net.meter.MeterTableKey) MeterCellId(org.onosproject.net.meter.MeterCellId) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId)

Example 4 with MeterCellId

use of org.onosproject.net.meter.MeterCellId in project onos by opennetworkinglab.

the class DistributedMeterStoreTest method testGetMetersImmutability.

/**
 * Test getMeters API immutability.
 */
@Test
public void testGetMetersImmutability() {
    initMeterStore(false);
    MeterCellId idOne = meterStore.allocateMeterId(did1, MeterScope.globalScope());
    assertThat(mid1, is(idOne));
    Meter meterOne = DefaultMeter.builder().forDevice(did1).fromApp(APP_ID).withId(mid1).withUnit(Meter.Unit.KB_PER_SEC).withBands(Collections.singletonList(b1)).build();
    ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
    meterStore.addOrUpdateMeter(meterOne);
    Collection<Meter> meters = meterStore.getAllMeters();
    Collection<Meter> metersDevice = meterStore.getAllMeters(did1);
    assertThat(1, is(meters.size()));
    assertThat(1, is(metersDevice.size()));
    MeterCellId idTwo = meterStore.allocateMeterId(did1, MeterScope.globalScope());
    assertThat(mid2, is(idTwo));
    Meter meterTwo = DefaultMeter.builder().forDevice(did1).fromApp(APP_ID).withId(mid2).withUnit(Meter.Unit.KB_PER_SEC).withBands(Collections.singletonList(b1)).build();
    ((DefaultMeter) meterTwo).setState(MeterState.PENDING_ADD);
    meterStore.addOrUpdateMeter(meterTwo);
    assertThat(1, is(meters.size()));
    assertThat(1, is(metersDevice.size()));
    meters = meterStore.getAllMeters();
    metersDevice = meterStore.getAllMeters(did1);
    assertThat(2, is(meters.size()));
    assertThat(2, is(metersDevice.size()));
}
Also used : DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter) DefaultMeter(org.onosproject.net.meter.DefaultMeter) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) MeterCellId(org.onosproject.net.meter.MeterCellId) Test(org.junit.Test)

Example 5 with MeterCellId

use of org.onosproject.net.meter.MeterCellId in project onos by opennetworkinglab.

the class DistributedMeterStoreTest method testNoDeleteMeter.

/**
 * Test no delete meter.
 */
@Test
public void testNoDeleteMeter() {
    initMeterStore(false);
    MeterCellId idOne = meterStore.allocateMeterId(did1, MeterScope.globalScope());
    MeterKey keyOne = MeterKey.key(did1, idOne);
    Meter meterOne = DefaultMeter.builder().forDevice(did1).fromApp(APP_ID).withId(mid1).withUnit(Meter.Unit.KB_PER_SEC).withBands(Collections.singletonList(b1)).build();
    ((DefaultMeter) meterOne).setState(MeterState.PENDING_REMOVE);
    meterStore.deleteMeter(meterOne);
    assertThat(0, is(meterStore.getAllMeters().size()));
    assertThat(0, is(meterStore.getAllMeters(did1).size()));
    assertNull(meterStore.getMeter(keyOne));
}
Also used : MeterKey(org.onosproject.net.meter.MeterKey) DefaultMeter(org.onosproject.net.meter.DefaultMeter) Meter(org.onosproject.net.meter.Meter) DefaultMeter(org.onosproject.net.meter.DefaultMeter) PiMeterCellId(org.onosproject.net.pi.runtime.PiMeterCellId) MeterCellId(org.onosproject.net.meter.MeterCellId) Test(org.junit.Test)

Aggregations

MeterCellId (org.onosproject.net.meter.MeterCellId)13 PiMeterCellId (org.onosproject.net.pi.runtime.PiMeterCellId)13 Meter (org.onosproject.net.meter.Meter)7 Test (org.junit.Test)5 DeviceId (org.onosproject.net.DeviceId)5 DefaultMeter (org.onosproject.net.meter.DefaultMeter)5 MeterService (org.onosproject.net.meter.MeterService)5 Path (javax.ws.rs.Path)4 MeterKey (org.onosproject.net.meter.MeterKey)4 DefaultMeterRequest (org.onosproject.net.meter.DefaultMeterRequest)3 MeterRequest (org.onosproject.net.meter.MeterRequest)3 MeterScope (org.onosproject.net.meter.MeterScope)3 ExecutionException (java.util.concurrent.ExecutionException)2 DELETE (javax.ws.rs.DELETE)2 GET (javax.ws.rs.GET)2 Produces (javax.ws.rs.Produces)2 CoreService (org.onosproject.core.CoreService)1 MeterTableKey (org.onosproject.net.meter.MeterTableKey)1