jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 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; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 23 | import org.onosproject.restconf.api.RestconfException; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 24 | import org.onosproject.restconf.utils.exceptions.RestconfUtilsException; |
| 25 | import org.onosproject.yang.model.DataNode; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 26 | import org.onosproject.yang.model.ResourceData; |
| 27 | import org.onosproject.yang.model.ResourceId; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 28 | import org.onosproject.yang.runtime.CompositeData; |
| 29 | import org.onosproject.yang.runtime.CompositeStream; |
| 30 | import org.onosproject.yang.runtime.DefaultCompositeData; |
| 31 | import org.onosproject.yang.runtime.DefaultCompositeStream; |
| 32 | import org.onosproject.yang.runtime.DefaultResourceData; |
| 33 | import org.onosproject.yang.runtime.DefaultRuntimeContext; |
| 34 | import org.onosproject.yang.runtime.RuntimeContext; |
| 35 | import org.onosproject.yang.runtime.YangRuntimeService; |
| 36 | |
| 37 | import java.io.IOException; |
| 38 | import java.io.InputStream; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 39 | |
| 40 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 41 | |
| 42 | /** |
| 43 | * Utilities used by the RESTCONF app. |
| 44 | */ |
| 45 | public final class RestconfUtils { |
| 46 | /** |
| 47 | * No instantiation. |
| 48 | */ |
| 49 | private RestconfUtils() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Data format required by YangRuntime Service. |
| 54 | */ |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 55 | private static final String JSON_FORMAT = "JSON"; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 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 Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 63 | * @param inputStream the InputStream from Resource Data |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 64 | * @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 Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 80 | * @param rootNode JSON representation of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 81 | * @return the InputStream from Resource Data |
| 82 | */ |
| 83 | public static InputStream convertObjectNodeToInputStream(ObjectNode rootNode) { |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 84 | String json = rootNode.toString(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 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 Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 97 | * @param uri URI of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 98 | * @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 Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 108 | * @param uri URI of the data resource |
| 109 | * @param rootNode JSON representation of the data resource |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 110 | * @return represents type of node in data store |
| 111 | */ |
| 112 | public static ResourceData convertJsonToDataNode(String uri, |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 113 | ObjectNode rootNode) { |
| 114 | RuntimeContext.Builder runtimeContextBuilder = new DefaultRuntimeContext.Builder(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 115 | 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 Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 131 | * @param rid resource identifier |
| 132 | * @param dataNode represents type of node in data store |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 133 | * @return JSON representation of the data resource |
| 134 | */ |
| 135 | public static ObjectNode convertDataNodeToJson(ResourceId rid, DataNode dataNode) { |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 136 | RuntimeContext.Builder runtimeContextBuilder = DefaultRuntimeContext.builder(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 137 | 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 | } |