Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -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 | */ |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 16 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 17 | package org.onosproject.restconf.restconfmanager; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.glassfish.jersey.server.ChunkedOutput; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 28 | import org.onosproject.config.DynamicConfigService; |
| 29 | import org.onosproject.config.FailedException; |
| 30 | import org.onosproject.config.Filter; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 31 | import org.onosproject.restconf.api.RestconfError; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 32 | import org.onosproject.restconf.api.RestconfException; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 33 | import org.onosproject.restconf.api.RestconfRpcOutput; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 34 | import org.onosproject.restconf.api.RestconfService; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 35 | import org.onosproject.restconf.utils.RestconfUtils; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 36 | import org.onosproject.yang.model.DataNode; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 37 | import org.onosproject.yang.model.DefaultResourceData; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 38 | import org.onosproject.yang.model.InnerNode; |
| 39 | import org.onosproject.yang.model.KeyLeaf; |
| 40 | import org.onosproject.yang.model.ListKey; |
| 41 | import org.onosproject.yang.model.NodeKey; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 42 | import org.onosproject.yang.model.ResourceData; |
| 43 | import org.onosproject.yang.model.ResourceId; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 44 | import org.onosproject.yang.model.RpcInput; |
| 45 | import org.onosproject.yang.model.RpcOutput; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 46 | import org.onosproject.yang.model.SchemaId; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 47 | import org.slf4j.Logger; |
| 48 | import org.slf4j.LoggerFactory; |
| 49 | |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 50 | import javax.ws.rs.core.Response; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 51 | import java.net.URI; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 52 | import java.util.Arrays; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 53 | import java.util.List; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 54 | import java.util.Map; |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 55 | import java.util.Optional; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 56 | import java.util.concurrent.CompletableFuture; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 57 | import java.util.concurrent.ExecutorService; |
| 58 | import java.util.concurrent.Executors; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 59 | |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 60 | import static javax.ws.rs.core.Response.Status.CONFLICT; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 61 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 62 | import static org.onosproject.d.config.ResourceIds.parentOf; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 63 | import static org.onosproject.restconf.utils.RestconfUtils.convertDataNodeToJson; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 64 | import static org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 65 | import static org.onosproject.restconf.utils.RestconfUtils.rmLastPathSegment; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 66 | import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE; |
| 67 | import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
| 68 | import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 69 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 70 | /* |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 71 | * ONOS RESTCONF application. The RESTCONF Manager |
| 72 | * implements the main logic of the RESTCONF application. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 73 | * |
| 74 | * The design of the RESTCONF subsystem contains 2 major bundles: |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 75 | * This bundle module is the back-end of the server. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 76 | * It provides the main logic of the RESTCONF server. It interacts with |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 77 | * the Dynamic Config Service and yang runtime service to run operations |
| 78 | * on the YANG data objects (i.e., resource id, yang data node). |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 79 | */ |
| 80 | |
jingan | 8a77332 | 2017-03-21 16:12:48 -0700 | [diff] [blame] | 81 | @Component(immediate = true) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 82 | @Service |
| 83 | public class RestconfManager implements RestconfService { |
| 84 | |
| 85 | private static final String RESTCONF_ROOT = "/onos/restconf"; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 86 | |
| 87 | private final int maxNumOfWorkerThreads = 5; |
| 88 | |
| 89 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 90 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 92 | protected DynamicConfigService dynamicConfigService; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 93 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 94 | private ExecutorService workerThreadPool; |
| 95 | |
| 96 | @Activate |
| 97 | protected void activate() { |
| 98 | workerThreadPool = Executors |
| 99 | .newFixedThreadPool(maxNumOfWorkerThreads, |
| 100 | new ThreadFactoryBuilder() |
| 101 | .setNameFormat("restconf-worker") |
| 102 | .build()); |
| 103 | log.info("Started"); |
| 104 | } |
| 105 | |
| 106 | @Deactivate |
| 107 | protected void deactivate() { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 108 | workerThreadPool.shutdownNow(); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 109 | log.info("Stopped"); |
| 110 | } |
| 111 | |
| 112 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 113 | public ObjectNode runGetOperationOnDataResource(URI uri) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 114 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 115 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 116 | // TODO: define Filter (if there is any requirement). |
Yuta HIGUCHI | ac85ee1 | 2017-08-03 20:07:35 -0700 | [diff] [blame] | 117 | Filter filter = Filter.builder().build(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 118 | DataNode dataNode; |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 119 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 120 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 121 | if (!dynamicConfigService.nodeExist(rl.ridForDynConfig())) { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 122 | return null; |
| 123 | } |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 124 | dataNode = dynamicConfigService.readNode(rl.ridForDynConfig(), filter); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 125 | } catch (FailedException e) { |
| 126 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 127 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 128 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 129 | Optional.of(uri.getPath())); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 130 | } |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 131 | ObjectNode rootNode = convertDataNodeToJson(rl.ridForYangRuntime(), dataNode); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 132 | return rootNode; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 136 | public void runPostOperationOnDataResource(URI uri, ObjectNode rootNode) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 137 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 138 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
| 139 | ResourceData receivedData = convertJsonToDataNode(rl.uriForYangRuntime(), rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 140 | ResourceId rid = receivedData.resourceId(); |
| 141 | List<DataNode> dataNodeList = receivedData.dataNodes(); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 142 | if (dataNodeList.size() > 1) { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 143 | log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size()); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 144 | } |
| 145 | DataNode dataNode = dataNodeList.get(0); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 146 | |
| 147 | if (rid == null) { |
| 148 | rid = ResourceId.builder().addBranchPointSchema("/", null).build(); |
| 149 | dataNode = removeTopNode(dataNode); |
| 150 | } |
| 151 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 152 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 153 | dynamicConfigService.createNode(rl.ridForDynConfig(), dataNode); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 154 | } catch (FailedException e) { |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 155 | if (e.getMessage().startsWith("Requested node already present")) { |
| 156 | throw new RestconfException("Already exists", e, |
| 157 | RestconfError.ErrorTag.DATA_EXISTS, CONFLICT, |
| 158 | Optional.of(uri.getPath())); |
| 159 | } else { |
| 160 | log.error("ERROR: DynamicConfigService: ", e); |
| 161 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 162 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 163 | Optional.of(uri.getPath())); |
| 164 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 165 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 169 | public void runPutOperationOnDataResource(URI uri, ObjectNode rootNode) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 170 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 171 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
| 172 | ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 173 | List<DataNode> dataNodeList = receivedData.dataNodes(); |
| 174 | if (dataNodeList.size() > 1) { |
| 175 | log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size()); |
| 176 | } |
| 177 | DataNode dataNode = dataNodeList.get(0); |
| 178 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 179 | try { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 180 | /* |
| 181 | * If the data node already exists, then replace it. |
| 182 | * Otherwise, create it. |
| 183 | */ |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 184 | if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) { |
| 185 | dynamicConfigService.replaceNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 186 | } else { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 187 | dynamicConfigService.createNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 188 | } |
| 189 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 190 | } catch (FailedException e) { |
| 191 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 192 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 193 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 194 | Optional.of(uri.getPath())); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 195 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 199 | public void runDeleteOperationOnDataResource(URI uri) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 200 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 201 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 202 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 203 | if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) { |
| 204 | dynamicConfigService.deleteNode(rl.ridForDynConfig()); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 205 | } |
| 206 | } catch (FailedException e) { |
| 207 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 208 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 209 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 210 | Optional.of(uri.getPath())); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void runPatchOperationOnDataResource(URI uri, ObjectNode rootNode) |
| 216 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 217 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
| 218 | ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 219 | ResourceId rid = receivedData.resourceId(); |
| 220 | List<DataNode> dataNodeList = receivedData.dataNodes(); |
| 221 | if (dataNodeList.size() > 1) { |
| 222 | log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size()); |
| 223 | } |
| 224 | DataNode dataNode = dataNodeList.get(0); |
| 225 | |
| 226 | if (rid == null) { |
| 227 | rid = ResourceId.builder().addBranchPointSchema("/", null).build(); |
| 228 | dataNode = removeTopNode(dataNode); |
| 229 | } |
| 230 | |
| 231 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 232 | dynamicConfigService.updateNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 233 | } catch (FailedException e) { |
| 234 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 235 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 236 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 237 | Optional.of(uri.getPath())); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | private DataNode removeTopNode(DataNode dataNode) { |
| 242 | if (dataNode instanceof InnerNode && dataNode.key().schemaId().name().equals("/")) { |
| 243 | Map.Entry<NodeKey, DataNode> entry = ((InnerNode) dataNode).childNodes().entrySet().iterator().next(); |
| 244 | dataNode = entry.getValue(); |
| 245 | } |
| 246 | return dataNode; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public String getRestconfRootPath() { |
| 251 | return RESTCONF_ROOT; |
| 252 | } |
| 253 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 254 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 255 | public void subscribeEventStream(String streamId, String clientIpAddr, |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 256 | ChunkedOutput<String> output) |
| 257 | throws RestconfException { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 258 | //TODO: to be completed |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 259 | throw new RestconfException("Not implemented", |
| 260 | RestconfError.ErrorTag.OPERATION_NOT_SUPPORTED, |
| 261 | Response.Status.NOT_IMPLEMENTED, |
| 262 | Optional.empty(), Optional.of("subscribeEventStream not yet implemented")); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public CompletableFuture<RestconfRpcOutput> runRpc(URI uri, |
| 267 | ObjectNode input, |
| 268 | String clientIpAddress) { |
| 269 | CompletableFuture<RestconfRpcOutput> result = |
| 270 | CompletableFuture.supplyAsync(() -> executeRpc(uri, input, clientIpAddress)); |
| 271 | return result; |
| 272 | } |
| 273 | |
| 274 | private RestconfRpcOutput executeRpc(URI uri, ObjectNode input, String clientIpAddress) { |
| 275 | ResourceData rpcInputNode = convertJsonToDataNode(uri, input); |
| 276 | ResourceId resourceId = rpcInputNode.resourceId(); |
| 277 | List<DataNode> inputDataNodeList = rpcInputNode.dataNodes(); |
| 278 | DataNode inputDataNode = inputDataNodeList.get(0); |
Gaurav Agrawal | 142ceb0 | 2018-02-16 12:19:08 +0530 | [diff] [blame] | 279 | RpcInput rpcInput = new RpcInput(resourceId, inputDataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 280 | |
| 281 | RestconfRpcOutput restconfOutput = null; |
| 282 | try { |
| 283 | CompletableFuture<RpcOutput> rpcFuture = |
Gaurav Agrawal | 142ceb0 | 2018-02-16 12:19:08 +0530 | [diff] [blame] | 284 | dynamicConfigService.invokeRpc(rpcInput); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 285 | RpcOutput rpcOutput = rpcFuture.get(); |
| 286 | restconfOutput = RestconfUtils.convertRpcOutput(resourceId, rpcOutput); |
| 287 | } catch (InterruptedException e) { |
| 288 | log.error("ERROR: computeResultQ.take() has been interrupted."); |
| 289 | log.debug("executeRpc Exception:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 290 | RestconfError error = |
| 291 | RestconfError.builder(RestconfError.ErrorType.RPC, |
| 292 | RestconfError.ErrorTag.OPERATION_FAILED) |
| 293 | .errorMessage("RPC execution has been interrupted") |
| 294 | .errorPath(uri.getPath()) |
| 295 | .build(); |
| 296 | restconfOutput = new RestconfRpcOutput(INTERNAL_SERVER_ERROR, |
| 297 | RestconfError.wrapErrorAsJson(Arrays.asList(error))); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 298 | restconfOutput.reason("RPC execution has been interrupted"); |
| 299 | } catch (Exception e) { |
| 300 | log.error("ERROR: executeRpc: {}", e.getMessage()); |
| 301 | log.debug("executeRpc Exception:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 302 | RestconfError error = |
| 303 | RestconfError.builder(RestconfError.ErrorType.RPC, |
| 304 | RestconfError.ErrorTag.OPERATION_FAILED) |
| 305 | .errorMessage(e.getMessage()) |
| 306 | .errorPath(uri.getPath()) |
| 307 | .build(); |
| 308 | restconfOutput = new RestconfRpcOutput(INTERNAL_SERVER_ERROR, |
| 309 | RestconfError.wrapErrorAsJson(Arrays.asList(error))); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 310 | restconfOutput.reason(e.getMessage()); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 313 | return restconfOutput; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 314 | } |
| 315 | |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 316 | private ResourceData getDataForStore(ResourceData resourceData) { |
| 317 | List<DataNode> nodes = resourceData.dataNodes(); |
| 318 | ResourceId rid = resourceData.resourceId(); |
| 319 | DataNode.Builder dbr = null; |
| 320 | ResourceId parentId = null; |
| 321 | try { |
| 322 | NodeKey lastKey = rid.nodeKeys().get(rid.nodeKeys().size() - 1); |
| 323 | SchemaId sid = lastKey.schemaId(); |
| 324 | if (lastKey instanceof ListKey) { |
| 325 | dbr = InnerNode.builder( |
| 326 | sid.name(), sid.namespace()).type(MULTI_INSTANCE_NODE); |
| 327 | for (KeyLeaf keyLeaf : ((ListKey) lastKey).keyLeafs()) { |
| 328 | Object val = keyLeaf.leafValue(); |
| 329 | dbr = dbr.addKeyLeaf(keyLeaf.leafSchema().name(), |
| 330 | sid.namespace(), val); |
| 331 | dbr = dbr.createChildBuilder(keyLeaf.leafSchema().name(), |
| 332 | sid.namespace(), val) |
| 333 | .type(SINGLE_INSTANCE_LEAF_VALUE_NODE); |
sonugupta-huawei | 6119ac7 | 2017-03-21 16:25:40 +0530 | [diff] [blame] | 334 | //Exit for key leaf node |
| 335 | dbr = dbr.exitNode(); |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 336 | } |
| 337 | } else { |
| 338 | dbr = InnerNode.builder( |
| 339 | sid.name(), sid.namespace()).type(SINGLE_INSTANCE_NODE); |
| 340 | } |
| 341 | if (nodes != null && !nodes.isEmpty()) { |
| 342 | // adding the parent node for given list of nodes |
| 343 | for (DataNode node : nodes) { |
| 344 | dbr = ((InnerNode.Builder) dbr).addNode(node); |
| 345 | } |
| 346 | } |
| 347 | parentId = rid.copyBuilder().removeLastKey().build(); |
| 348 | } catch (CloneNotSupportedException e) { |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 349 | log.error("getDataForStore()", e); |
| 350 | return null; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 351 | } |
| 352 | ResourceData.Builder resData = DefaultResourceData.builder(); |
| 353 | resData.addDataNode(dbr.build()); |
| 354 | resData.resourceId(parentId); |
| 355 | return resData.build(); |
| 356 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 357 | } |