ONOS-6154
Demo1: Integration - Restconf Exception during GET

Change-Id: I5e41de333748ed282f70bdda6e31269668e37ed1
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 060c286..790951a 100755
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigService.java
@@ -38,20 +38,6 @@
      * @param node recursive data structure, holding a leaf node or a subtree
      * @throws FailedException if the new node could not be created
      */
-    void createNode(ResourceId path, DataNode node);
-
-    /**
-     * Creates a new node in the dynamic config store.
-     * Creates any missing parent nodes, leading up to the given node.
-     * This method will throw an exception if there is a node with the same
-     * identifier, already present at the specified path or any of the parent
-     * nodes were not present in the path leading up to the requested node.
-     * 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 new node could not be created
-     */
     void createNodeRecursive(ResourceId path, DataNode node);
 
     /**
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 9ea34f2..1bed059 100644
--- a/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java
+++ b/apps/config/src/main/java/org/onosproject/config/DynamicConfigStore.java
@@ -40,21 +40,8 @@
      * successfully added or completed exceptionally with
      * {@code FailedException} if node addition failed
      */
-    CompletableFuture<Boolean> addNode(ResourceId path, DataNode node);
 
-    /**
-     * Adds a new node in the dynamic config store.
-     * Creates any missing parent nodes, leading up to the given node. Node
-     * will not be added if there is a node with the same identifier, already
-     * present at the specified path.
-     *
-     * @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 added or completed exceptionally with
-     * {@code FailedException} if the node addition failed
-     */
-    CompletableFuture<Boolean> addRecursive(ResourceId path, DataNode node);
+    CompletableFuture<Boolean> addNode(ResourceId path, DataNode node);
 
     /**
      * Reads the requested node from the dynamic config store.
diff --git a/apps/config/src/main/java/org/onosproject/config/ResourceIdParser.java b/apps/config/src/main/java/org/onosproject/config/ResourceIdParser.java
index 23c107d..089aeff 100755
--- a/apps/config/src/main/java/org/onosproject/config/ResourceIdParser.java
+++ b/apps/config/src/main/java/org/onosproject/config/ResourceIdParser.java
@@ -36,6 +36,8 @@
     public static final String VAL_SEP = "@";
     public static final String KEY_SEP = "$";
     public static final String EL_SEP = ".";
+    public static final String VAL_CHK = "\\@";
+    public static final String KEY_CHK = "\\$";
 
 
 
@@ -69,6 +71,27 @@
         }
     }
 
+    public static String getNamespace(String nmspc) {
+        String ret = null;
+        if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
+            ret = nmspc.split(KEY_CHK)[0];
+        } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
+            ret = nmspc.split(VAL_CHK)[0];
+        } else {
+            ret = nmspc;
+        }
+        return ret;
+    }
+
+    public static String getKeyVal(String nmspc) {
+        String ret = null;
+        if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
+            ret = nmspc.split(VAL_CHK)[1];
+        }
+        return ret;
+    }
+
+
     public static String appendMultiInstKey(String path, String leaf) {
         return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
     }
@@ -97,11 +120,11 @@
     }
 
     public static String appendLeafList(String path, LeafListKey key) {
-        return (path + NM_SEP + key.asString());
+        return (path + VAL_SEP + key.asString());
     }
 
     public static String appendLeafList(String path, String val) {
-        return (path + NM_SEP + val);
+        return (path + VAL_SEP + val);
     }
 
     public static String appendKeyList(String path, ListKey key) {
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 9f31621..11dce6b 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
@@ -120,13 +120,7 @@
 
     @Override
     public CompletableFuture<Boolean>
-    addNode(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
-    }
-
-    @Override
-    public CompletableFuture<Boolean>
-    addRecursive(ResourceId complete, DataNode node) {
+    addNode(ResourceId complete, DataNode node) {
         CompletableFuture<Boolean> eventFuture = CompletableFuture.completedFuture(true);
         List<NodeKey> nodeKeyList = complete.nodeKeys();
         NodeKey f = nodeKeyList.get(0);
@@ -137,10 +131,10 @@
         if (spath == null) {
             throw new FailedException("Invalid RsourceId, cannot create Node");
         }
-        if (keystore.get(DocumentPath.from(spath)).join() == null) {
-            ////TODO recursively creating missing parents
-            throw new FailedException("Some of the parents in the path " +
-                    "are not present, creation not supported currently");
+        if (spath.compareTo(ResourceIdParser.ROOT) != 0) {
+            if (completeVersioned(keystore.get(DocumentPath.from(spath))) == null) {
+                throw new FailedException("Node or parent doesnot exist");
+            }
         }
         spath = ResourceIdParser.appendNodeKey(spath, node.key());
         parseNode(spath, node);
@@ -148,7 +142,7 @@
     }
 
     private void parseNode(String path, DataNode node) {
-        if (keystore.get(DocumentPath.from(path)).join() != null) {
+        if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
             throw new FailedException("Requested node already present in the" +
                     " store, please use an update method");
         }
@@ -156,7 +150,7 @@
             addLeaf(path, (LeafNode) node);
         } else if (node.type() == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
             path = ResourceIdParser.appendLeafList(path, (LeafListKey) node.key());
-            if (keystore.get(DocumentPath.from(path)).join() != null) {
+            if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
                 throw new FailedException("Requested node already present in the" +
                         " store, please use an update method");
             }
@@ -165,7 +159,7 @@
             traverseInner(path, (InnerNode) node);
         } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
             path = ResourceIdParser.appendKeyList(path, (ListKey) node.key());
-            if (keystore.get(DocumentPath.from(path)).join() != null) {
+            if (completeVersioned(keystore.get(DocumentPath.from(path))) != null) {
                 throw new FailedException("Requested node already present in the" +
                         " store, please use an update method");
             }
@@ -193,7 +187,7 @@
                 traverseInner(tempPath, (InnerNode) v);
             } else if (v.type() == DataNode.Type.MULTI_INSTANCE_NODE) {
                 tempPath = ResourceIdParser.appendKeyList(tempPath, (ListKey) v.key());
-                traverseInner(path, (InnerNode) v);
+                traverseInner(tempPath, (InnerNode) v);
             } else {
                 throw new FailedException("Invalid node type");
             }
@@ -252,10 +246,10 @@
                     .builder(key.schemaId().name(), key.schemaId().namespace())
                     .type(type);
             for (KeyLeaf keyLeaf : ((ListKey) key).keyLeafs()) {
-                String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
-                LeafNode lfnd = readLeaf(tempPath);
+                //String tempPath = ResourceIdParser.appendKeyLeaf(spath, keyLeaf);
+                //LeafNode lfnd = readLeaf(tempPath);
                 superBldr.addKeyLeaf(keyLeaf.leafSchema().name(),
-                        keyLeaf.leafSchema().namespace(), lfnd.value());
+                        keyLeaf.leafSchema().namespace(), keyLeaf.leafValue());
             }
             readInner(superBldr, spath);
             retVal = superBldr.build();
@@ -265,7 +259,7 @@
         if (retVal != null) {
             eventFuture = CompletableFuture.completedFuture(retVal);
         } else {
-            log.info("STORE: FAILED to READ node");
+            log.info("STORE: Failed to READ node");
         }
         return eventFuture;
     }
@@ -281,7 +275,8 @@
         entries.forEach((k, v) -> {
             String[] names = k.split(ResourceIdParser.NM_SEP);
             String name = names[0];
-            String nmSpc = names[1];
+            String nmSpc = ResourceIdParser.getNamespace(names[1]);
+            String keyVal = ResourceIdParser.getKeyVal(names[1]);
             DataNode.Type type = v.value();
             String tempPath = ResourceIdParser.appendNodeKey(spath, name, nmSpc);
             if (type == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) {
@@ -289,7 +284,7 @@
                         .type(type)
                         .exitNode();
             } else if (type == DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE) {
-                String mlpath = ResourceIdParser.appendLeafList(tempPath, names[2]);
+                String mlpath = ResourceIdParser.appendLeafList(tempPath, keyVal);
                 LeafNode lfnode = readLeaf(mlpath);
                 superBldr.createChildBuilder(name, nmSpc, lfnode.value())
                         .type(type)
@@ -304,16 +299,16 @@
                 DataNode.Builder tempBldr = superBldr.createChildBuilder(name, nmSpc)
                         .type(type);
                 tempPath = ResourceIdParser.appendMultiInstKey(tempPath, k);
-                String[] keys = k.split(ResourceIdParser.KEY_SEP);
+                String[] keys = k.split(ResourceIdParser.KEY_CHK);
                 for (int i = 1; i < keys.length; i++) {
-                    String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
-                    LeafNode lfnd = readLeaf(curKey);
+                    //String curKey = ResourceIdParser.appendKeyLeaf(tempPath, keys[i]);
+                    //LeafNode lfnd = readLeaf(curKey);
                     String[] keydata = keys[i].split(ResourceIdParser.NM_SEP);
-                    superBldr.addKeyLeaf(keydata[0], keydata[1], lfnd.value());
+                    tempBldr.addKeyLeaf(keydata[0], keydata[1], keydata[2]);
                 }
                 readInner(tempBldr, tempPath);
             } else {
-                throw new FailedException("Node type should either be LEAF or INNERNODE");
+                throw new FailedException("Invalid node type");
             }
         });
         superBldr.exitNode();
@@ -322,18 +317,22 @@
     private LeafNode readLeaf(String path) {
         return objectStore.get(path).value();
     }
+
     @Override
     public CompletableFuture<Boolean> updateNode(ResourceId path, DataNode node) {
         throw new FailedException("Not yet implemented");
     }
+
     @Override
     public CompletableFuture<Boolean> updateNodeRecursive(ResourceId path, DataNode node) {
         throw new FailedException("Not yet implemented");
     }
+
     @Override
     public CompletableFuture<Boolean> replaceNode(ResourceId path, DataNode node) {
         throw new FailedException("Not yet implemented");
     }
+
     @Override
     public CompletableFuture<Boolean> deleteNode(ResourceId path) {
         throw new FailedException("Not yet implemented");
@@ -352,7 +351,7 @@
         CompletableFuture<Versioned<DataNode.Type>> vtype = keystore.removeNode(dpath);
         type = completeVersioned(vtype);
         if (type == null) {
-            throw new FailedException("node delete failed");
+            throw new FailedException("Node delete failed");
         }
         Versioned<LeafNode> res = objectStore.remove(spath);
         if (res == null) {
@@ -369,19 +368,19 @@
             ResourceId path;
             switch (event.type()) {
                 case CREATED:
-                    log.info("NODE created in store");
                     type = NODE_ADDED;
+                    //log.info("NODE added in store");
                     break;
                 case UPDATED:
-                    log.info("NODE updated in store");
+                    //log.info("NODE updated in store");
                     type = NODE_UPDATED;
                     break;
                 case DELETED:
-                    log.info("NODE deleted in store");
+                    //log.info("NODE deleted in store");
                     type = NODE_DELETED;
                     break;
                 default:
-                    log.info("UNKNOWN operation in store");
+                    //log.info("UNKNOWN operation in store");
                     type = UNKNOWN_OPRN;
             }
             path = ResourceIdParser.getResId(event.path().pathElements());
@@ -412,9 +411,15 @@
             return future.get();
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
-            throw new FailedException(e.getCause().getMessage());
+            if (e == null) {
+                throw new FailedException("Unknown Exception");
+            } else {
+                throw new FailedException(e.getCause().getMessage());
+            }
         } catch (ExecutionException e) {
-            if (e.getCause() instanceof IllegalDocumentModificationException) {
+            if (e == null) {
+                throw new FailedException("Unknown Exception");
+            } else if (e.getCause() instanceof IllegalDocumentModificationException) {
                 throw new FailedException("Node or parent doesnot exist or is root or is not a Leaf Node");
             } else if (e.getCause() instanceof NoSuchDocumentPathException) {
                 throw new FailedException("Resource id does not exist");
@@ -426,12 +431,18 @@
 
     private <T> T completeVersioned(CompletableFuture<Versioned<T>> future) {
         try {
-            return future.get().value();
+            if (future.get() != null) {
+                return future.get().value();
+            } else {
+                return null;
+            }
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
             throw new FailedException(e.getCause().getMessage());
         } catch (ExecutionException e) {
-            if (e.getCause() instanceof IllegalDocumentModificationException) {
+            if (e == null) {
+                throw new FailedException("Unknown Exception");
+            } else if (e.getCause() instanceof IllegalDocumentModificationException) {
                 throw new FailedException("Node or parent does not exist or is root or is not a Leaf Node");
             } else if (e.getCause() instanceof NoSuchDocumentPathException) {
                 throw new FailedException("Resource id does not exist");
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 938734d..1307de3 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
@@ -70,14 +70,9 @@
         log.info("DynamicConfigService Stopped");
     }
 
-    @Override
-    public void createNode(ResourceId path, DataNode node) {
-        throw new FailedException("Not yet implemented");
-    }
-
     public void createNodeRecursive(ResourceId path, DataNode node) {
         Boolean stat = false;
-        stat = this.store.addRecursive(path, node).join();
+        stat = this.store.addNode(path, node).join();
     }
 
     public DataNode readNode(ResourceId path, Filter filter) {