ONOS-2710: Change singnatures of register/unregister in ResourceStore

Change-Id: I2e90871d79cac94474a39c797168870173441eeb
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 0189a57..cf22ef2 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
@@ -13,28 +13,26 @@
 public interface ResourceStore {
 
     /**
-     * Registers the resources as children of the parent resource in transactional way.
+     * 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.
      * The whole registration fails when any one of the resource can't be registered.
      *
-     * @param parent resource which is the parent of the resource to be registered
-     * @param children resources to be registered
+     * @param resources resources to be registered
      * @return true if the registration succeeds, false otherwise
      */
-    boolean register(ResourcePath parent, List<ResourcePath> children);
+    boolean register(List<ResourcePath> resources);
 
     /**
-     * Unregisters the resources as children of the parent resource in transactional way.
+     * 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
      * resource can't be unregistered.
      *
-     * @param parent resource which is the parent of the resource to be unregistered
-     * @param children resources to be unregistered
+     * @param resources resources to be unregistered
      * @return true if the registration succeeds, false otherwise
      */
-    boolean unregister(ResourcePath parent, List<ResourcePath> children);
+    boolean unregister(List<ResourcePath> resources);
 
     /**
      * Allocates the specified resources to the specified consumer in transactional way.
diff --git a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
index b8c31a4..2cd1a2e 100644
--- a/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
+++ b/core/net/src/main/java/org/onosproject/net/newresource/impl/ResourceManager.java
@@ -133,7 +133,7 @@
         checkArgument(!children.isEmpty());
 
         List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
-        return store.register(parent, resources);
+        return store.register(resources);
     }
 
     @Override
@@ -143,6 +143,6 @@
         checkArgument(!children.isEmpty());
 
         List<ResourcePath> resources = Lists.transform(children, x -> ResourcePath.child(parent, x));
-        return store.unregister(parent, resources);
+        return store.unregister(resources);
     }
 }
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 cf199ec..a071414 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
@@ -94,9 +94,8 @@
     }
 
     @Override
-    public boolean register(ResourcePath resource, List<ResourcePath> children) {
-        checkNotNull(resource);
-        checkNotNull(children);
+    public boolean register(List<ResourcePath> resources) {
+        checkNotNull(resources);
 
         TransactionContext tx = service.transactionContextBuilder().build();
         tx.begin();
@@ -105,12 +104,18 @@
             TransactionalMap<ResourcePath, List<ResourcePath>> childTxMap =
                     tx.getTransactionalMap(CHILD_MAP, SERIALIZER);
 
-            if (!isRegistered(childTxMap, resource)) {
-                return abortTransaction(tx);
-            }
+            Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
+                    .filter(x -> x.parent().isPresent())
+                    .collect(Collectors.groupingBy(x -> x.parent().get()));
 
-            if (!appendValues(childTxMap, resource, children)) {
-                return abortTransaction(tx);
+            for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
+                if (!isRegistered(childTxMap, entry.getKey())) {
+                    return abortTransaction(tx);
+                }
+
+                if (!appendValues(childTxMap, entry.getKey(), entry.getValue())) {
+                    return abortTransaction(tx);
+                }
             }
 
             return commitTransaction(tx);
@@ -121,9 +126,8 @@
     }
 
     @Override
-    public boolean unregister(ResourcePath resource, List<ResourcePath> children) {
-        checkNotNull(resource);
-        checkNotNull(children);
+    public boolean unregister(List<ResourcePath> resources) {
+        checkNotNull(resources);
 
         TransactionContext tx = service.transactionContextBuilder().build();
         tx.begin();
@@ -134,14 +138,20 @@
             TransactionalMap<ResourcePath, ResourceConsumer> consumerTxMap =
                     tx.getTransactionalMap(CONSUMER_MAP, SERIALIZER);
 
+            Map<ResourcePath, List<ResourcePath>> resourceMap = resources.stream()
+                    .filter(x -> x.parent().isPresent())
+                    .collect(Collectors.groupingBy(x -> x.parent().get()));
+
             // even if one of the resources is allocated to a consumer,
             // all unregistrations are regarded as failure
-            if (children.stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
-                return abortTransaction(tx);
-            }
+            for (Map.Entry<ResourcePath, List<ResourcePath>> entry: resourceMap.entrySet()) {
+                if (entry.getValue().stream().anyMatch(x -> consumerTxMap.get(x) != null)) {
+                    return abortTransaction(tx);
+                }
 
-            if (!removeValues(childTxMap, resource, children)) {
-                return abortTransaction(tx);
+                if (!removeValues(childTxMap, entry.getKey(), entry.getValue())) {
+                    return abortTransaction(tx);
+                }
             }
 
             return commitTransaction(tx);