ONOS-6787
Dynamic Config: API cleanup and Rpc brokerage implementation

Change-Id: Ic8b9922533c9bb8869d3b4c0ed55611a0e61f4f5
diff --git a/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java b/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
index ec856ad..72e9d8b 100755
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
@@ -20,12 +20,11 @@
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.ResourceId;
 import org.onosproject.event.ListenerService;
-import org.onosproject.yang.model.RpcCaller;
-import org.onosproject.yang.model.RpcCommand;
-import org.onosproject.yang.model.RpcHandler;
 import org.onosproject.yang.model.RpcInput;
 import org.onosproject.yang.model.RpcOutput;
 
+import java.util.concurrent.CompletableFuture;
+
 /**
  * Service for storing and distributing dynamic configuration data.
  */
@@ -43,7 +42,7 @@
      * @param node recursive data structure, holding a leaf node or a subtree
      * @throws FailedException if the new node could not be created
      */
-    void createNodeRecursive(ResourceId path, DataNode node);
+    void createNode(ResourceId path, DataNode node);
 
     /**
      * Reads the requested node form the dynamic config store.
@@ -61,19 +60,6 @@
     DataNode readNode(ResourceId path, Filter filter);
 
     /**
-     * Returns the number of children under the node at the given path.
-     * This method would throw an exception if the requested node or any parent
-     * nodes in the path were not present.
-     * Failure reason will be the error message in the exception.
-     *
-     * @param path data structure with absolute path to the intended node
-     * @param filter filtering conditions to be applied on the result list of nodes
-     * @return the number of children after applying the filtering conditions if any
-     * @throws FailedException if the request failed
-     */
-    Integer getNumberOfChildren(ResourceId path, Filter filter);
-
-    /**
      * Returns whether the requested node exists in the Dynamic Config store.
      *
      * @param path data structure with absolute path to the intended node
@@ -84,7 +70,7 @@
 
     /**
      * Updates an existing node in the dynamic config store.
-     * Any missing children nodes will be created with this request.
+     * Existing nodes will be updated and missing nodes will be created as needed.
      * This method would throw an exception if the requested node or any of the
      * parent nodes in the path were not present.
      * Failure reason will be the error message in the exception.
@@ -109,11 +95,10 @@
     void replaceNode(ResourceId path, DataNode node);
 
     /**
-     * Removes a leaf node from the dynamic config store.
-     * This method would throw an exception if the requested node or any of the
-     * parent nodes in the path were not present or the specified node is the
-     * root node or has one or more children.
-     * Failure reason will be the error message in the exception.
+     * Removes a node from the dynamic config store.
+     * If the node pointed to a subtree, that will be deleted recursively.
+     * It will throw an exception if the requested node or any of the parent nodes in the
+     * path were not present; Failure reason will be the error message in the exception.
      *
      * @param path data structure with absolute path to the intended node
      * @throws FailedException if the delete request failed
@@ -121,53 +106,12 @@
     void deleteNode(ResourceId path);
 
     /**
-     * Removes a subtree from the dynamic config store.
-     * This method will delete all the children recursively, under the given
-     * node. It will throw an exception if the requested node or any of the
-     * parent nodes in the path were not present.
-     * Failure reason will be the error message in the exception.
-     *
-     * @param path data structure with absolute path to the intended node
-     * @throws FailedException if the delete request failed
-     */
-    void deleteNodeRecursive(ResourceId path);
-
-    /**
-     * Registers an RPC handler.
-     *
-     * @param handler RPC handler
-     * @param command RPC command
-     * @throws FailedException if the handler could not be added
-     */
-    void registerHandler(RpcHandler handler, RpcCommand command);
-
-    /**
-     * Unregisters an RPC receiver.
-     *
-     * @param handler RPC handler
-     * @param command RPC command
-     * @throws FailedException if the handler could not be removed
-     */
-    void unRegisterHandler(RpcHandler handler, RpcCommand command);
-
-    /**
      * Invokes an RPC.
      *
-     * @param caller of the of the RPC
-     * @param msgId RPC message id
-     * @param command RPC command
+     * @param id of RPC node
      * @param input RPC input
+     * @return future that will be completed with RpcOutput
      * @throws FailedException if the RPC could not be invoked
      */
