blob: 2150e1d2df6f80c528f7becb3ce64834bea2dd9a [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;
23import java.io.IOException;
24import java.io.InputStream;
25
26import org.onosproject.restconf.utils.exceptions.RestconfUtilsException;
27import org.onosproject.restconf.api.RestconfException;
28import org.onosproject.yang.runtime.CompositeStream;
29import org.onosproject.yang.runtime.CompositeData;
30import org.onosproject.yang.runtime.YangRuntimeService;
31import org.onosproject.yang.runtime.DefaultCompositeStream;
32import org.onosproject.yang.runtime.DefaultCompositeData;
33import org.onosproject.yang.runtime.DefaultResourceData;
34import org.onosproject.yang.runtime.RuntimeContext;
35import org.onosproject.yang.model.ResourceData;
36import org.onosproject.yang.model.ResourceId;
37import org.onosproject.yang.model.DataNode;
38
39import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
40
41/**
42 * Utilities used by the RESTCONF app.
43 */
44public final class RestconfUtils {
45 /**
46 * No instantiation.
47 */
48 private RestconfUtils() {
49 }
50
51 /**
52 * Data format required by YangRuntime Service.
53 */
54 private static final String JSON_FORMAT = "json";
55
56 private static final YangRuntimeService YANG_RUNTIME =
57 DefaultServiceDirectory.getService(YangRuntimeService.class);
58
59 /**
60 * Converts an input stream to JSON objectNode.
61 *
62 * @param inputStream the InputStream from Resource Data
63 * @return JSON representation of the data resource
64 */
65 public static ObjectNode convertInputStreamToObjectNode(InputStream inputStream) {
66 ObjectNode rootNode;
67 ObjectMapper mapper = new ObjectMapper();
68 try {
69 rootNode = (ObjectNode) mapper.readTree(inputStream);
70 } catch (IOException e) {
71 throw new RestconfUtilsException("ERROR: InputStream failed to parse");
72 }
73 return rootNode;
74 }
75
76 /**
77 * Convert ObjectNode to InputStream.
78 *
79 * @param rootNode JSON representation of the data resource
80 * @return the InputStream from Resource Data
81 */
82 public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) {
83 String json = rootNode.asText();
84 InputStream inputStream;
85 try {
86 inputStream = IOUtils.toInputStream(json);
87 } catch (Exception e) {
88 throw new RestconfUtilsException("ERROR: Json Node failed to parse");
89 }
90 return inputStream;
91 }
92
93 /**
94 * Convert URI to ResourceId.
95 *
96 * @param uri URI of the data resource
97 * @return resource identifier
98 */
99 public static ResourceId convertUriToRid(String uri) {
100 ResourceData resourceData = convertJsonToDataNode(uri, null);
101 return resourceData.resourceId();
102 }
103
104 /**
105 * Convert URI and ObjectNode to ResourceData.
106 *
107 * @param uri URI of the data resource
108 * @param rootNode JSON representation of the data resource
109 * @return represents type of node in data store
110 */
111 public static ResourceData convertJsonToDataNode(String uri,
112 ObjectNode rootNode) {
113 RuntimeContext.Builder runtimeContextBuilder = null;
114 runtimeContextBuilder.setDataFormat(JSON_FORMAT);
115 RuntimeContext context = runtimeContextBuilder.build();
116 InputStream jsonData = null;
117 if (rootNode != null) {
118 jsonData = convertObjectNodeToInputStream(rootNode);
119 }
120 CompositeStream compositeStream = new DefaultCompositeStream(uri, jsonData);
121 // CompositeStream --- YangRuntimeService ---> CompositeData.
122 CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context);
123 ResourceData resourceData = compositeData.resourceData();
124 return resourceData;
125 }
126
127 /**
128 * Convert Resource Id and Data Node to Json ObjectNode.
129 *
130 * @param rid resource identifier
131 * @param dataNode represents type of node in data store
132 * @return JSON representation of the data resource
133 */
134 public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) {
135 RuntimeContext.Builder runtimeContextBuilder = null;
136 runtimeContextBuilder.setDataFormat(JSON_FORMAT);
137 RuntimeContext context = runtimeContextBuilder.build();
138 DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder();
139 resourceDataBuilder.addDataNode(dataNode);
140 resourceDataBuilder.resourceId(rid);
141 ResourceData resourceData = resourceDataBuilder.build();
142 DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder();
143 compositeDataBuilder.resourceData(resourceData);
144 CompositeData compositeData = compositeDataBuilder.build();
145 // CompositeData --- YangRuntimeService ---> CompositeStream.
146 CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context);
147 InputStream inputStream = compositeStream.resourceData();
148 ObjectNode rootNode = convertInputStreamToObjectNode(inputStream);
149 if (rootNode == null) {
150 throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode",
151 INTERNAL_SERVER_ERROR);
152 }
153 return rootNode;
154 }
155}