Avoid extracting resoure set from backing range set
This is one of fixes for ONOS-4684
Change-Id: I5192df366d16b8aad5f8987abbf539897ca0e51c
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
index f1b8f89..3dc12b3 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodableDiscreteResources.java
@@ -122,7 +122,16 @@
@Override
public DiscreteResources add(DiscreteResources other) {
if (other instanceof EncodableDiscreteResources) {
- return of(parent, Sets.union(this.values(), other.values()));
+ EncodableDiscreteResources cast = (EncodableDiscreteResources) other;
+ LinkedHashMap<Class<?>, EncodedDiscreteResources> newMap =
+ Stream.concat(this.map.entrySet().stream(), cast.map.entrySet().stream())
+ .collect(Collectors.toMap(
+ Map.Entry::getKey,
+ Map.Entry::getValue,
+ EncodedDiscreteResources::add,
+ LinkedHashMap::new
+ ));
+ return new EncodableDiscreteResources(parent, newMap);
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
index 5ccb6d2..d02e91c 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/EncodedDiscreteResources.java
@@ -93,6 +93,15 @@
return new EncodedDiscreteResources(newRangeSet, this.codec);
}
+ EncodedDiscreteResources add(EncodedDiscreteResources other) {
+ checkArgument(this.codec.getClass() == other.codec.getClass());
+
+ RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
+ newRangeSet.addAll(other.rangeSet);
+
+ return new EncodedDiscreteResources(newRangeSet, this.codec);
+ }
+
boolean isEmpty() {
return rangeSet.isEmpty();
}
diff --git a/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java
index fe1e781..066f57f 100644
--- a/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java
+++ b/core/store/dist/src/test/java/org/onosproject/store/resource/impl/EncodedDiscreteResourcesTest.java
@@ -62,4 +62,17 @@
assertThat(sut.difference(other), is(EncodedDiscreteResources.of(ImmutableSet.of(res2), new VlanIdCodec())));
}
+ @Test
+ public void testAdd() {
+ DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
+ DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
+ DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
+
+ EncodedDiscreteResources sut = EncodedDiscreteResources.of(ImmutableSet.of(res1, res2), new VlanIdCodec());
+ EncodedDiscreteResources other = EncodedDiscreteResources.of(ImmutableSet.of(res1, res3), new VlanIdCodec());
+
+ assertThat(sut.add(other),
+ is(EncodedDiscreteResources.of(ImmutableSet.of(res1, res2, res3), new VlanIdCodec())));
+ }
+
}