ONOS 6447 Dynamic Config Update support

Change-Id: I2d91f9d26b5a5498db7c0c2d3ad5871658499ca7
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 1ac8303..102474d 100755
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
@@ -75,18 +75,6 @@
 
     /**
      * Updates an existing node in the dynamic config store.
-     * This method would throw an exception if the requested node, any of its
-     * children 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 parent
-     * @param node recursive data structure, holding a leaf node or a subtree
-     * @throws FailedException if the update request failed
-     */
-    void updateNode(ResourceId path, DataNode node);
-
-    /**
-     * Updates an existing node in the dynamic config store.
      * Any missing children nodes will be created with this request.
      * This method would throw an exception if the requested node or any of the
      * parent nodes in the path were not present.
@@ -95,9 +83,8 @@
      * @param path data structure with absolute path to the parent
      * @param node recursive data structure, holding a leaf node or a subtree
      * @throws FailedException if the update request failed for any reason
-     *
      */
-    void updateNodeRecursive(ResourceId path, DataNode node);
+    void updateNode(ResourceId path, DataNode node);
 
     /**
      * Replaces nodes in the dynamic config store.
diff --git a/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java b/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java
index 1bed059..e07a7b7 100644
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java
@@ -59,19 +59,6 @@
 
     /**
      * Updates an existing node in the dynamic config store.
-     * This request would fail if the requested node, any of its children or
-     * any parent nodes in the path were not present.
-     *
-     * @param path data structure with absolute path to the parent
-     * @param node recursive data structure, holding a leaf node or a subtree
-     * @return future that is completed with {@code true} if the node was
-     * successfully updated or completed exceptionally with
-     * {@code FailedException} if the update request failed
-     */
-    CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node);
-
-    /**
-     * Updates an existing node in the dynamic config store.
      * Any missing children will be created with this request. The update will
      * fail if the requested node or any of the parent nodes in the path
      * were not present.
@@ -82,7 +69,7 @@
      * successfully updated or completed exceptionally with
      * {@code FailedException} if the update request failed
      */
-    CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node);
+    CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node);
 
     /**
      * Replaces nodes in the dynamic config store.
diff --git a/apps/config/src/main/java/org/onosproject/config/impl/DistributedDynamicConfigStore.java b/apps/config/src/main/java/org/onosproject/config/impl/DistributedDynamicConfigStore.java
index fa33e70..5acdacd 100644
--- a/apps/config/src/main/java/org/onosproject/config/impl/DistributedDynamicConfigStore.java
+++ b/apps/config/src/main/java/org/onosproject/config/impl/DistributedDynamicConfigStore.java
@@ -321,14 +321,42 @@
         return objectStore.get(path).value();
     }
 
-    @Override
-    public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
+    private void parseForUpdate(String path, DataNode node) {
+        if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
+            addLeaf(path, (LeafNode) node);
+        } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
+            path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
+            addLeaf(path, (LeafNode) node);
+        } else if (node.type() == DataNode.Type.SINGLE_INSTANCE_NODE) {
+            traverseInner(path, (InnerNode) node);
+        } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
+            path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
+            traverseInner(path, (InnerNode) node);
+        } else {
+            throw new FailedException("Invalid node type");
+        }
     }
 
     @Override
-    public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
+    public CompletableFuture<Boolean> updateNode(ResourceId complete, DataNode node) {
+        CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
+        List<NodeKey> nodeKeyList = complete.nodeKeys();
+        NodeKey f = nodeKeyList.get(0);
+        if (f.schemaId().name().compareTo("/") == 0) {
+            nodeKeyList.remove(0);
+        }
+        String spath = ResourceIdParser.parseResId(complete);
+        if (spath == null) {
+            throw new FailedException("Invalid RsourceId, cannot update Node");
+        }
+        if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
+            if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
+                throw new FailedException("Node or parent doesnot exist, cannot update");
+            }
+        }
+        spath = ResourceIdParser.appendNodeKey(spath, node.key());
+        parseForUpdate(spath, node);
+        return eventFuture;
     }
 
     @Override
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 bc88007..9d54e5a 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
@@ -71,8 +71,7 @@
     }
 
     public void createNodeRecursive(ResourceId path, DataNode node) {
-        Boolean stat = false;
-        stat = this.store.addNode(path, node).join();
+        store.addNode(path, node).join();
     }
 
     public DataNode readNode(ResourceId path, Filter filter) {
@@ -80,7 +79,7 @@
     }
 
     public void updateNode(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
+        store.updateNode(path, node).join();
     }
 
     public void deleteNode(ResourceId path) {
@@ -91,10 +90,6 @@
         store.deleteNodeRecursive(path).join();
     }
 
-    public void updateNodeRecursive(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
-    }
-
     public void replaceNode(ResourceId path, DataNode node) {
         throw new FailedException("Not yet implemented");
     }