jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 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 | |
| 17 | package org.onosproject.restconf.utils; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.apache.commons.io.IOUtils; |
| 22 | import org.onlab.osgi.DefaultServiceDirectory; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 23 | import org.onosproject.restconf.api.RestconfError; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 24 | import org.onosproject.restconf.api.RestconfException; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 25 | import org.onosproject.restconf.api.RestconfRpcOutput; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 26 | import org.onosproject.yang.model.DataNode; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 27 | import org.onosproject.yang.model.DefaultResourceData; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 28 | import org.onosproject.yang.model.ResourceData; |
| 29 | import org.onosproject.yang.model.ResourceId; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 30 | import org.onosproject.yang.model.RpcOutput; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 31 | import org.onosproject.yang.runtime.CompositeData; |
| 32 | import org.onosproject.yang.runtime.CompositeStream; |
| 33 | import org.onosproject.yang.runtime.DefaultCompositeData; |
| 34 | import org.onosproject.yang.runtime.DefaultCompositeStream; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 35 | import org.onosproject.yang.runtime.DefaultRuntimeContext; |
| 36 | import org.onosproject.yang.runtime.RuntimeContext; |
| 37 | import org.onosproject.yang.runtime.YangRuntimeService; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 38 | import org.slf4j.Logger; |
| 39 | import org.slf4j.LoggerFactory; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 40 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 41 | import javax.ws.rs.core.Response; |
| 42 | import javax.ws.rs.core.UriBuilder; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 43 | import java.io.IOException; |
| 44 | import java.io.InputStream; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 45 | import java.net.URI; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 46 | import java.util.Optional; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 47 | |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 48 | import static javax.ws.rs.core.Response.Status.*; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Utilities used by the RESTCONF app. |
| 52 | */ |
| 53 | public final class RestconfUtils { |
| 54 | /** |
| 55 | * No instantiation. |
| 56 | */ |
| 57 | private RestconfUtils() { |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Data format required by YangRuntime Service. |
| 62 | */ |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 63 | private static final String JSON_FORMAT = "JSON"; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 64 | private static final String SLASH = "/"; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 65 | |
| 66 | private static final YangRuntimeService YANG_RUNTIME = |
| 67 | DefaultServiceDirectory.getService(YangRuntimeService.class); |
| 68 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 69 | private static final Logger log = LoggerFactory.getLogger(RestconfUtils.class); |
| 70 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 71 | /** |
| 72 | * Converts an input stream to JSON objectNode. |
| 73 | * |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 74 | * @param inputStream the InputStream from Resource Data |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 75 | * @return JSON representation of the data resource |
| 76 | */ |
| 77 | public static ObjectNode convertInputStreamToObjectNode(InputStream inputStream) { |
| 78 | ObjectNode rootNode; |
| 79 | ObjectMapper mapper = new ObjectMapper(); |
| 80 | try { |
| 81 | rootNode = (ObjectNode) mapper.readTree(inputStream); |
| 82 | } catch (IOException e) { |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 83 | throw new RestconfException("ERROR: InputStream failed to parse", |
| 84 | e, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 85 | Optional.empty()); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 86 | } |
| 87 | return rootNode; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Convert ObjectNode to InputStream. |
| 92 | * |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 93 | * @param rootNode JSON representation of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 94 | * @return the InputStream from Resource Data |
| 95 | */ |
| 96 | public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) { |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 97 | String json = rootNode.toString(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 98 | InputStream inputStream; |
| 99 | try { |
| 100 | inputStream = IOUtils.toInputStream(json); |
| 101 | } catch (Exception e) { |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 102 | throw new RestconfException("ERROR: Json Node failed to parse", e, |
| 103 | RestconfError.ErrorTag.MALFORMED_MESSAGE, BAD_REQUEST, |
| 104 | Optional.empty()); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 105 | } |
| 106 | return inputStream; |
| 107 | } |
| 108 | |
| 109 | /** |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 110 | * Convert URI to ResourceId. If the URI represents the datastore resource |
| 111 | * (i.e., the root of datastore), a null is returned. |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 112 | * |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 113 | * @param uri URI of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 114 | * @return resource identifier |
| 115 | */ |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 116 | public static ResourceId convertUriToRid(URI uri) { |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 117 | ResourceData resourceData = convertJsonToDataNode(uri, null); |
| 118 | return resourceData.resourceId(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Convert URI and ObjectNode to ResourceData. |
| 123 | * |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 124 | * @param uri URI of the data resource |
| 125 | * @param rootNode JSON representation of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 126 | * @return represents type of node in data store |
| 127 | */ |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 128 | public static ResourceData convertJsonToDataNode(URI uri, |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 129 | ObjectNode rootNode) { |
| 130 | RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 131 | runtimeContextBuilder.setDataFormat(JSON_FORMAT); |
| 132 | RuntimeContext context = runtimeContextBuilder.build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 133 | ResourceData resourceData = null; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 134 | InputStream jsonData = null; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 135 | try { |
| 136 | if (rootNode != null) { |
| 137 | jsonData = convertObjectNodeToInputStream(rootNode); |
| 138 | } |
| 139 | String uriString = getRawUriPath(uri); |
| 140 | |
| 141 | CompositeStream compositeStream = new DefaultCompositeStream(uriString, jsonData); |
| 142 | // CompositeStream --- YangRuntimeService ---> CompositeData. |
| 143 | CompositeData compositeData = YANG_RUNTIME.decode(compositeStream, context); |
| 144 | resourceData = compositeData.resourceData(); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 145 | } catch (RestconfException ex) { |
| 146 | throw ex; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 147 | } catch (Exception ex) { |
Yuta HIGUCHI | 6f73bc6 | 2018-03-01 17:09:13 -0800 | [diff] [blame] | 148 | log.error("convertJsonToDataNode failure: {}", ex.getMessage(), ex); |
| 149 | log.info("Failed JSON: \n{}", rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 150 | log.debug("convertJsonToDataNode failure", ex); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 151 | throw new RestconfException("ERROR: JSON cannot be converted to DataNode", |
| 152 | ex, RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 153 | Optional.of(uri.getPath())); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 154 | } |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 155 | if (resourceData == null) { |
| 156 | throw new RestconfException("ERROR: JSON cannot be converted to DataNode", |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 157 | RestconfError.ErrorTag.DATA_MISSING, CONFLICT, |
| 158 | Optional.of(uri.getPath()), Optional.empty()); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 159 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 160 | return resourceData; |
| 161 | } |
| 162 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 163 | private static String getRawUriPath(URI uri) { |
| 164 | String path = uri.getRawPath(); |
sonugupta-huawei | 8065a3b | 2017-09-08 16:50:17 +0530 | [diff] [blame] | 165 | if (path.equals("/onos/restconf/data")) { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 166 | return null; |
| 167 | } |
| 168 | |
sonugupta-huawei | 8065a3b | 2017-09-08 16:50:17 +0530 | [diff] [blame] | 169 | return path.replaceAll("^/onos/restconf/data/", "") |
| 170 | .replaceAll("^/onos/restconf/operations/", ""); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 171 | } |
| 172 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 173 | /** |
| 174 | * Convert Resource Id and Data Node to Json ObjectNode. |
| 175 | * |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 176 | * @param rid resource identifier |
| 177 | * @param dataNode represents type of node in data store |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 178 | * @return JSON representation of the data resource |
| 179 | */ |
| 180 | public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) { |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 181 | RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 182 | runtimeContextBuilder.setDataFormat(JSON_FORMAT); |
| 183 | RuntimeContext context = runtimeContextBuilder.build(); |
| 184 | DefaultResourceData.Builder resourceDataBuilder = DefaultResourceData.builder(); |
| 185 | resourceDataBuilder.addDataNode(dataNode); |
| 186 | resourceDataBuilder.resourceId(rid); |
| 187 | ResourceData resourceData = resourceDataBuilder.build(); |
| 188 | DefaultCompositeData.Builder compositeDataBuilder = DefaultCompositeData.builder(); |
| 189 | compositeDataBuilder.resourceData(resourceData); |
| 190 | CompositeData compositeData = compositeDataBuilder.build(); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 191 | ObjectNode rootNode = null; |
| 192 | try { |
| 193 | // CompositeData --- YangRuntimeService ---> CompositeStream. |
| 194 | CompositeStream compositeStream = YANG_RUNTIME.encode(compositeData, context); |
| 195 | InputStream inputStream = compositeStream.resourceData(); |
| 196 | rootNode = convertInputStreamToObjectNode(inputStream); |
| 197 | } catch (Exception ex) { |
| 198 | log.error("convertInputStreamToObjectNode failure: {}", ex.getMessage()); |
| 199 | log.debug("convertInputStreamToObjectNode failure", ex); |
| 200 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 201 | if (rootNode == null) { |
| 202 | throw new RestconfException("ERROR: InputStream can not be convert to ObjectNode", |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 203 | null, RestconfError.ErrorTag.DATA_MISSING, CONFLICT, |
| 204 | Optional.empty()); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 205 | } |
| 206 | return rootNode; |
| 207 | } |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Removes the last path segment from the given URI. That is, returns |
| 211 | * the parent of the given URI. |
| 212 | * |
| 213 | * @param uri given URI |
| 214 | * @return parent URI |
| 215 | */ |
| 216 | public static URI rmLastPathSegment(URI uri) { |
| 217 | if (uri == null) { |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | UriBuilder builder = UriBuilder.fromUri(uri); |
| 222 | String newPath = rmLastPathSegmentStr(uri.getRawPath()); |
| 223 | builder.replacePath(newPath); |
| 224 | |
| 225 | return builder.build(); |
| 226 | } |
| 227 | |
| 228 | private static String rmLastPathSegmentStr(String rawPath) { |
| 229 | if (rawPath == null) { |
| 230 | return null; |
| 231 | } |
| 232 | int pos = rawPath.lastIndexOf(SLASH); |
| 233 | if (pos <= 0) { |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | return rawPath.substring(0, pos); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Creates a RESTCONF RPC output object from a given YANG RPC output object. |
| 242 | * |
| 243 | * @param cmdId resource ID of the RPC |
| 244 | * @param rpcOutput given RPC output in YANG format |
| 245 | * @return RPC output in RESTCONF format |
| 246 | */ |
| 247 | public static RestconfRpcOutput convertRpcOutput(ResourceId cmdId, RpcOutput rpcOutput) { |
| 248 | RestconfRpcOutput restconfRpcOutput = new RestconfRpcOutput(); |
| 249 | |
| 250 | restconfRpcOutput.status(convertResponseStatus(rpcOutput.status())); |
| 251 | if (rpcOutput.data() != null) { |
| 252 | restconfRpcOutput.output(convertDataNodeToJson(cmdId, rpcOutput.data())); |
| 253 | } |
| 254 | |
| 255 | return restconfRpcOutput; |
| 256 | } |
| 257 | |
| 258 | private static Response.Status convertResponseStatus(RpcOutput.Status status) { |
| 259 | switch (status) { |
| 260 | case RPC_SUCCESS: |
| 261 | return OK; |
| 262 | case RPC_FAILURE: |
| 263 | return EXPECTATION_FAILED; |
| 264 | case RPC_NODATA: |
| 265 | return NO_CONTENT; |
| 266 | case RPC_TIMEOUT: |
| 267 | return REQUEST_TIMEOUT; |
| 268 | default: |
| 269 | return BAD_REQUEST; |
| 270 | } |
| 271 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 272 | } |