Immutability for getMeters APIs
Change-Id: Iaf908766aa360e84e82306e398fff56c9593d8f1
diff --git a/core/store/dist/src/main/java/org/onosproject/store/meter/impl/DistributedMeterStore.java b/core/store/dist/src/main/java/org/onosproject/store/meter/impl/DistributedMeterStore.java
index 9aec726..11570f7 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/meter/impl/DistributedMeterStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/meter/impl/DistributedMeterStore.java
@@ -16,6 +16,7 @@
package org.onosproject.store.meter.impl;
import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@@ -88,6 +89,7 @@
private static final String METERSTORE = "onos-meter-store";
private ConsistentMap<MeterKey, MeterData> meters;
private MapEventListener<MeterKey, MeterData> mapListener = new InternalMapEventListener();
+ private Map<MeterKey, MeterData> metersMap;
// Meters features related objects
private static final String METERFEATURESSTORE = "onos-meter-features-store";
@@ -157,6 +159,7 @@
Meter.Unit.class,
MeterFailReason.class,
MeterFeaturesFlag.class)).build();
+ metersMap = meters.asJavaMap();
// Init the set of the available ids
availableMeterIds = new DefaultDistributedSet<>(storageService.<MeterKey>setBuilder()
.withName(AVAILABLEMETERIDSTORE)
@@ -276,10 +279,10 @@
}
@Override
- public void updateMeterState(Meter meter) {
+ public Meter updateMeterState(Meter meter) {
// Update meter if present (stats workflow)
MeterKey key = MeterKey.key(meter.deviceId(), meter.id());
- meters.computeIfPresent(key, (k, v) -> {
+ Versioned<MeterData> value = meters.computeIfPresent(key, (k, v) -> {
DefaultMeter m = (DefaultMeter) v.meter();
MeterState meterState = m.state();
if (meterState == MeterState.PENDING_ADD) {
@@ -292,6 +295,7 @@
m.setReferenceCount(meter.referenceCount());
return new MeterData(m, null);
});
+ return value != null ? value.value().meter() : null;
}
@Override
@@ -302,14 +306,14 @@
@Override
public Collection<Meter> getAllMeters() {
- return Collections2.transform(meters.asJavaMap().values(),
+ return Collections2.transform(ImmutableSet.copyOf(metersMap.values()),
MeterData::meter);
}
@Override
public Collection<Meter> getAllMeters(DeviceId deviceId) {
return Collections2.transform(
- Collections2.filter(meters.asJavaMap().values(),
+ Collections2.filter(ImmutableSet.copyOf(metersMap.values()),
(MeterData m) -> m.meter().deviceId().equals(deviceId)),
MeterData::meter);
}
diff --git a/core/store/dist/src/test/java/org/onosproject/store/meter/impl/DistributedMeterStoreTest.java b/core/store/dist/src/test/java/org/onosproject/store/meter/impl/DistributedMeterStoreTest.java
index 2566803..4cddf54 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/meter/impl/DistributedMeterStoreTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/meter/impl/DistributedMeterStoreTest.java
@@ -45,6 +45,7 @@
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.TestStorageService;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.concurrent.CompletableFuture;
@@ -415,6 +416,64 @@
assertNull(meterStore.getMeter(keyOne));
}
+ /**
+ * Test getMeters API immutability.
+ */
+ @Test
+ public void testGetMetersImmutability() {
+ // Init the store
+ initMeterStore();
+
+ // Simulate the allocation of an id
+ MeterId idOne = meterStore.allocateMeterId(did1);
+ // Verify the allocation
+ assertThat(mid1, is(idOne));
+ // Let's create a meter
+ Meter meterOne = DefaultMeter.builder()
+ .forDevice(did1)
+ .fromApp(APP_ID)
+ .withId(mid1)
+ .withUnit(Meter.Unit.KB_PER_SEC)
+ .withBands(Collections.singletonList(b1))
+ .build();
+ // Set the state
+ ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
+ // Store the meter
+ meterStore.storeMeter(meterOne);
+
+ // Verify the immutability
+ Collection<Meter> meters = meterStore.getAllMeters();
+ Collection<Meter> metersDevice = meterStore.getAllMeters(did1);
+ assertThat(1, is(meters.size()));
+ assertThat(1, is(metersDevice.size()));
+
+ MeterId idTwo = meterStore.allocateMeterId(did1);
+ // Verify the allocation
+ assertThat(mid2, is(idTwo));
+ // Let's create a meter
+ Meter meterTwo = DefaultMeter.builder()
+ .forDevice(did1)
+ .fromApp(APP_ID)
+ .withId(mid2)
+ .withUnit(Meter.Unit.KB_PER_SEC)
+ .withBands(Collections.singletonList(b1))
+ .build();
+ // Set the state
+ ((DefaultMeter) meterTwo).setState(MeterState.PENDING_ADD);
+ // Store the meter
+ meterStore.storeMeter(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()));
+
+
+ }
+
// Test class for driver service.
private class TestDriverService extends DriverServiceAdapter {
@Override