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