blob: 203dcbb93a6daee09d15f0f5f3226061bd00dd64 [file] [log] [blame]
jingan7c5bf1f2017-02-09 02:58:09 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.restconf.utils;
18
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import org.apache.commons.io.IOUtils;
22import org.onlab.osgi.DefaultServiceDirectory;
jingan7c5bf1f2017-02-09 02:58:09 -080023import org.onosproject.restconf.api.RestconfException;
Henry Yu14af7782017-03-09 19:33:36 -050024import org.onosproject.restconf.utils.exceptions.RestconfUtilsException;
25import org.onosproject.yang.model.DataNode;
jingan7c5bf1f2017-02-09 02:58:09 -080026import org.onosproject.yang.model.ResourceData;
27import org.onosproject.yang.model.ResourceId;
Henry Yu14af7782017-03-09 19:33:36 -050028import org.onosproject.yang.runtime.CompositeData;
29import org.onosproject.yang.runtime.CompositeStream;
30import org.onosproject.yang.runtime.DefaultCompositeData;
31import org.onosproject.yang.runtime.DefaultCompositeStream;
32import org.onosproject.yang.runtime.DefaultResourceData;
33import org.onosproject.yang.runtime.DefaultRuntimeContext;
34import org.onosproject.yang.runtime.RuntimeContext;
35import org.onosproject.yang.runtime.YangRuntimeService;
36
37import java.io.IOException;
38import java.io.InputStream;
jingan7c5bf1f2017-02-09 02:58:09 -080039
40import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
41
42/**
43 * Utilities used by the RESTCONF app.
44 */
45public final class RestconfUtils {
46 /**
47 * No instantiation.
48 */
49 private RestconfUtils() {
50 }
51
52 /**
53 * Data format required by YangRuntime Service.
54 */
55 private static final String JSON_FORMAT = "json";
56
57 private static final YangRuntimeService YANG_RUNTIME =
58 DefaultServiceDirectory.getService(YangRuntimeService.class);
59
60 /**
61 * Converts an input stream to JSON objectNode.
62 *
Henry Yu14af7782017-03-09 19:33:36 -050063 * @param inputStream the InputStream from Resource Data
jingan7c5bf1f2017-02-09 02:58:09 -080064 * @return JSON representation of the data resource
65 */
66 public static ObjectNode convertInputStreamToObjectNode(InputStream inputStream) {
67 ObjectNode rootNode;
68 ObjectMapper mapper = new ObjectMapper();
69 try {
70 rootNode = (ObjectNode) mapper.readTree(inputStream);
71 } catch (IOException e) {
72 throw new RestconfUtilsException("ERROR: InputStream failed to parse");
73 }
74 return rootNode;
75 }
76
77 /**
78 * Convert ObjectNode to InputStream.
79 *
Henry Yu14af7782017-03-09 19:33:36 -050080 * @param rootNode JSON representation of the data resource
jingan7c5bf1f2017-02-09 02:58:09 -080081 * @return the InputStream from Resource Data
82 */
83 public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) {
84 String json = rootNode.asText();
85 InputStream inputStream;
86 try {
87 inputStream = IOUtils.toInputStream(json);
88 } catch (Exception e) {
89 throw new RestconfUtilsException("ERROR: Json Node failed to parse");
90 }
91 return inputStream;
92 }
93
94 /**
95 * Convert URI to ResourceId.
96 *
Henry Yu14af7782017-03-09 19:33:36 -050097 * @param uri URI of the data resource
jingan7c5bf1f2017-02-09 02:58:09 -080098 * @return resource identifier
99 */
100 public static ResourceId convertUriToRid(String uri) {
101 ResourceData resourceData = convertJsonToDataNode(uri, null);
102 return resourceData.resourceId();
103 }
104
105 /**
106 * Convert URI and ObjectNode to ResourceData.
107 *
Henry Yu14af7782017-03-09 19:33:36 -0500108 * @param uri URI of the data resource
109 * @param rootNode JSON representation of the data resource
jingan7c5bf1f2017-02-09 02:58:09 -0800110 * @return represents type of node in data store
111 */
112 public static ResourceData convertJsonToDataNode(String uri,
Henry Yu14af7782017-03-09 19:33:36 -0500113 ObjectNode rootNode) {
114 RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder();
jingan7c5bf1f2017-02-09 02:58:09 -0800115 runtimeContextBuilder.setDataFormat(JSON_FORMAT);
116 RuntimeContext context = runtimeContextBuilder.build();
117 InputStream jsonData = null;
118 if (rootNode != null) {
119 jsonData = convertObjectNodeToInputStream(rootNode);
120 }
121 CompositeStream compositeStream = new DefaultCompositeStream(uri, jsonData);
122 // CompositeStream --- YangRuntimeService ---> CompositeData.
123 CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
124 ResourceData resourceData = compositeData.resourceData();
125 return resourceData;
126 }
127
128 /**
129 * Convert Resource Id and Data Node to Json ObjectNode.
130 *
Henry Yu14af7782017-03-09 19:33:36 -0500131 * @param rid resource identifier
132 * @param dataNode represents type of node in data store
jingan7c5bf1f2017-02-09 02:58:09 -0800133 * @return JSON representation of the data resource
134 */
135 public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) {
Henry Yu14af7782017-03-09 19:33:36 -0500136 RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder();
jingan7c5bf1f2017-02-09 02:58:09 -0800137 runtimeContextBuilder.setDataFormat(JSON_FORMAT);
138 RuntimeContext context = runtimeContextBuilder.build();
139 DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
140 resourceDataBuilder.addDataNode(dataNode);
141 resourceDataBuilder.resourceId(rid);
142 ResourceData resourceData = resourceDataBuilder.build();
143 DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
144 compositeDataBuilder.resourceData(resourceData);
145 CompositeData compositeData = compositeDataBuilder.build();
146 // CompositeData --- YangRuntimeService ---> CompositeStream.
147 CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context);
148 InputStream inputStream = compositeStream.resourceData();
149 ObjectNode rootNode = convertInputStreamToObjectNode(inputStream);
150 if (rootNode == null) {
151 throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode",
152 INTERNAL_SERVER_ERROR);
153 }
154 return rootNode;
155 }
156}