[ONOS-6147] POST operation support utility

Change-Id: Id3bdebfc63c8b9066c3e16d2e55e220578f343c8
diff --git a/apps/restconf/restconfmgr/BUCK b/apps/restconf/restconfmgr/BUCK
index 7bb7871..9be0fdc 100644
--- a/apps/restconf/restconfmgr/BUCK
+++ b/apps/restconf/restconfmgr/BUCK
@@ -9,6 +9,7 @@
     '//apps/restconf/api:onos-apps-restconf-api',
     '//apps/restconf/utils:onos-apps-restconf-utils',
     '//apps/config:onos-apps-config',
+    '//lib:onos-yang-runtime',
 ]
 
 osgi_jar_with_tests (
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 c0fdc31..349c0f1 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
@@ -31,8 +31,14 @@
 import org.onosproject.restconf.api.RestconfException;
 import org.onosproject.restconf.api.RestconfService;
 import org.onosproject.yang.model.DataNode;
+import org.onosproject.yang.model.InnerNode;
+import org.onosproject.yang.model.KeyLeaf;
+import org.onosproject.yang.model.ListKey;
+import org.onosproject.yang.model.NodeKey;
 import org.onosproject.yang.model.ResourceData;
 import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.SchemaId;
+import org.onosproject.yang.runtime.DefaultResourceData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,6 +57,9 @@
 import static org.onosproject.restconf.utils.RestconfUtils.convertDataNodeToJson;
 import static org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode;
 import static org.onosproject.restconf.utils.RestconfUtils.convertUriToRid;
+import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE;
+import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE;
 
 /*
  * ONOS RESTCONF application. The RESTCONF Manager
@@ -129,7 +138,8 @@
     @Override
     public void runPostOperationOnDataResource(String uri, ObjectNode rootNode)
             throws RestconfException {
-        ResourceData resourceData = convertJsonToDataNode(uri, rootNode);
+        ResourceData receivedData = convertJsonToDataNode(uri, rootNode);
+        ResourceData resourceData = getDataForStore(receivedData);
         ResourceId rid = resourceData.resourceId();
         List<DataNode> dataNodeList = resourceData.dataNodes();
         // TODO: Error message needs to be fixed
@@ -198,13 +208,51 @@
             throw new RestconfException("Server ERROR: workerThreadPool NOT " +
                                                 "instanceof ThreadPoolExecutor",
                                         INTERNAL_SERVER_ERROR);
-
         }
 
         BlockingQueue<ObjectNode> eventQueue = new LinkedBlockingQueue<>();
         workerThreadPool.submit(new EventConsumer(output, eventQueue));
     }
 
+    private ResourceData getDataForStore(ResourceData resourceData) {
+        List<DataNode> nodes = resourceData.dataNodes();
+        ResourceId rid = resourceData.resourceId();
+        DataNode.Builder dbr = null;
+        ResourceId parentId = null;
+        try {
+            NodeKey lastKey = rid.nodeKeys().get(rid.nodeKeys().size() - 1);
+            SchemaId sid = lastKey.schemaId();
+            if (lastKey instanceof ListKey) {
+                dbr = InnerNode.builder(
+                        sid.name(), sid.namespace()).type(MULTI_INSTANCE_NODE);
+                for (KeyLeaf keyLeaf : ((ListKey) lastKey).keyLeafs()) {
+                    Object val = keyLeaf.leafValue();
+                    dbr = dbr.addKeyLeaf(keyLeaf.leafSchema().name(),
+                                         sid.namespace(), val);
+                    dbr = dbr.createChildBuilder(keyLeaf.leafSchema().name(),
+                                                 sid.namespace(), val)
+                            .type(SINGLE_INSTANCE_LEAF_VALUE_NODE);
+                }
+            } else {
+                dbr = InnerNode.builder(
+                        sid.name(), sid.namespace()).type(SINGLE_INSTANCE_NODE);
+            }
+            if (nodes != null && !nodes.isEmpty()) {
+                // adding the parent node for given list of nodes
+                for (DataNode node : nodes) {
+                    dbr = ((InnerNode.Builder) dbr).addNode(node);
+                }
+            }
+            parentId = rid.copyBuilder().removeLastKey().build();
+        } catch (CloneNotSupportedException e) {
+            e.printStackTrace();
+        }
+        ResourceData.Builder resData = DefaultResourceData.builder();
+        resData.addDataNode(dbr.build());
+        resData.resourceId(parentId);
+        return resData.build();
+    }
+
     /**
      * Shutdown a pool cleanly if possible.
      *