ONOS-2717: Handle duplicate resource unregistration in resource store

Change-Id: I49648be9c26ba66218172d570632d54584a908d8
diff --git a/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java b/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
index 0aa4827..fc2eba7 100644
--- a/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
+++ b/core/api/src/main/java/org/onosproject/net/newresource/ResourceStore.java
@@ -26,7 +26,7 @@
     /**
      * Unregisters the resources in transactional way.
      * The state after completion of this method is all the resources are unregistered,
-     * or no resource is unregistered. The whole unregistration fails when any one of the
+     * or none of the given resources is unregistered. The whole unregistration fails when any one of the
      * resource can't be unregistered.
      *
      * @param resources resources to be unregistered
diff --git a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
index c955d3c..648119e 100644
--- a/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
+++ b/core/store/dist/src/main/java/org/onosproject/store/newresource/impl/ConsistentResourceStore.java
@@ -305,6 +305,7 @@
 
     /**
      * Removes teh values from the existing values associated with the specified key.
+     * If the map doesn't contain the given values, removal will not happen.
      *
      * @param map map holding multiple values for a key
      * @param key key specifying values
@@ -315,16 +316,18 @@
      */
     private <K, V> boolean removeValues(TransactionalMap<K, List<V>> map, K key, List<V> values) {
         List<V> oldValues = map.get(key);
-        List<V> newValues;
         if (oldValues == null) {
-            newValues = new ArrayList<>();
-        } else {
-            LinkedHashSet<V> newSet = new LinkedHashSet<>(oldValues);
-            newSet.removeAll(values);
-            newValues = new ArrayList<>(newSet);
+            return map.replace(key, oldValues, new ArrayList<>());
         }
 
-        return map.replace(key, oldValues, newValues);
+        LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
+        if (values.stream().allMatch(x -> !oldSet.contains(x))) {
+            // don't write map because none of the values are stored
+            return true;
+        }
+
+        oldSet.removeAll(values);
+        return map.replace(key, oldValues, new ArrayList<>(oldSet));
     }
 
     /**