9aa37c73cb7cf013bdbebded253eda8ac5b85ad8
[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.deviceNotificationListenerFor(streamName);
57             if (listener == null) {
58                 LOG.debug("Listener for device path with name {} was not found.", streamName);
59                 throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
60             }
61         } else {
62             listener = listenersBroker.listenerFor(streamName);
63             if (listener == null) {
64                 LOG.debug("Listener for stream with name {} was not found.", streamName);
65                 throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
66             }
67         }
68
69         LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
70         // FIXME: invert control here: we should call 'listener.addSession()', which in turn should call
71         //        handler.init()/handler.close()
72         final SSESessionHandler handler = new SSESessionHandler(executorService, sink, sse, listener,
73             maximumFragmentLength, heartbeatInterval);
74         handler.init();
75     }
76 }