-    void invokeRpc(RpcCaller caller, Integer msgId, RpcCommand command, RpcInput input);
-
-    /**
-     * Provides response to a a previously invoked RPC.
-     *
-     * @param msgId of a previously invoked RPC
-     * @param output data from the RPC execution
-     * @throws FailedException if the RPC response was invalid
-     * (or the msg id was not recognised by the store)
-     */
-    void rpcResponse(Integer msgId, RpcOutput output);
+    CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input);
 }
\ No newline at end of file
diff --git a/apps/config/src/main/java/org/onosproject/config/impl/DynamicConfigManager.java b/apps/config/src/main/java/org/onosproject/config/impl/DynamicConfigManager.java
index fac5073..5ec5422 100644
--- a/apps/config/src/main/java/org/onosproject/config/impl/DynamicConfigManager.java
+++ b/apps/config/src/main/java/org/onosproject/config/impl/DynamicConfigManager.java
@@ -23,6 +23,9 @@
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.onlab.util.KryoNamespace;
+import org.onosproject.event.AbstractListenerManager;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.StorageService;
 import org.onosproject.config.DynamicConfigEvent;
 import org.onosproject.config.DynamicConfigListener;
 import org.onosproject.config.DynamicConfigService;
@@ -30,25 +33,21 @@
 import org.onosproject.config.DynamicConfigStoreDelegate;
 import org.onosproject.config.FailedException;
 import org.onosproject.config.Filter;
-import org.onosproject.store.serializers.KryoNamespaces;
-import org.onosproject.store.service.ConsistentMap;
-import org.onosproject.store.service.StorageService;
-import org.onosproject.store.service.Serializer;
-import org.onosproject.store.service.Versioned;
-import org.onosproject.yang.model.RpcCaller;
-import org.onosproject.yang.model.RpcCommand;
 import org.onosproject.yang.model.RpcHandler;
 import org.onosproject.yang.model.RpcInput;
 import org.onosproject.yang.model.RpcOutput;
 import org.onosproject.yang.model.DataNode;
 import org.onosproject.yang.model.ResourceId;
-import org.onosproject.event.AbstractListenerManager;
-import org.slf4j.Logger;
+//TODO import org.onosproject.yang.model.RpcRegistry;
+//TODO import org.onosproject.yang.model.RpcService;
 
+import java.util.concurrent.CompletableFuture;
+
+import org.slf4j.Logger;
 import static org.slf4j.LoggerFactory.getLogger;
 
 /**
- * Demo application to use the DynamicConfig Service and DynamicConfigStore.
+ * Implementation of the Dynamic Config Service.
  *
  */
 @Beta
@@ -61,8 +60,8 @@
     private final DynamicConfigStoreDelegate storeDelegate = new InternalStoreDelegate();
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DynamicConfigStore store;
-    private ConsistentMap<RpcCommand, RpcHandler> handlerRegistry;
-    private ConsistentMap<Integer, RpcCaller> callerRegistry;
+    //TODO after 14420 is merged
+    //private ConsistentMap<RpcService, RpcHandler> handlerRegistry;
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected StorageService storageService;
 
@@ -75,19 +74,13 @@
                 .register(KryoNamespaces.BASIC)
                 .register(Class.class)
                 .register(RpcHandler.class)
-                .register(RpcCaller.class)
-                .register(RpcCommand.class)
                 .register(ResourceId.class);
-        callerRegistry = storageService.<Integer, RpcCaller>consistentMapBuilder()
+        //TODO after 14420 is merged
+        /*handlerRegistry = storageService.<RpcService, RpcHandler>consistentMapBuilder()
                 .withSerializer(Serializer.using(kryoBuilder.build()))
                 .withName("config-object-store")
                 .withRelaxedReadConsistency()
-                .build();
-        handlerRegistry = storageService.<RpcCommand, RpcHandler>consistentMapBuilder()
-                .withSerializer(Serializer.using(kryoBuilder.build()))
-                .withName("config-object-store")
-                .withRelaxedReadConsistency()
-                .build();
+                .build();*/
         log.info("Started");
     }
 
@@ -98,7 +91,7 @@
         log.info("Stopped");
     }
 
-    public void createNodeRecursive(ResourceId path, DataNode node) {
+    public void createNode(ResourceId path, DataNode node) {
         store.addNode(path, node).join();
     }
 
@@ -111,10 +104,6 @@
     }
 
     public void deleteNode(ResourceId path) {
-        throw new FailedException("Not yet implemented");
-    }
-
-    public void deleteNodeRecursive(ResourceId path) {
         store.deleteNodeRecursive(path).join();
     }
 
