MeterManager, check max meters before requesting a new meter

Change-Id: I5a5d55ede89bee46c6442b885fe64f231a14e165
This commit is contained in:
Jordi Ortiz 2016-12-27 18:50:14 +01:00 committed by Pier Luigi
parent aa8de49765
commit 21daebff4b

View File

@ -30,6 +30,7 @@ import org.onosproject.net.meter.Meter;
import org.onosproject.net.meter.MeterEvent; import org.onosproject.net.meter.MeterEvent;
import org.onosproject.net.meter.MeterFailReason; import org.onosproject.net.meter.MeterFailReason;
import org.onosproject.net.meter.MeterFeatures; import org.onosproject.net.meter.MeterFeatures;
import org.onosproject.net.meter.MeterFeaturesKey;
import org.onosproject.net.meter.MeterId; import org.onosproject.net.meter.MeterId;
import org.onosproject.net.meter.MeterKey; import org.onosproject.net.meter.MeterKey;
import org.onosproject.net.meter.MeterListener; import org.onosproject.net.meter.MeterListener;
@ -173,14 +174,26 @@ public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, M
} }
private MeterId allocateMeterId(DeviceId deviceId) { private MeterId allocateMeterId(DeviceId deviceId) {
long maxMeters = store.getMaxMeters(MeterFeaturesKey.key(deviceId));
if (maxMeters == 0L) {
throw new IllegalStateException("Meters not supported by device " + deviceId);
}
final long mmeters = maxMeters;
long id = meterIdCounters.compute(deviceId, (k, v) -> { long id = meterIdCounters.compute(deviceId, (k, v) -> {
if (v == null) { if (v == null) {
return allocateCounter(k); return allocateCounter(k);
} }
if (v.get() >= mmeters) {
throw new IllegalStateException("Maximum number of meters " +
meterIdCounters.get(deviceId).get() +
" reached for device " + deviceId);
}
return v; return v;
}).incrementAndGet(); }).incrementAndGet();
return MeterId.meterId((int) id); return MeterId.meterId(id);
} }
private AtomicCounter allocateCounter(DeviceId deviceId) { private AtomicCounter allocateCounter(DeviceId deviceId) {