Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 1 | /* |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Laboratory |
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; |
| 31 | import org.onosproject.restconf.api.RestconfException; |
| 32 | import org.onosproject.restconf.api.RestconfService; |
| 33 | import org.onosproject.yang.model.DataNode; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 34 | import org.onosproject.yang.model.InnerNode; |
| 35 | import org.onosproject.yang.model.KeyLeaf; |
| 36 | import org.onosproject.yang.model.ListKey; |
| 37 | import org.onosproject.yang.model.NodeKey; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 38 | import org.onosproject.yang.model.ResourceData; |
| 39 | import org.onosproject.yang.model.ResourceId; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 40 | import org.onosproject.yang.model.SchemaId; |
| 41 | import org.onosproject.yang.runtime.DefaultResourceData; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
| 45 | import java.io.IOException; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 46 | import java.util.List; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 47 | import java.util.concurrent.BlockingQueue; |
| 48 | import java.util.concurrent.ConcurrentHashMap; |
| 49 | import java.util.concurrent.ConcurrentMap; |
| 50 | import java.util.concurrent.ExecutorService; |
| 51 | import java.util.concurrent.Executors; |
| 52 | import java.util.concurrent.LinkedBlockingQueue; |
| 53 | import java.util.concurrent.ThreadPoolExecutor; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 54 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 55 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 56 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 57 | import static org.onosproject.restconf.utils.RestconfUtils.convertDataNodeToJson; |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 58 | import static org.onosproject.restconf.utils.RestconfUtils.convertJsonToDataNode; |
| 59 | import static org.onosproject.restconf.utils.RestconfUtils.convertUriToRid; |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 60 | import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE; |
| 61 | import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
| 62 | import static org.onosproject.yang.model.DataNode.Type.SINGLE_INSTANCE_NODE; |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 63 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 64 | /* |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 65 | * ONOS RESTCONF application. The RESTCONF Manager |
| 66 | * implements the main logic of the RESTCONF application. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 67 | * |
| 68 | * The design of the RESTCONF subsystem contains 2 major bundles: |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 69 | * This bundle module is the back-end of the server. |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 70 | * It provides the main logic of the RESTCONF server. It interacts with |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 71 | * the Dynamic Config Service and yang runtime service to run operations |
| 72 | * on the YANG data objects (i.e., resource id, yang data node). |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 73 | */ |
| 74 | |
jingan | 8a77332 | 2017-03-21 16:12:48 -0700 | [diff] [blame^] | 75 | @Component(immediate = true) |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 76 | @Service |
| 77 | public class RestconfManager implements RestconfService { |
| 78 | |
| 79 | private static final String RESTCONF_ROOT = "/onos/restconf"; |
| 80 | private static final int THREAD_TERMINATION_TIMEOUT = 10; |
| 81 | |
| 82 | // Jersey's default chunk parser uses "\r\n" as the chunk separator. |
| 83 | private static final String EOL = "\r\n"; |
| 84 | |
| 85 | private final int maxNumOfWorkerThreads = 5; |
| 86 | |
| 87 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 88 | |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 90 | protected DynamicConfigService dynamicConfigService; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 91 | |
| 92 | private ConcurrentMap<String, BlockingQueue<ObjectNode>> eventQueueList = |
| 93 | new ConcurrentHashMap<>(); |
| 94 | |
| 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() { |
| 109 | shutdownAndAwaitTermination(workerThreadPool); |
| 110 | log.info("Stopped"); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public ObjectNode runGetOperationOnDataResource(String uri) |
| 115 | throws RestconfException { |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 116 | ResourceId rid = convertUriToRid(uri); |
| 117 | // TODO: define Filter (if there is any requirement). |
| 118 | Filter filter = new Filter(); |
| 119 | DataNode dataNode; |
| 120 | try { |
Henry Yu | 14af778 | 2017-03-09 19:33:36 -0500 | [diff] [blame] | 121 | dataNode = dynamicConfigService.readNode(rid, filter); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 122 | } catch (FailedException e) { |
| 123 | log.error("ERROR: DynamicConfigService: ", e); |
| 124 | throw new RestconfException("ERROR: DynamicConfigService", |
| 125 | INTERNAL_SERVER_ERROR); |
| 126 | } |
| 127 | ObjectNode rootNode = convertDataNodeToJson(rid, dataNode); |
| 128 | return rootNode; |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void runPostOperationOnDataResource(String uri, ObjectNode rootNode) |
| 133 | throws RestconfException { |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 134 | ResourceData receivedData = convertJsonToDataNode(uri, rootNode); |
| 135 | ResourceData resourceData = getDataForStore(receivedData); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 136 | ResourceId rid = resourceData.resourceId(); |
| 137 | List<DataNode> dataNodeList = resourceData.dataNodes(); |
| 138 | // TODO: Error message needs to be fixed |
| 139 | if (dataNodeList.size() > 1) { |
| 140 | log.warn("ERROR: There are more than one Data Node can be proceed"); |
| 141 | } |
| 142 | DataNode dataNode = dataNodeList.get(0); |
| 143 | try { |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 144 | dynamicConfigService.createNodeRecursive(rid, dataNode); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 145 | } catch (FailedException e) { |
| 146 | log.error("ERROR: DynamicConfigService: ", e); |
| 147 | throw new RestconfException("ERROR: DynamicConfigService", |
| 148 | INTERNAL_SERVER_ERROR); |
| 149 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public void runPutOperationOnDataResource(String uri, ObjectNode rootNode) |
| 154 | throws RestconfException { |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 155 | runPostOperationOnDataResource(uri, rootNode); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public void runDeleteOperationOnDataResource(String uri) |
| 160 | throws RestconfException { |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 161 | ResourceId rid = convertUriToRid(uri); |
| 162 | try { |
jingan | 364cec3 | 2017-03-10 12:29:11 -0800 | [diff] [blame] | 163 | dynamicConfigService.deleteNodeRecursive(rid); |
jingan | 7c5bf1f | 2017-02-09 02:58:09 -0800 | [diff] [blame] | 164 | } catch (FailedException e) { |
| 165 | log.error("ERROR: DynamicConfigService: ", e); |
| 166 | throw new RestconfException("ERROR: DynamicConfigService", |
| 167 | INTERNAL_SERVER_ERROR); |
| 168 | } |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void runPatchOperationOnDataResource(String uri, ObjectNode rootNode) |
| 173 | throws RestconfException { |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public String getRestconfRootPath() { |
| 178 | return RESTCONF_ROOT; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Creates a worker thread to listen to events and write to chunkedOutput. |
| 183 | * The worker thread blocks if no events arrive. |
| 184 | * |
| 185 | * @param streamId the RESTCONF stream id to which the client subscribes |
| 186 | * @param output the string data stream |
| 187 | * @throws RestconfException if the worker thread fails to create |
| 188 | */ |
| 189 | @Override |
| 190 | public void subscribeEventStream(String streamId, |
| 191 | ChunkedOutput<String> output) |
| 192 | throws RestconfException { |
| 193 | if (workerThreadPool instanceof ThreadPoolExecutor) { |
| 194 | if (((ThreadPoolExecutor) workerThreadPool).getActiveCount() >= |
| 195 | maxNumOfWorkerThreads) { |
| 196 | throw new RestconfException("no more work threads left to " + |
| 197 | "handle event subscription", |
| 198 | INTERNAL_SERVER_ERROR); |
| 199 | } |
| 200 | } else { |
| 201 | throw new RestconfException("Server ERROR: workerThreadPool NOT " + |
| 202 | "instanceof ThreadPoolExecutor", |
| 203 | INTERNAL_SERVER_ERROR); |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | BlockingQueue<ObjectNode> eventQueue = new LinkedBlockingQueue<>(); |
| 207 | workerThreadPool.submit(new EventConsumer(output, eventQueue)); |
| 208 | } |
| 209 | |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 210 | private ResourceData getDataForStore(ResourceData resourceData) { |
| 211 | List<DataNode> nodes = resourceData.dataNodes(); |
| 212 | ResourceId rid = resourceData.resourceId(); |
| 213 | DataNode.Builder dbr = null; |
| 214 | ResourceId parentId = null; |
| 215 | try { |
| 216 | NodeKey lastKey = rid.nodeKeys().get(rid.nodeKeys().size() - 1); |
| 217 | SchemaId sid = lastKey.schemaId(); |
| 218 | if (lastKey instanceof ListKey) { |
| 219 | dbr = InnerNode.builder( |
| 220 | sid.name(), sid.namespace()).type(MULTI_INSTANCE_NODE); |
| 221 | for (KeyLeaf keyLeaf : ((ListKey) lastKey).keyLeafs()) { |
| 222 | Object val = keyLeaf.leafValue(); |
| 223 | dbr = dbr.addKeyLeaf(keyLeaf.leafSchema().name(), |
| 224 | sid.namespace(), val); |
| 225 | dbr = dbr.createChildBuilder(keyLeaf.leafSchema().name(), |
| 226 | sid.namespace(), val) |
| 227 | .type(SINGLE_INSTANCE_LEAF_VALUE_NODE); |
sonugupta-huawei | 6119ac7 | 2017-03-21 16:25:40 +0530 | [diff] [blame] | 228 | //Exit for key leaf node |
| 229 | dbr = dbr.exitNode(); |
sonugupta-huawei | f0af7aa | 2017-03-17 00:54:52 +0530 | [diff] [blame] | 230 | } |
| 231 | } else { |
| 232 | dbr = InnerNode.builder( |
| 233 | sid.name(), sid.namespace()).type(SINGLE_INSTANCE_NODE); |
| 234 | } |
| 235 | if (nodes != null && !nodes.isEmpty()) { |
| 236 | // adding the parent node for given list of nodes |
| 237 | for (DataNode node : nodes) { |
| 238 | dbr = ((InnerNode.Builder) dbr).addNode(node); |
| 239 | } |
| 240 | } |
| 241 | parentId = rid.copyBuilder().removeLastKey().build(); |
| 242 | } catch (CloneNotSupportedException e) { |
| 243 | e.printStackTrace(); |
| 244 | } |
| 245 | ResourceData.Builder resData = DefaultResourceData.builder(); |
| 246 | resData.addDataNode(dbr.build()); |
| 247 | resData.resourceId(parentId); |
| 248 | return resData.build(); |
| 249 | } |
| 250 | |
Jin Gan | 79f7537 | 2017-01-05 15:08:11 -0800 | [diff] [blame] | 251 | /** |
| 252 | * Shutdown a pool cleanly if possible. |
| 253 | * |
| 254 | * @param pool an executorService |
| 255 | */ |
| 256 | private void shutdownAndAwaitTermination(ExecutorService pool) { |
| 257 | pool.shutdown(); // Disable new tasks from being submitted |
| 258 | try { |
| 259 | // Wait a while for existing tasks to terminate |
| 260 | if (!pool.awaitTermination(THREAD_TERMINATION_TIMEOUT, SECONDS)) { |
| 261 | pool.shutdownNow(); // Cancel currently executing tasks |
| 262 | // Wait a while for tasks to respond to being cancelled |
| 263 | if (!pool.awaitTermination(THREAD_TERMINATION_TIMEOUT, |
| 264 | SECONDS)) { |
| 265 | log.error("Pool did not terminate"); |
| 266 | } |
| 267 | } |
| 268 | } catch (Exception ie) { |
| 269 | // (Re-)Cancel if current thread also interrupted |
| 270 | pool.shutdownNow(); |
| 271 | // Preserve interrupt status |
| 272 | Thread.currentThread().interrupt(); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Implementation of a worker thread which reads data from a |
| 278 | * blocking queue and writes the data to a given chunk output stream. |
| 279 | * The thread is blocked when no data arrive to the queue and is |
| 280 | * terminated when the chunk output stream is closed (i.e., the |
| 281 | * HTTP-keep-alive session is closed). |
| 282 | */ |
| 283 | private class EventConsumer implements Runnable { |
| 284 | |
| 285 | private String queueId; |
| 286 | private final ChunkedOutput<String> output; |
| 287 | private final BlockingQueue<ObjectNode> bqueue; |
| 288 | |
| 289 | public EventConsumer(ChunkedOutput<String> output, |
| 290 | BlockingQueue<ObjectNode> q) { |
| 291 | this.output = output; |
| 292 | this.bqueue = q; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public void run() { |
| 297 | try { |
| 298 | queueId = String.valueOf(Thread.currentThread().getId()); |
| 299 | eventQueueList.put(queueId, bqueue); |
| 300 | log.debug("EventConsumer thread created: {}", queueId); |
| 301 | |
| 302 | ObjectNode chunk; |
| 303 | while ((chunk = bqueue.take()) != null) { |
| 304 | output.write(chunk.toString().concat(EOL)); |
| 305 | } |
| 306 | } catch (IOException e) { |
| 307 | log.debug("chunkedOuput is closed: {}", this.bqueue.toString()); |
| 308 | /* |
| 309 | * Remove queue from the queue list, so that the event producer |
| 310 | * (i.e., listener) would stop working. |
| 311 | */ |
| 312 | eventQueueList.remove(this.queueId); |
| 313 | } catch (InterruptedException e) { |
| 314 | log.error("ERROR: EventConsumer: bqueue.take() " + |
| 315 | "has been interrupted."); |
| 316 | log.debug("EventConsumer Exception:", e); |
| 317 | } finally { |
| 318 | try { |
| 319 | output.close(); |
| 320 | log.debug("EventConsumer thread terminated: {}", queueId); |
| 321 | } catch (IOException e) { |
| 322 | log.error("ERROR: EventConsumer: ", e); |
| 323 | } |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |