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