Switch to using SseEventSink
[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.Optional;
11 import java.util.concurrent.ScheduledExecutorService;
12 import javax.inject.Inject;
13 import javax.inject.Singleton;
14 import javax.ws.rs.core.UriInfo;
15 import javax.ws.rs.sse.Sse;
16 import javax.ws.rs.sse.SseEventSink;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
19 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
20 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataStreamService;
21 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.BaseListenerInterface;
22 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
23 import org.opendaylight.restconf.nb.rfc8040.streams.sse.SSEInitializer;
24 import org.opendaylight.restconf.nb.rfc8040.streams.sse.SSESessionHandler;
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 SSEInitializer configuration) {
42         executorService = configuration.getExecutorService();
43         heartbeatInterval = configuration.getHeartbeatInterval();
44         maximumFragmentLength = configuration.getMaximumFragmentLength();
45     }
46
47     @Override
48     public void getSSE(final String identifier, final UriInfo uriInfo, final SseEventSink sink, final Sse sse) {
49         final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
50         final Optional<BaseListenerInterface> listener = listenersBroker.getListenerFor(streamName);
51
52         if (listener.isEmpty()) {
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.get(),
62             maximumFragmentLength, heartbeatInterval);
63         handler.init();
64     }
65 }