[ONOS-5769] Implement encodeYdtToCompositeProtocolFormat() for JSON codec

Change-Id: Ifb68a25ae6bc3ba10e7d983d694b132a23785890
diff --git a/protocols/restconf/client/ctl/BUCK b/protocols/restconf/client/ctl/BUCK
index 0a78565..445e4b8b 100644
--- a/protocols/restconf/client/ctl/BUCK
+++ b/protocols/restconf/client/ctl/BUCK
@@ -13,6 +13,7 @@
     '//protocols/rest/api:onos-protocols-rest-api',
     '//apps/yms/api:onos-apps-yms-api',
     '//protocols/restconf/server/utils:onos-protocols-restconf-server-utils',
+    '//providers/ietfte/utils:onos-providers-ietfte-utils',
 ]
 
 osgi_jar_with_tests (
diff --git a/protocols/restconf/client/ctl/pom.xml b/protocols/restconf/client/ctl/pom.xml
index 32d2681..f4912b3 100644
--- a/protocols/restconf/client/ctl/pom.xml
+++ b/protocols/restconf/client/ctl/pom.xml
@@ -78,6 +78,12 @@
             <version>${project.version}</version>
             <type>bundle</type>
         </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-ietfte-provider-utils</artifactId>
+            <version>${project.version}</version>
+            <type>bundle</type>
+        </dependency>
   </dependencies>
 
     <build>
diff --git a/protocols/restconf/client/ctl/src/main/java/org/onosproject/protocol/restconf/ctl/JsonYdtCodec.java b/protocols/restconf/client/ctl/src/main/java/org/onosproject/protocol/restconf/ctl/JsonYdtCodec.java
index dfb7294..6fda6cd 100644
--- a/protocols/restconf/client/ctl/src/main/java/org/onosproject/protocol/restconf/ctl/JsonYdtCodec.java
+++ b/protocols/restconf/client/ctl/src/main/java/org/onosproject/protocol/restconf/ctl/JsonYdtCodec.java
@@ -19,9 +19,12 @@
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.apache.commons.io.IOUtils;
 import org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils;
+import org.onosproject.provider.te.utils.YangCompositeEncodingImpl;
 import org.onosproject.yms.ych.YangCompositeEncoding;
 import org.onosproject.yms.ych.YangDataTreeCodec;
+import org.onosproject.yms.ych.YangResourceIdentifierType;
 import org.onosproject.yms.ydt.YdtBuilder;
+import org.onosproject.yms.ydt.YdtContext;
 import org.onosproject.yms.ydt.YmsOperationType;
 import org.onosproject.yms.ymsm.YmsService;
 import org.slf4j.Logger;
@@ -30,6 +33,10 @@
 import java.io.IOException;
 import java.io.InputStream;
 
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertYdtToJson;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.findTopNodeInCompositeYdt;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getUriInCompositeYdt;
 import static org.onosproject.yms.ydt.YdtContextOperationType.NONE;
 
 
@@ -38,6 +45,7 @@
  */
 public class JsonYdtCodec implements YangDataTreeCodec {
     private static final String RESTCONF_ROOT = "restconf/data";
+    private static final String EMPTY_JSON_OBJECT = "{}";
 
     protected final YmsService ymsService;
 
@@ -48,23 +56,28 @@
     }
 
     @Override
-    public String encodeYdtToProtocolFormat(YdtBuilder ydtBuilder) {
-        String json = ParserUtils.convertYdtToJson(ydtBuilder.getRootNode().getName(),
-                                                   ydtBuilder.getRootNode(),
-                                                   ymsService.getYdtWalker())
-                .textValue();
-        return json;
+    public String encodeYdtToProtocolFormat(YdtBuilder builder) {
+        return convertYdtToJson(getJsonNameFromYdtNode(builder.getRootNode()),
+                                builder.getRootNode(),
+                                ymsService.getYdtWalker()).textValue();
     }
 
     @Override
-    public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder ydtBuilder) {
-        // Mainly for POST/PUT operation.
-        // YdtBuilder/YdtContext has YdtContextType NONE for URI,
-        // YdtContextType CREATE/MERGE/REPLACE for Resource data.
+    public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(YdtBuilder builder) {
+        String uriString = getUriInCompositeYdt(builder);
+        YdtContext topNode = findTopNodeInCompositeYdt(builder);
+        if (topNode != null) {
+            ObjectNode objectNode = convertYdtToJson(getJsonNameFromYdtNode(topNode),
+                                                     topNode,
+                                                     ymsService.getYdtWalker());
+            return new YangCompositeEncodingImpl(YangResourceIdentifierType.URI,
+                                                 uriString,
+                                                 objectNode.toString());
+        }
 
-        // TODO: Implement this method in Release Ibis for TE Tunnel.
-
-        return null;
+        return new YangCompositeEncodingImpl(YangResourceIdentifierType.URI,
+                                             uriString,
+                                             EMPTY_JSON_OBJECT);
     }
 
     @Override