ONOS-2716: Handle duplicate registration in resource store

Change-Id: Ic46ad17359d76a23fa9d9c78afc4fc65a41d1dbc
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 cf22ef2..06788cb 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
@@ -15,7 +15,7 @@
     /**
      * Registers the resources in transactional way.
      * Resource registration is must be done before resource allocation. The state after completion
-     * of this method is all the resources are registered, or no resource is registered.
+     * of this method is all the resources are registered, or none of the given resources is registered.
      * The whole registration fails when any one of the resource can't be registered.
      *
      * @param resources resources to be registered
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 8347c03..c955d3c 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
@@ -278,6 +278,7 @@
 
     /**
      * Appends the values to the existing values associated with the specified key.
+     * If the map already has all the given values, appending will not happen.
      *
      * @param map map holding multiple values for a key
      * @param key key specifying values
@@ -288,16 +289,18 @@
      */
     private <K, V> boolean appendValues(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<>(values);
-        } else {
-            LinkedHashSet<V> newSet = new LinkedHashSet<>(oldValues);
-            newSet.addAll(values);
-            newValues = new ArrayList<>(newSet);
+            return map.replace(key, oldValues, new ArrayList<>(values));
         }
 
-        return map.replace(key, oldValues, newValues);
+        LinkedHashSet<V> oldSet = new LinkedHashSet<>(oldValues);
+        if (oldSet.containsAll(values)) {
+            // don't write to map because all values are already stored
+            return true;
+        }
+
+        oldSet.addAll(values);
+        return map.replace(key, oldValues, new ArrayList<>(oldSet));
     }
 
     /**