[ONOS-5465][TE Tunnel SB Provider] Interaction with ONOS core TE subsystems for TE tunnel updates

Change-Id: I2cb7ed7731192228a5f6ef4c4b6c3a14a7175732
diff --git a/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/CodecTools.java b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/CodecTools.java
new file mode 100644
index 0000000..d5213ae
--- /dev/null
+++ b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/CodecTools.java
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.te.utils;
+
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.ObjectWriter;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+
+/**
+ * Convert utility methods for IETF SB.
+ */
+public final class CodecTools {
+    private static final Logger log = getLogger(CodecTools.class);
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    //no instantiation
+    private CodecTools() {
+    }
+
+    /**
+     * Returns an object node from a InputStream type input which usually comes
+     * from the HTTP response.
+     *
+     * @param stream stream data comes from a HTTP response
+     * @return object node
+     */
+    public static ObjectNode toJson(InputStream stream) {
+        ObjectNode response = null;
+        try {
+            response = (ObjectNode) MAPPER.readTree(stream);
+        } catch (IOException e) {
+            log.error("Parse json string failed {}", e.getMessage());
+        }
+
+        return response;
+    }
+
+    /**
+     * Returns an object node from a string.
+     *
+     * @param jsonString string with JSON format
+     * @return object node
+     */
+    public static ObjectNode toJson(String jsonString) {
+        ObjectNode response = null;
+        try {
+            response = (ObjectNode) MAPPER.readTree(jsonString);
+        } catch (IOException e) {
+            log.error("Parse json string failed {}", e.getMessage());
+        }
+
+        return response;
+    }
+
+    /**
+     * Returns a JSON format string from a Jackson object node.
+     *
+     * @param node JSON object node
+     * @return string with JSON format
+     */
+    public static String jsonToString(ObjectNode node) {
+        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
+        String jsonString = null;
+        try {
+            jsonString = ow.writeValueAsString(node);
+        } catch (JsonProcessingException e) {
+            log.error("Parse json to string failed {}", e.getMessage());
+        }
+
+        return jsonString;
+    }
+}
diff --git a/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/DefaultJsonCodec.java b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/DefaultJsonCodec.java
new file mode 100755
index 0000000..c8b5bae
--- /dev/null
+++ b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/DefaultJsonCodec.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.te.utils;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.yms.ych.YangCompositeEncoding;
+import org.onosproject.yms.ych.YangDataTreeCodec;
+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;
+import org.slf4j.LoggerFactory;
+
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertJsonToYdt;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertUriToYdt;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertYdtToJson;
+import static org.onosproject.provider.te.utils.CodecTools.jsonToString;
+import static org.onosproject.provider.te.utils.CodecTools.toJson;
+import static org.onosproject.yms.ych.YangResourceIdentifierType.URI;
+import static org.onosproject.yms.ydt.YdtContextOperationType.NONE;
+
+
+/**
+ * JSON/YDT Codec implementation.
+ */
+public class DefaultJsonCodec implements YangDataTreeCodec {
+    private static final String RESTCONF_ROOT = "/onos/restconf";
+    private static final String DATA = "data";
+    private static final String SLASH = "/";
+
+    private final YmsService ymsService;
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    public DefaultJsonCodec(YmsService service) {
+        ymsService = service;
+    }
+
+    @Override
+    public String encodeYdtToProtocolFormat(YdtBuilder builder) {
+        ObjectNode jsonNode = convertYdtToJson(builder.getRootNode().getName(),
+                                               builder.getRootNode(),
+                                               ymsService.getYdtWalker());
+        return jsonToString(jsonNode);
+    }
+
+    @Override
+    public YangCompositeEncoding encodeYdtToCompositeProtocolFormat(
+            YdtBuilder builder) {
+        YdtContext rootNode = builder.getRootNode();
+        String rootName = rootNode.getName();
+        YdtContext child = rootNode.getFirstChild();
+        String name = child.getName();
+        String url = rootName + SLASH + DATA + SLASH + name;
+        ObjectNode objectNode = convertYdtToJson(name, child,
+                                                 ymsService.getYdtWalker());
+        String payload = jsonToString((ObjectNode) objectNode.get(name));
+        return new YangCompositeEncodingImpl(URI, url, payload);
+    }
+
+    @Override
+    public YdtBuilder decodeProtocolDataToYdt(String protocolData,
+                                              Object schemaRegistry,
+                                              YmsOperationType opType) {
+        // Get a new builder
+        YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
+                                                      null,
+                                                      opType,
+                                                      schemaRegistry);
+
+        convertJsonToYdt(toJson(protocolData), builder);
+        return builder;
+    }
+
+    @Override
+    public YdtBuilder decodeCompositeProtocolDataToYdt(YangCompositeEncoding protocolData,
+                                                       Object schemaRegistry,
+                                                       YmsOperationType opType) {
+
+        YdtBuilder builder = ymsService.getYdtBuilder(RESTCONF_ROOT,
+                                                      null,
+                                                      opType,
+                                                      schemaRegistry);
+
+        // YdtContextOperationType should be NONE for URI in QUERY_RESPONSE.
+        convertUriToYdt(protocolData.getResourceIdentifier(), builder, NONE);
+
+        // NULL/EMPTY for Resource data
+        builder.setDefaultEditOperationType(null);
+
+        // Convert the payload json body to ydt
+        convertJsonToYdt(toJson(protocolData.getResourceInformation()), builder);
+        return builder;
+    }
+}
diff --git a/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/YangCompositeEncodingImpl.java b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/YangCompositeEncodingImpl.java
new file mode 100755
index 0000000..177c803
--- /dev/null
+++ b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/YangCompositeEncodingImpl.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.provider.te.utils;
+
+import org.onosproject.yms.ych.YangCompositeEncoding;
+import org.onosproject.yms.ych.YangResourceIdentifierType;
+
+/**
+ * Represents implementation of YangCompositeEncoding interfaces.
+ */
+public class YangCompositeEncodingImpl implements YangCompositeEncoding {
+
+    /**
+     * Resource identifier for composite encoding.
+     */
+    private String resourceIdentifier;
+
+    /**
+     * Resource information for composite encoding.
+     */
+    private String resourceInformation;
+
+    /**
+     * Resource identifier type.
+     */
+    private YangResourceIdentifierType resourceIdentifierType;
+
+    /**
+     * Creates an instance of YangCompositeEncodingImpl.
+     *
+     * @param resIdType is URI
+     * @param resId     is the URI string
+     * @param resInfo   is the JSON body string
+     */
+    public YangCompositeEncodingImpl(YangResourceIdentifierType resIdType,
+                                     String resId,
+                                     String resInfo) {
+        this.resourceIdentifierType = resIdType;
+        this.resourceIdentifier = resId;
+        this.resourceInformation = resInfo;
+    }
+
+    public String getResourceIdentifier() {
+        return resourceIdentifier;
+    }
+
+    public YangResourceIdentifierType getResourceIdentifierType() {
+        return resourceIdentifierType;
+    }
+
+    public String getResourceInformation() {
+        return resourceInformation;
+    }
+
+    public void setResourceIdentifier(String resourceId) {
+        resourceIdentifier = resourceId;
+    }
+
+    public void setResourceInformation(String resourceInfo) {
+        resourceInformation = resourceInfo;
+    }
+
+    public void setResourceIdentifierType(YangResourceIdentifierType idType) {
+        resourceIdentifierType = idType;
+    }
+}
+
diff --git a/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/package-info.java b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/package-info.java
new file mode 100644
index 0000000..74a914e
--- /dev/null
+++ b/providers/ietfte/utils/src/main/java/org/onosproject/provider/te/utils/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Public codec classes for ietf provider.
+ */
+package org.onosproject.provider.te.utils;
\ No newline at end of file