@@ -122,15 +111,12 @@
         throw new FailedException("Not yet implemented");
     }
 
-    public Integer getNumberOfChildren(ResourceId path, Filter filter) {
-        throw new FailedException("Not yet implemented");
-    }
-
     public Boolean nodeExist(ResourceId path) {
         return store.nodeExist(path).join();
     }
 
-    public void registerHandler(RpcHandler handler, RpcCommand command) {
+    //TODO after RPC abstractions are merged; else will lead to build failure
+    /*public void registerHandler(RpcHandler handler, RpcCommand command) {
         handlerRegistry.put(command, handler);
     }
 
@@ -140,24 +126,13 @@
             throw new FailedException("No registered handler found, cannot unregister");
         }
         handlerRegistry.remove(command);
+    }*/
+
+    public CompletableFuture<RpcOutput> invokeRpc(ResourceId id, RpcInput input) {
+        //TODO after RPC abstractions are merged; else will lead to build failure
+        throw new FailedException("Not yet implemented");
     }
 
-    public void invokeRpc(RpcCaller caller, Integer msgId, RpcCommand command, RpcInput input) {
-        callerRegistry.put(msgId, caller);
-        Versioned<RpcHandler> hndlr = handlerRegistry.get(command);
-        if ((hndlr == null) || (hndlr.value() == null)) {
-            throw new FailedException("No registered handler found, cannot invoke");
-        }
-        hndlr.value().executeRpc(msgId, command, input);
-    }
-
-    public void rpcResponse(Integer msgId, RpcOutput output) {
-        Versioned<RpcCaller> caller = callerRegistry.get(msgId);
-        if (caller.value() == null) {
-            throw new FailedException("No registered receiver found, cannot relay response");
-        }
-        caller.value().receiveResponse(msgId, output);
-    }
     /**
      * Auxiliary store delegate to receive notification about changes in the store.
      */
diff --git a/apps/l3vpn/src/main/java/org/onosproject/l3vpn/netl3vpn/impl/NetL3VpnManager.java b/apps/l3vpn/src/main/java/org/onosproject/l3vpn/netl3vpn/impl/NetL3VpnManager.java
index b39a4c1..79cba2e 100644
--- a/apps/l3vpn/src/main/java/org/onosproject/l3vpn/netl3vpn/impl/NetL3VpnManager.java
+++ b/apps/l3vpn/src/main/java/org/onosproject/l3vpn/netl3vpn/impl/NetL3VpnManager.java
@@ -612,7 +612,7 @@
         if (resData != null && resData.dataNodes() != null) {
             List<DataNode> dataNodes = resData.dataNodes();
             for (DataNode node : dataNodes) {
-                configService.createNodeRecursive(resData.resourceId(), node);
+                configService.createNode(resData.resourceId(), node);
             }
         }
     }
@@ -960,7 +960,7 @@
      */
     private void deleteFromStore(ResourceData resData) {
         if (resData != null) {
-            configService.deleteNodeRecursive(resData.resourceId());
+            configService.deleteNode(resData.resourceId());
         }
     }
 
diff --git a/apps/restconf/restconfmgr/src/main/java/org/onosproject/restconf/restconfmanager/RestconfManager.java b/apps/restconf/restconfmgr/src/main/java/org/onosproject/restconf/restconfmanager/RestconfManager.java
index 1ad10fd..2d32379 100644
--- a/apps/restconf/restconfmgr/src/main/java/org/onosproject/restconf/restconfmanager/RestconfManager.java
+++ b/apps/restconf/restconfmgr/src/main/java/org/onosproject/restconf/restconfmanager/RestconfManager.java
@@ -141,7 +141,7 @@
         }
         DataNode dataNode = dataNodeList.get(0);
         try {
-            dynamicConfigService.createNodeRecursive(rid, dataNode);
+            dynamicConfigService.createNode(rid, dataNode);
         } catch (FailedException e) {
             log.error("ERROR: DynamicConfigService: ", e);
             throw new RestconfException("ERROR: DynamicConfigService",
@@ -160,7 +160,7 @@
             throws RestconfException {
         ResourceId rid = convertUriToRid(uri);
         try {
-            dynamicConfigService.deleteNodeRecursive(rid);
+            dynamicConfigService.deleteNode(rid);
         } catch (FailedException e) {
             log.error("ERROR: DynamicConfigService: ", e);
             throw new RestconfException("ERROR: DynamicConfigService",