6b06b5278a0b6c6e0eed7259a5de8dfb0fcfef32
[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 org.glassfish.jersey.media.sse.EventOutput;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
18 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
19 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataStreamService;
20 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.BaseListenerInterface;
21 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
22 import org.opendaylight.restconf.nb.rfc8040.streams.sse.SSEInitializer;
23 import org.opendaylight.restconf.nb.rfc8040.streams.sse.SSESessionHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Implementation of {@link RestconfDataStreamService}.
29  */
30 @Singleton
31 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
32     private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
33
34     private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
35     private final ScheduledExecutorService executorService;
36     private final int maximumFragmentLength;
37     private final int heartbeatInterval;
38
39     @Inject
40     public RestconfDataStreamServiceImpl(final SSEInitializer configuration) {
41         executorService = configuration.getExecutorService();
42         heartbeatInterval = configuration.getHeartbeatInterval();
43         maximumFragmentLength = configuration.getMaximumFragmentLength();
44     }
45
46     @Override
47     public EventOutput getSSE(final String identifier, final UriInfo uriInfo) {
48         final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
49         final Optional<BaseListenerInterface> listener = listenersBroker.getListenerFor(streamName);
50
51         if (listener.isEmpty()) {
52             LOG.debug("Listener for stream with name {} was not found.", streamName);
53             throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
54         }
55
56         LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
57         final EventOutput eventOutput = new EventOutput();
58         final SSESessionHandler handler = new SSESessionHandler(executorService, eventOutput, listener.get(),
59             maximumFragmentLength, heartbeatInterval);
60         handler.init();
61         return eventOutput;
62     }
63 }