Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.protocol.restconf.server.restconfmanager; |
| 17 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.glassfish.jersey.server.ChunkedOutput; |
| 29 | import org.onosproject.event.ListenerTracker; |
| 30 | import org.onosproject.protocol.restconf.server.api.RestconfException; |
| 31 | import org.onosproject.protocol.restconf.server.api.RestconfService; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 32 | import org.onosproject.yms.ydt.YdtBuilder; |
| 33 | import org.onosproject.yms.ydt.YdtContext; |
| 34 | import org.onosproject.yms.ydt.YdtContextOperationType; |
| 35 | import org.onosproject.yms.ydt.YdtResponse; |
| 36 | import org.onosproject.yms.ydt.YmsOperationExecutionStatus; |
| 37 | import org.onosproject.yms.ydt.YmsOperationType; |
| 38 | import org.onosproject.yms.ymsm.YmsService; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 39 | import org.onosproject.yms.ynh.YangNotificationEvent; |
| 40 | import org.onosproject.yms.ynh.YangNotificationListener; |
| 41 | import org.onosproject.yms.ynh.YangNotificationService; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 42 | import org.slf4j.Logger; |
| 43 | import org.slf4j.LoggerFactory; |
| 44 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 45 | import java.io.IOException; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 46 | import java.util.Map.Entry; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [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 | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 54 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 55 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 56 | import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; |
| 57 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertJsonToYdt; |
| 58 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertUriToYdt; |
| 59 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.convertYdtToJson; |
| 60 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 61 | import static org.onosproject.yms.ydt.YdtContextOperationType.CREATE; |
| 62 | import static org.onosproject.yms.ydt.YdtContextOperationType.DELETE; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 63 | import static org.onosproject.yms.ydt.YdtContextOperationType.MERGE; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 64 | import static org.onosproject.yms.ydt.YdtContextOperationType.NONE; |
| 65 | import static org.onosproject.yms.ydt.YdtContextOperationType.REPLACE; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 66 | import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_LEAF_VALUE_NODE; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 67 | import static org.onosproject.yms.ydt.YmsOperationExecutionStatus.EXECUTION_EXCEPTION; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 68 | import static org.onosproject.yms.ydt.YmsOperationExecutionStatus.EXECUTION_SUCCESS; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 69 | import static org.onosproject.yms.ydt.YmsOperationType.EDIT_CONFIG_REQUEST; |
| 70 | import static org.onosproject.yms.ydt.YmsOperationType.QUERY_REQUEST; |
| 71 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 72 | /* |
| 73 | * Skeletal ONOS RESTCONF Server application. The RESTCONF Manager |
| 74 | * implements the main logic of the RESTCONF Server. |
| 75 | * |
| 76 | * The design of the RESTCONF subsystem contains 2 major bundles: |
| 77 | * |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 78 | * 1. RESTCONF Protocol Proxy (RPP). This bundle is implemented as a |
| 79 | * JAX-RS application. It acts as the frond-end of the RESTCONF server. |
| 80 | * It intercepts/handles HTTP requests that are sent to the RESTCONF |
| 81 | * Root Path. It then calls the RESTCONF Manager to process the requests. |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 82 | * |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 83 | * 2. RESTCONF Manager. This bundle module is the back-end of the server. |
| 84 | * It provides the main logic of the RESTCONF server. It interacts with |
| 85 | * the YMS (YANG Management System) to run operations on the YANG data |
| 86 | * objects (i.e., data resources). |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 87 | */ |
| 88 | |
| 89 | /** |
| 90 | * Implementation of the RestconfService interface. The class is designed |
| 91 | * as a Apache Flex component. Note that to avoid unnecessary |
| 92 | * activation, the @Component annotation's immediate parameter is set to false. |
| 93 | * So the component is not activated until a RESTCONF request is received by |
| 94 | * the RESTCONF Protocol Proxy (RPP) module, which consumes the service. |
| 95 | */ |
| 96 | @Component(immediate = false) |
| 97 | @Service |
| 98 | public class RestconfManager implements RestconfService { |
| 99 | |
| 100 | private static final String RESTCONF_ROOT = "/onos/restconf"; |
| 101 | private static final int THREAD_TERMINATION_TIMEOUT = 10; |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 102 | |
| 103 | // Jersey's default chunk parser uses "\r\n" as the chunk separator. |
| 104 | private static final String EOL = "\r\n"; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 105 | |
| 106 | private final int maxNumOfWorkerThreads = 5; |
| 107 | |
| 108 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 109 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 110 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 111 | protected YmsService ymsService; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 112 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 113 | protected YangNotificationService ymsNotificationService; |
| 114 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 115 | private ListenerTracker listeners; |
| 116 | |
| 117 | private ConcurrentMap<String, BlockingQueue<ObjectNode>> eventQueueList = |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 118 | new ConcurrentHashMap<>(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 119 | |
| 120 | private ExecutorService workerThreadPool; |
| 121 | |
| 122 | @Activate |
| 123 | protected void activate() { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 124 | workerThreadPool = Executors |
| 125 | .newFixedThreadPool(maxNumOfWorkerThreads, |
| 126 | new ThreadFactoryBuilder() |
| 127 | .setNameFormat("restconf-worker") |
| 128 | .build()); |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 129 | ymsNotificationService = ymsService.getYangNotificationService(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 130 | listeners = new ListenerTracker(); |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 131 | listeners.addListener(ymsNotificationService, new InternalYangNotificationListener()); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 132 | log.info("Started"); |
| 133 | } |
| 134 | |
| 135 | @Deactivate |
| 136 | protected void deactivate() { |
| 137 | listeners.removeListeners(); |
| 138 | shutdownAndAwaitTermination(workerThreadPool); |
| 139 | log.info("Stopped"); |
| 140 | } |
| 141 | |
| 142 | @Override |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 143 | public ObjectNode runGetOperationOnDataResource(String uri) |
| 144 | throws RestconfException { |
| 145 | YdtBuilder ydtBuilder = getYdtBuilder(QUERY_REQUEST); |
| 146 | //Convert the URI to ydtBuilder |
| 147 | convertUriToYdt(uri, ydtBuilder, NONE); |
| 148 | YdtResponse ydtResponse = ymsService.executeOperation(ydtBuilder); |
| 149 | YmsOperationExecutionStatus status = ydtResponse |
| 150 | .getYmsOperationResult(); |
| 151 | if (status != EXECUTION_SUCCESS) { |
| 152 | throw new RestconfException("YMS GET operation failed", |
| 153 | INTERNAL_SERVER_ERROR); |
| 154 | } |
| 155 | |
| 156 | YdtContext rootNode = ydtResponse.getRootNode(); |
| 157 | YdtContext curNode = ydtBuilder.getCurNode(); |
| 158 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 159 | ObjectNode result = convertYdtToJson(getJsonNameFromYdtNode(curNode), |
| 160 | rootNode, |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 161 | ymsService.getYdtWalker()); |
| 162 | //if the query URI contain a key, something like list=key |
| 163 | //here should only get get child with the specific key |
| 164 | YdtContext child = curNode.getFirstChild(); |
| 165 | if (child != null && |
| 166 | child.getYdtType() == SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 167 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 168 | ArrayNode jsonNode = (ArrayNode) result.get(getJsonNameFromYdtNode(curNode)); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 169 | for (JsonNode next : jsonNode) { |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 170 | if (next.findValue(getJsonNameFromYdtNode(child)) |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 171 | .asText().equals(child.getValue())) { |
| 172 | return (ObjectNode) next; |
| 173 | } |
| 174 | } |
| 175 | throw new RestconfException(String.format("No content for %s = %s", |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 176 | getJsonNameFromYdtNode(child), |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 177 | child.getValue()), |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 178 | INTERNAL_SERVER_ERROR); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 179 | } |
| 180 | return result; |
| 181 | } |
| 182 | |
| 183 | private YmsOperationExecutionStatus |
| 184 | invokeYmsOp(String uri, ObjectNode rootNode, |
| 185 | YdtContextOperationType opType) { |
| 186 | YdtBuilder ydtBuilder = getYdtBuilder(EDIT_CONFIG_REQUEST); |
| 187 | //Convert the URI to ydtBuilder |
| 188 | convertUriToYdt(uri, ydtBuilder, opType); |
| 189 | |
| 190 | //set default operation type for the payload node |
| 191 | ydtBuilder.setDefaultEditOperationType(opType); |
| 192 | //convert the payload json body to ydt |
| 193 | convertJsonToYdt(rootNode, ydtBuilder); |
| 194 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 195 | YmsOperationExecutionStatus status = EXECUTION_EXCEPTION; |
| 196 | |
| 197 | try { |
| 198 | status = ymsService.executeOperation(ydtBuilder).getYmsOperationResult(); |
| 199 | } catch (Exception e) { |
| 200 | log.error("YMS operation failed: {}", e.getMessage()); |
| 201 | log.debug("Exception in invokeYmsOp: ", e); |
| 202 | } |
| 203 | |
| 204 | return status; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 208 | public void runPostOperationOnDataResource(String uri, ObjectNode rootNode) |
| 209 | throws RestconfException { |
| 210 | YmsOperationExecutionStatus status = |
| 211 | invokeYmsOp(uri, rootNode, CREATE); |
| 212 | |
| 213 | if (status != EXECUTION_SUCCESS) { |
| 214 | throw new RestconfException("YMS post operation failed.", |
| 215 | INTERNAL_SERVER_ERROR); |
| 216 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 220 | public void runPutOperationOnDataResource(String uri, ObjectNode rootNode) |
| 221 | throws RestconfException { |
| 222 | YmsOperationExecutionStatus status = |
| 223 | invokeYmsOp(uri, rootNode, REPLACE); |
| 224 | |
| 225 | if (status != EXECUTION_SUCCESS) { |
| 226 | throw new RestconfException("YMS put operation failed.", |
| 227 | INTERNAL_SERVER_ERROR); |
| 228 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 229 | } |
| 230 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 231 | @Override |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 232 | public void runDeleteOperationOnDataResource(String uri) |
| 233 | throws RestconfException { |
| 234 | //Get a root ydtBuilder |
| 235 | YdtBuilder ydtBuilder = getYdtBuilder(EDIT_CONFIG_REQUEST); |
| 236 | //Convert the URI to ydtBuilder |
| 237 | convertUriToYdt(uri, ydtBuilder, DELETE); |
| 238 | //Execute the delete operation |
| 239 | YmsOperationExecutionStatus status = ymsService |
| 240 | .executeOperation(ydtBuilder) |
| 241 | .getYmsOperationResult(); |
| 242 | if (status != EXECUTION_SUCCESS) { |
| 243 | throw new RestconfException("YMS delete operation failed.", |
| 244 | INTERNAL_SERVER_ERROR); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public void runPatchOperationOnDataResource(String uri, ObjectNode rootNode) |
| 250 | throws RestconfException { |
| 251 | YmsOperationExecutionStatus status = invokeYmsOp(uri, rootNode, MERGE); |
| 252 | |
| 253 | if (status != EXECUTION_SUCCESS) { |
| 254 | throw new RestconfException("YMS patch operation failed.", |
| 255 | INTERNAL_SERVER_ERROR); |
| 256 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public String getRestconfRootPath() { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 261 | return RESTCONF_ROOT; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Creates a worker thread to listen to events and write to chunkedOutput. |
| 266 | * The worker thread blocks if no events arrive. |
| 267 | * |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 268 | * @param streamId the RESTCONF stream id to which the client subscribes |
| 269 | * @param output the string data stream |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 270 | * @throws RestconfException if the worker thread fails to create |
| 271 | */ |
| 272 | @Override |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 273 | public void subscribeEventStream(String streamId, |
| 274 | ChunkedOutput<String> output) |
| 275 | throws RestconfException { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 276 | if (workerThreadPool instanceof ThreadPoolExecutor) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 277 | if (((ThreadPoolExecutor) workerThreadPool).getActiveCount() >= |
| 278 | maxNumOfWorkerThreads) { |
| 279 | throw new RestconfException("no more work threads left to " + |
| 280 | "handle event subscription", |
| 281 | INTERNAL_SERVER_ERROR); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 282 | } |
| 283 | } else { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 284 | throw new RestconfException("Server ERROR: workerThreadPool NOT " + |
| 285 | "instanceof ThreadPoolExecutor", |
| 286 | INTERNAL_SERVER_ERROR); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 287 | |
| 288 | } |
| 289 | |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 290 | BlockingQueue<ObjectNode> eventQueue = new LinkedBlockingQueue<>(); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 291 | workerThreadPool.submit(new EventConsumer(output, eventQueue)); |
| 292 | } |
| 293 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 294 | /** |
| 295 | * Shutdown a pool cleanly if possible. |
| 296 | * |
| 297 | * @param pool an executorService |
| 298 | */ |
| 299 | private void shutdownAndAwaitTermination(ExecutorService pool) { |
| 300 | pool.shutdown(); // Disable new tasks from being submitted |
| 301 | try { |
| 302 | // Wait a while for existing tasks to terminate |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 303 | if (!pool.awaitTermination(THREAD_TERMINATION_TIMEOUT, SECONDS)) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 304 | pool.shutdownNow(); // Cancel currently executing tasks |
| 305 | // Wait a while for tasks to respond to being cancelled |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 306 | if (!pool.awaitTermination(THREAD_TERMINATION_TIMEOUT, |
| 307 | SECONDS)) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 308 | log.error("Pool did not terminate"); |
| 309 | } |
| 310 | } |
| 311 | } catch (Exception ie) { |
| 312 | // (Re-)Cancel if current thread also interrupted |
| 313 | pool.shutdownNow(); |
| 314 | // Preserve interrupt status |
| 315 | Thread.currentThread().interrupt(); |
| 316 | } |
| 317 | } |
| 318 | |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 319 | /** |
| 320 | * Implementation of a worker thread which reads data from a |
| 321 | * blocking queue and writes the data to a given chunk output stream. |
| 322 | * The thread is blocked when no data arrive to the queue and is |
| 323 | * terminated when the chunk output stream is closed (i.e., the |
| 324 | * HTTP-keep-alive session is closed). |
| 325 | */ |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 326 | private class EventConsumer implements Runnable { |
| 327 | |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 328 | private String queueId; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 329 | private final ChunkedOutput<String> output; |
| 330 | private final BlockingQueue<ObjectNode> bqueue; |
| 331 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 332 | public EventConsumer(ChunkedOutput<String> output, |
| 333 | BlockingQueue<ObjectNode> q) { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 334 | this.output = output; |
| 335 | this.bqueue = q; |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void run() { |
| 340 | try { |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 341 | queueId = String.valueOf(Thread.currentThread().getId()); |
| 342 | eventQueueList.put(queueId, bqueue); |
| 343 | log.debug("EventConsumer thread created: {}", queueId); |
| 344 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 345 | ObjectNode chunk; |
| 346 | while ((chunk = bqueue.take()) != null) { |
| 347 | output.write(chunk.toString().concat(EOL)); |
| 348 | } |
| 349 | } catch (IOException e) { |
| 350 | log.debug("chunkedOuput is closed: {}", this.bqueue.toString()); |
| 351 | /* |
| 352 | * Remove queue from the queue list, so that the event producer |
| 353 | * (i.e., listener) would stop working. |
| 354 | */ |
| 355 | eventQueueList.remove(this.queueId); |
| 356 | } catch (InterruptedException e) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 357 | log.error("ERROR: EventConsumer: bqueue.take() " + |
| 358 | "has been interrupted."); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 359 | log.debug("EventConsumer Exception:", e); |
| 360 | } finally { |
| 361 | try { |
| 362 | output.close(); |
| 363 | log.debug("EventConsumer thread terminated: {}", queueId); |
| 364 | } catch (IOException e) { |
| 365 | log.error("ERROR: EventConsumer: ", e); |
| 366 | } |
| 367 | } |
| 368 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 369 | } |
| 370 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 371 | private YdtBuilder getYdtBuilder(YmsOperationType ymsOperationType) { |
| 372 | return ymsService.getYdtBuilder(RESTCONF_ROOT, null, ymsOperationType); |
| 373 | } |
| 374 | |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 375 | /** |
| 376 | * The listener class acts as the event producer for the event queues. The |
| 377 | * queues are created by the event consumer threads and are removed when the |
| 378 | * threads terminate. |
| 379 | */ |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 380 | private class InternalYangNotificationListener implements YangNotificationListener { |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 381 | |
| 382 | @Override |
| 383 | public void event(YangNotificationEvent event) { |
| 384 | if (event.type() != YangNotificationEvent.Type.YANG_NOTIFICATION) { |
| 385 | // For now, we only handle YANG notification events. |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | if (eventQueueList.isEmpty()) { |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 390 | /* |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 391 | * There is no consumer waiting to consume, so don't have to |
| 392 | * produce this event. |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 393 | */ |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 394 | log.debug("Q list is empty"); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | |
| 398 | try { |
Henry Yu | dc747af | 2016-11-16 13:29:54 -0500 | [diff] [blame^] | 399 | YdtContext ydtNode = event.subject().getNotificationRootContext(); |
| 400 | ObjectNode jsonNode = convertYdtToJson(getJsonNameFromYdtNode(ydtNode), |
| 401 | ydtNode, |
| 402 | ymsService.getYdtWalker()); |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 403 | /* |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 404 | * Put the event to every queue out there. Each queue is |
| 405 | * corresponding to an event stream session. The queue is |
| 406 | * removed when the session terminates. |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 407 | */ |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 408 | for (Entry<String, BlockingQueue<ObjectNode>> entry : eventQueueList |
| 409 | .entrySet()) { |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 410 | entry.getValue().put(jsonNode); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 411 | } |
| 412 | } catch (InterruptedException e) { |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 413 | log.error("Failed to put event in queue: {}", e.getMessage()); |
| 414 | log.debug("Exception trace in InternalYangNotificationListener: ", e); |
| 415 | throw new RestconfException("Failed to put event in queue", |
| 416 | INTERNAL_SERVER_ERROR); |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 417 | } |
| 418 | } |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 419 | } |
Henry Yu | e20926e | 2016-08-25 22:58:02 -0400 | [diff] [blame] | 420 | } |