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(); |
柯志勇10068695 | 6f7ba6c | 2018-10-22 10:43:04 +0800 | [diff] [blame] | 143 | if (dataNodeList == null || dataNodeList.isEmpty()) { |
| 144 | log.warn("There is no one Data Node can be proceed."); |
| 145 | return; |
| 146 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 147 | if (dataNodeList.size() > 1) { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 148 | 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] | 149 | } |
| 150 | DataNode dataNode = dataNodeList.get(0); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 151 | |
| 152 | if (rid == null) { |
| 153 | rid = ResourceId.builder().addBranchPointSchema("/", null).build(); |
| 154 | dataNode = removeTopNode(dataNode); |
| 155 | } |
| 156 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 157 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 158 | dynamicConfigService.createNode(rl.ridForDynConfig(), dataNode); |
Yuta HIGUCHI | fa105b2 | 2018-03-01 21:16:51 -0800 | [diff] [blame] | 159 | } catch (Exception e) { |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 160 | if (e.getMessage().startsWith("Requested node already present")) { |
| 161 | throw new RestconfException("Already exists", e, |
| 162 | RestconfError.ErrorTag.DATA_EXISTS, CONFLICT, |
| 163 | Optional.of(uri.getPath())); |
| 164 | } else { |
Yuta HIGUCHI | fa105b2 | 2018-03-01 21:16:51 -0800 | [diff] [blame] | 165 | log.error("ERROR: DynamicConfigService: creating {} with {}", |
| 166 | ResourceIds.toInstanceIdentifier(rl.ridForDynConfig()), |
| 167 | dataNode, |
| 168 | e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 169 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 170 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 171 | Optional.of(uri.getPath())); |
| 172 | } |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 173 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 177 | public void runPutOperationOnDataResource(URI uri, ObjectNode rootNode) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 178 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 179 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
| 180 | ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 181 | List<DataNode> dataNodeList = receivedData.dataNodes(); |
柯志勇10068695 | 6f7ba6c | 2018-10-22 10:43:04 +0800 | [diff] [blame] | 182 | if (dataNodeList == null || dataNodeList.isEmpty()) { |
| 183 | log.warn("There is no one Data Node can be proceed."); |
| 184 | return; |
| 185 | } |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 186 | if (dataNodeList.size() > 1) { |
| 187 | log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size()); |
| 188 | } |
| 189 | DataNode dataNode = dataNodeList.get(0); |
| 190 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 191 | try { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 192 | /* |
| 193 | * If the data node already exists, then replace it. |
| 194 | * Otherwise, create it. |
| 195 | */ |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 196 | if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) { |
| 197 | dynamicConfigService.replaceNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 198 | } else { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 199 | dynamicConfigService.createNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 200 | } |
| 201 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 202 | } catch (FailedException e) { |
| 203 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 204 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 205 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 206 | Optional.of(uri.getPath())); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 207 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 211 | public void runDeleteOperationOnDataResource(URI uri) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 212 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 213 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 214 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 215 | if (dynamicConfigService.nodeExist(rl.ridForDynConfig())) { |
| 216 | dynamicConfigService.deleteNode(rl.ridForDynConfig()); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 217 | } |
| 218 | } catch (FailedException e) { |
| 219 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 220 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 221 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 222 | Optional.of(uri.getPath())); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public void runPatchOperationOnDataResource(URI uri, ObjectNode rootNode) |
| 228 | throws RestconfException { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 229 | DataResourceLocator rl = DataResourceLocator.newInstance(uri); |
| 230 | ResourceData receivedData = convertJsonToDataNode(rmLastPathSegment(rl.uriForYangRuntime()), rootNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 231 | ResourceId rid = receivedData.resourceId(); |
| 232 | List<DataNode> dataNodeList = receivedData.dataNodes(); |
柯志勇10068695 | d34aaf2 | 2018-10-23 17:18:19 +0800 | [diff] [blame] | 233 | if (dataNodeList == null || dataNodeList.isEmpty()) { |
| 234 | log.warn("There is no one Data Node can be proceed."); |
| 235 | return; |
| 236 | } |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 237 | if (dataNodeList.size() > 1) { |
| 238 | log.warn("There are more than one Data Node can be proceed: {}", dataNodeList.size()); |
| 239 | } |
| 240 | DataNode dataNode = dataNodeList.get(0); |
| 241 | |
| 242 | if (rid == null) { |
| 243 | rid = ResourceId.builder().addBranchPointSchema("/", null).build(); |
| 244 | dataNode = removeTopNode(dataNode); |
| 245 | } |
| 246 | |
| 247 | try { |
Henry Yu | 830b5dc | 2017-11-16 10:44:45 -0500 | [diff] [blame] | 248 | dynamicConfigService.updateNode(parentOf(rl.ridForDynConfig()), dataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 249 | } catch (FailedException e) { |
| 250 | log.error("ERROR: DynamicConfigService: ", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 251 | throw new RestconfException("ERROR: DynamicConfigService", e, |
| 252 | RestconfError.ErrorTag.OPERATION_FAILED, INTERNAL_SERVER_ERROR, |
| 253 | Optional.of(uri.getPath())); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | private DataNode removeTopNode(DataNode dataNode) { |
| 258 | if (dataNode instanceof InnerNode && dataNode.key().schemaId().name().equals("/")) { |
| 259 | Map.Entry<NodeKey, DataNode> entry = ((InnerNode) dataNode).childNodes().entrySet().iterator().next(); |
| 260 | dataNode = entry.getValue(); |
| 261 | } |
| 262 | return dataNode; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public String getRestconfRootPath() { |
| 267 | return RESTCONF_ROOT; |
| 268 | } |
| 269 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 270 | @Override |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 271 | public void subscribeEventStream(String streamId, String clientIpAddr, |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 272 | ChunkedOutput<String> output) |
| 273 | throws RestconfException { |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 274 | //TODO: to be completed |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 275 | throw new RestconfException("Not implemented", |
| 276 | RestconfError.ErrorTag.OPERATION_NOT_SUPPORTED, |
| 277 | Response.Status.NOT_IMPLEMENTED, |
| 278 | Optional.empty(), Optional.of("subscribeEventStream not yet implemented")); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public CompletableFuture<RestconfRpcOutput> runRpc(URI uri, |
| 283 | ObjectNode input, |
| 284 | String clientIpAddress) { |
| 285 | CompletableFuture<RestconfRpcOutput> result = |
| 286 | CompletableFuture.supplyAsync(() -> executeRpc(uri, input, clientIpAddress)); |
| 287 | return result; |
| 288 | } |
| 289 | |
| 290 | private RestconfRpcOutput executeRpc(URI uri, ObjectNode input, String clientIpAddress) { |
| 291 | ResourceData rpcInputNode = convertJsonToDataNode(uri, input); |
| 292 | ResourceId resourceId = rpcInputNode.resourceId(); |
| 293 | List<DataNode> inputDataNodeList = rpcInputNode.dataNodes(); |
| 294 | DataNode inputDataNode = inputDataNodeList.get(0); |
Gaurav Agrawal | 142ceb0 | 2018-02-16 12:19:08 +0530 | [diff] [blame] | 295 | RpcInput rpcInput = new RpcInput(resourceId, inputDataNode); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 296 | |
| 297 | RestconfRpcOutput restconfOutput = null; |
| 298 | try { |
| 299 | CompletableFuture<RpcOutput> rpcFuture = |
Gaurav Agrawal | 142ceb0 | 2018-02-16 12:19:08 +0530 | [diff] [blame] | 300 | dynamicConfigService.invokeRpc(rpcInput); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 301 | RpcOutput rpcOutput = rpcFuture.get(); |
| 302 | restconfOutput = RestconfUtils.convertRpcOutput(resourceId, rpcOutput); |
| 303 | } catch (InterruptedException e) { |
| 304 | log.error("ERROR: computeResultQ.take() has been interrupted."); |
| 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("RPC execution has been interrupted") |
| 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("RPC execution has been interrupted"); |
| 315 | } catch (Exception e) { |
| 316 | log.error("ERROR: executeRpc: {}", e.getMessage()); |
| 317 | log.debug("executeRpc Exception:", e); |
Sean Condon | 13b1681 | 2018-01-25 10:31:49 +0000 | [diff] [blame] | 318 | RestconfError error = |
| 319 | RestconfError.builder(RestconfError.ErrorType.RPC, |
| 320 | RestconfError.ErrorTag.OPERATION_FAILED) |
| 321 | .errorMessage(e.getMessage()) |
| 322 | .errorPath(uri.getPath()) |
| 323 | .build(); |
| 324 | restconfOutput = new RestconfRpcOutput(INTERNAL_SERVER_ERROR, |
| 325 | RestconfError.wrapErrorAsJson(Arrays.asList(error))); |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 326 | restconfOutput.reason(e.getMessage()); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Henry Yu | c10f7fc | 2017-07-26 13:42:08 -0400 | [diff] [blame] | 329 | return restconfOutput; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 330 | } |
| 331 | |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 332 | private ResourceData getDataForStore(ResourceData resourceData) { |
| 333 | List<DataNode> nodes = resourceData.dataNodes(); |
| 334 | ResourceId rid = resourceData.resourceId(); |
| 335 | DataNode.Builder dbr = null; |
| 336 | ResourceId parentId = null; |
| 337 | try { |
| 338 | NodeKey lastKey = rid.nodeKeys().get(rid.nodeKeys().size() - 1); |
| 339 | SchemaId sid = lastKey.schemaId(); |
| 340 | if (lastKey instanceof ListKey) { |
| 341 | dbr = InnerNode.builder( |
| 342 | sid.name(), sid.namespace()).type(MULTI_INSTANCE_NODE); |
| 343 | for (KeyLeaf keyLeaf : ((ListKey) lastKey).keyLeafs()) { |
| 344 | Object val = keyLeaf.leafValue(); |
| 345 | dbr = dbr.addKeyLeaf(keyLeaf.leafSchema().name(), |
| 346 | sid.namespace(), val); |
| 347 | dbr = dbr.createChildBuilder(keyLeaf.leafSchema().name(), |
| 348 | sid.namespace(), val) |
| 349 | .type(SINGLE_INSTANCE_LEAF_VALUE_NODE); |
sonugupta-huawei | 6119ac7 | 2017-03-21 16:25:40 +0530 | [diff] [blame] | 350 | //Exit for key leaf node |
| 351 | dbr = dbr.exitNode(); |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 352 | } |
| 353 | } else { |
| 354 | dbr = InnerNode.builder( |
| 355 | sid.name(), sid.namespace()).type(SINGLE_INSTANCE_NODE); |
| 356 | } |
| 357 | if (nodes != null && !nodes.isEmpty()) { |
| 358 | // adding the parent node for given list of nodes |
| 359 | for (DataNode node : nodes) { |
| 360 | dbr = ((InnerNode.Builder) dbr).addNode(node); |
| 361 | } |
| 362 | } |
| 363 | parentId = rid.copyBuilder().removeLastKey().build(); |
| 364 | } catch (CloneNotSupportedException e) { |
Ray Milkey | 74e5913 | 2018-01-17 15:24:52 -0800 | [diff] [blame] | 365 | log.error("getDataForStore()", e); |
| 366 | return null; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 367 | } |
| 368 | ResourceData.Builder resData = DefaultResourceData.builder(); |
| 369 | resData.addDataNode(dbr.build()); |
| 370 | resData.resourceId(parentId); |
| 371 | return resData.build(); |
| 372 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 373 | } |