Netconf Device Notification
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataStreamServiceImpl.java
1 /*
2  * Copyright (c) 2020 Lumina Networks, Inc. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import java.util.concurrent.ScheduledExecutorService;
11 import javax.inject.Inject;
12 import javax.inject.Singleton;
13 import javax.ws.rs.core.UriInfo;
14 import javax.ws.rs.sse.Sse;
15 import javax.ws.rs.sse.SseEventSink;
16 import org.opendaylight.controller.config.threadpool.ScheduledThreadPool;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataStreamService;
19 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
20 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
21 import org.opendaylight.restconf.nb.rfc8040.streams.SSESessionHandler;
22 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.BaseListenerInterface;
23 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
24 import org.opendaylight.yangtools.yang.common.ErrorTag;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Implementation of {@link RestconfDataStreamService}.
31  */
32 @Singleton
33 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
34     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
35
36     private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
37     private final ScheduledExecutorService executorService;
38     private final int maximumFragmentLength;
39     private final int heartbeatInterval;
40
41     @Inject
42     public RestconfDataStreamServiceImpl(final ScheduledThreadPool scheduledThreadPool,
43             final Configuration configuration) {
44         executorService = scheduledThreadPool.getExecutor();
45         heartbeatInterval = configuration.getHeartbeatInterval();
46         maximumFragmentLength = configuration.getMaximumFragmentLength();
47     }
48
49     @Override
50     public void getSSE(final String identifier, final UriInfo uriInfo, final SseEventSink sink, final Sse sse) {
51         final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
52         final BaseListenerInterface listener;
53         final String notificaionType =
54             uriInfo.getQueryParameters().getFirst(RestconfStreamsConstants.NOTIFICATION_TYPE);
55         if (notificaionType != null && notificaionType.equals(RestconfStreamsConstants.DEVICE)) {
56             listener = listenersBroker.getDeviceNotificationListenerFor(streamName)
57                 .orElseThrow(() -> {
58                     LOG.debug("Listener for device path with name {} was not found.", streamName);
59                     throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION,
60                         ErrorTag.DATA_MISSING);
61                 });
62         } else {
63             listener = listenersBroker.getListenerFor(streamName)
64                 .orElseThrow(() -> {
65                     LOG.debug("Listener for stream with name {} was not found.", streamName);
66                     throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
67                 });
68         }
69
70         LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
71         // FIXME: invert control here: we should call 'listener.addSession()', which in turn should call
72         //        handler.init()/handler.close()
73         final SSESessionHandler handler = new SSESessionHandler(executorService, sink, sse, listener,
74             maximumFragmentLength, heartbeatInterval);
75         handler.init();
76     }
77 }