Resource Store Defect Fix: Allowing allocation/release of more than 1 continous resource from same consumer with same value

Change-Id: Ic143b263d5e5922c7bf4b78309bf3b2a38194a0b
diff --git a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
index c8c91c8..897b487 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/resource/impl/TransactionalContinuousResourceStore.java
@@ -33,6 +33,7 @@
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static org.onosproject.store.resource.impl.ConsistentResourceStore.SERIALIZER;
@@ -162,11 +163,6 @@
             return true;
         }
 
-        if (oldValue.allocations().contains(value)) {
-            // don't write to map because all values are already stored
-            return true;
-        }
-
         ContinuousResourceAllocation newValue = new ContinuousResourceAllocation(original,
                 ImmutableList.<ResourceAllocation>builder()
                         .addAll(oldValue.allocations())
@@ -177,13 +173,26 @@
 
     boolean release(ContinuousResource resource, ResourceConsumer consumer) {
         ContinuousResourceAllocation oldAllocation = consumers.get(resource.id());
-        ImmutableList<ResourceAllocation> newAllocations = oldAllocation.allocations().stream()
+
+        List<ResourceAllocation> nonMatchResources = oldAllocation.allocations().stream()
                 .filter(x -> !(x.consumer().equals(consumer) &&
                         ((ContinuousResource) x.resource()).value() == resource.value()))
-                .collect(GuavaCollectors.toImmutableList());
+                .collect(Collectors.toList());
+
+        List<ResourceAllocation> matchResources = oldAllocation.allocations().stream()
+                .filter(x -> (x.consumer().equals(consumer) &&
+                        ((ContinuousResource) x.resource()).value() == resource.value()))
+                .collect(Collectors.toList());
+
+        if (matchResources.size() > 1) {
+            matchResources.remove(0);
+        }
+
+        ImmutableList<ResourceAllocation> finalAllocations = Stream.concat(nonMatchResources.stream(),
+                matchResources.stream()).collect(GuavaCollectors.toImmutableList());
 
         if (!consumers.replace(resource.id(), oldAllocation,
-                new ContinuousResourceAllocation(oldAllocation.original(), newAllocations))) {
+                new ContinuousResourceAllocation(oldAllocation.original(), finalAllocations))) {
             return false;
         }