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