[ONOS-5757]&[ONOS-5758] add Restconf GET, POST and DELETE
operates to RestconfManager.java

Change-Id: I4edf183676b7d4fdf94f1f2c7da8c88891b9785b
diff --git a/apps/restconf/utils/BUCK b/apps/restconf/utils/BUCK
new file mode 100644
index 0000000..760082d
--- /dev/null
+++ b/apps/restconf/utils/BUCK
@@ -0,0 +1,16 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:jersey-client',
+    '//lib:jersey-server',
+    '//lib:javax.ws.rs-api',
+    '//lib:easymock',
+    '//utils/rest:onlab-rest',
+    '//apps/restconf/api:onos-apps-restconf-api',
+    '//lib:onos-yang-model',
+    '//lib:onos-yang-compiler-api',
+    '//lib:onos-yang-runtime',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+)
diff --git a/apps/restconf/utils/pom.xml b/apps/restconf/utils/pom.xml
new file mode 100644
index 0000000..28c868b
--- /dev/null
+++ b/apps/restconf/utils/pom.xml
@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2017-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.
+  -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>onos-restconf-parent</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.10.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>onos-app-restconf-utils</artifactId>
+    <packaging>bundle</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-yang-runtime</artifactId>
+            <version>1.12.0-b5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-yang-model</artifactId>
+            <version>1.12.0-b5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-restconf-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-config</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-yms-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-yms</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.easymock</groupId>
+            <artifactId>easymock</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java
new file mode 100644
index 0000000..2150e1d
--- /dev/null
+++ b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/RestconfUtils.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2017-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.restconf.utils;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.commons.io.IOUtils;
+import org.onlab.osgi.DefaultServiceDirectory;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.onosproject.restconf.utils.exceptions.RestconfUtilsException;
+import org.onosproject.restconf.api.RestconfException;
+import org.onosproject.yang.runtime.CompositeStream;
+import org.onosproject.yang.runtime.CompositeData;
+import org.onosproject.yang.runtime.YangRuntimeService;
+import org.onosproject.yang.runtime.DefaultCompositeStream;
+import org.onosproject.yang.runtime.DefaultCompositeData;
+import org.onosproject.yang.runtime.DefaultResourceData;
+import org.onosproject.yang.runtime.RuntimeContext;
+import org.onosproject.yang.model.ResourceData;
+import org.onosproject.yang.model.ResourceId;
+import org.onosproject.yang.model.DataNode;
+
+import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
+
+/**
+ * Utilities used by the RESTCONF app.
+ */
+public final class RestconfUtils {
+    /**
+     * No instantiation.
+     */
+    private RestconfUtils() {
+    }
+
+    /**
+     * Data format required by YangRuntime Service.
+     */
+    private static final String JSON_FORMAT = "json";
+
+    private static final YangRuntimeService YANG_RUNTIME =
+            DefaultServiceDirectory.getService(YangRuntimeService.class);
+
+    /**
+     * Converts an input stream to JSON objectNode.
+     *
+     * @param  inputStream the InputStream from Resource Data
+     * @return JSON representation of the data resource
+     */
+    public static ObjectNode convertInputStreamToObjectNode(InputStream inputStream) {
+        ObjectNode rootNode;
+        ObjectMapper mapper = new ObjectMapper();
+        try {
+            rootNode = (ObjectNode) mapper.readTree(inputStream);
+        } catch (IOException e) {
+            throw new RestconfUtilsException("ERROR: InputStream failed to parse");
+        }
+        return rootNode;
+    }
+
+    /**
+     * Convert ObjectNode to InputStream.
+     *
+     * @param  rootNode JSON representation of the data resource
+     * @return the InputStream from Resource Data
+     */
+    public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) {
+        String json = rootNode.asText();
+        InputStream inputStream;
+        try {
+            inputStream = IOUtils.toInputStream(json);
+        } catch (Exception e) {
+            throw new RestconfUtilsException("ERROR: Json Node failed to parse");
+        }
+        return inputStream;
+    }
+
+    /**
+     * Convert URI to ResourceId.
+     *
+     * @param  uri URI of the data resource
+     * @return resource identifier
+     */
+    public static ResourceId convertUriToRid(String uri) {
+        ResourceData resourceData = convertJsonToDataNode(uri, null);
+        return resourceData.resourceId();
+    }
+
+    /**
+     * Convert URI and ObjectNode to ResourceData.
+     *
+     * @param  uri URI of the data resource
+     * @param  rootNode JSON representation of the data resource
+     * @return represents type of node in data store
+     */
+    public static ResourceData convertJsonToDataNode(String uri,
+                                                    ObjectNode rootNode) {
+        RuntimeContext.Builder runtimeContextBuilder = null;
+        runtimeContextBuilder.setDataFormat(JSON_FORMAT);
+        RuntimeContext context = runtimeContextBuilder.build();
+        InputStream jsonData = null;
+        if (rootNode != null) {
+            jsonData = convertObjectNodeToInputStream(rootNode);
+        }
+        CompositeStream compositeStream = new DefaultCompositeStream(uri, jsonData);
+        // CompositeStream --- YangRuntimeService ---> CompositeData.
+        CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
+        ResourceData resourceData = compositeData.resourceData();
+        return resourceData;
+    }
+
+    /**
+     * Convert Resource Id and Data Node to Json ObjectNode.
+     *
+     * @param  rid resource identifier
+     * @param  dataNode represents type of node in data store
+     * @return JSON representation of the data resource
+     */
+    public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) {
+        RuntimeContext.Builder runtimeContextBuilder = null;
+        runtimeContextBuilder.setDataFormat(JSON_FORMAT);
+        RuntimeContext context = runtimeContextBuilder.build();
+        DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
+        resourceDataBuilder.addDataNode(dataNode);
+        resourceDataBuilder.resourceId(rid);
+        ResourceData resourceData = resourceDataBuilder.build();
+        DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
+        compositeDataBuilder.resourceData(resourceData);
+        CompositeData compositeData = compositeDataBuilder.build();
+        // CompositeData --- YangRuntimeService ---> CompositeStream.
+        CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context);
+        InputStream inputStream = compositeStream.resourceData();
+        ObjectNode rootNode = convertInputStreamToObjectNode(inputStream);
+        if (rootNode == null) {
+            throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode",
+                                        INTERNAL_SERVER_ERROR);
+        }
+        return rootNode;
+    }
+}
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java
new file mode 100644
index 0000000..c8d0791
--- /dev/null
+++ b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/RestconfUtilsException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2017-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.restconf.utils.exceptions;
+
+/**
+ * Represents class of errors related to RESTCONF utilities.
+ */
+public class RestconfUtilsException extends RuntimeException {
+
+    /**
+     * Constructs an exception with the specified message.
+     *
+     * @param message the message describing the specific nature of the error
+     */
+    public RestconfUtilsException(String message) {
+        super(message);
+    }
+
+    /**
+     * Constructs an exception with the specified message and the underlying
+     * cause.
+     *
+     * @param message the message describing the specific nature of the error
+     * @param cause   the underlying cause of this error
+     */
+    public RestconfUtilsException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java
new file mode 100644
index 0000000..4ae7122
--- /dev/null
+++ b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/exceptions/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * Parse utils custom exceptions.
+ */
+package org.onosproject.restconf.utils.exceptions;
\ No newline at end of file
diff --git a/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/package-info.java b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/package-info.java
new file mode 100644
index 0000000..a3149ab
--- /dev/null
+++ b/apps/restconf/utils/src/main/java/org/onosproject/restconf/utils/package-info.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2017-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.
+ */
+
+/**
+ * Parser Utilities for:
+ * 1. input stream and object node conversion.
+ * 2. uri and resource id conversion.
+ * 3. object node and data node conversion.
+ */
+package org.onosproject.restconf.utils;
\ No newline at end of file