2 * Copyright (c) 2020 Lumina Networks, Inc. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
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;
28 * Implementation of {@link RestconfDataStreamService}.
31 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
32 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
34 private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
35 private final ScheduledExecutorService executorService;
36 private final int maximumFragmentLength;
37 private final int heartbeatInterval;
40 public RestconfDataStreamServiceImpl(final SSEInitializer configuration) {
41 executorService = configuration.getExecutorService();
42 heartbeatInterval = configuration.getHeartbeatInterval();
43 maximumFragmentLength = configuration.getMaximumFragmentLength();
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);
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);
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);