9f90352a4061650e3078f5fbe1de89e70a70cfa3
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataStreamService;
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.ErrorType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Implementation of {@link RestconfDataStreamService}.
30  */
31 @Singleton
32 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
33     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
34
35     private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
36     private final ScheduledExecutorService executorService;
37     private final int maximumFragmentLength;
38     private final int heartbeatInterval;
39
40     @Inject
41     public RestconfDataStreamServiceImpl(final ScheduledThreadPool scheduledThreadPool,
42             final Configuration configuration) {
43         executorService = scheduledThreadPool.getExecutor();
44         heartbeatInterval = configuration.getHeartbeatInterval();
45         maximumFragmentLength = configuration.getMaximumFragmentLength();
46     }
47
48     @Override
49     public void getSSE(final String identifier, final UriInfo uriInfo, final SseEventSink sink, final Sse sse) {
50         final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
51         final BaseListenerInterface listener = listenersBroker.getListenerFor(streamName)
52             .orElseThrow(() -> {
53                 LOG.debug("Listener for stream with name {} was not found.", streamName);
54                 throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
55             });
56
57         LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
58
59         // FIXME: invert control here: we should call 'listener.addSession()', which in turn should call
60         //        handler.init()/handler.close()
61         final SSESessionHandler handler = new SSESessionHandler(executorService, sink, sse, listener,
62             maximumFragmentLength, heartbeatInterval);
63         handler.init();
64     }
65 }