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.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.nb.rfc8040.rests.services.api.RestconfDataStreamService;
19 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
20 import org.opendaylight.restconf.nb.rfc8040.streams.SSESessionHandler;
21 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.BaseListenerInterface;
22 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Implementation of {@link RestconfDataStreamService}.
32 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
33 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
35 private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
36 private final ScheduledExecutorService executorService;
37 private final int maximumFragmentLength;
38 private final int heartbeatInterval;
41 public RestconfDataStreamServiceImpl(final ScheduledThreadPool scheduledThreadPool,
42 final Configuration configuration) {
43 executorService = scheduledThreadPool.getExecutor();
44 heartbeatInterval = configuration.getHeartbeatInterval();
45 maximumFragmentLength = configuration.getMaximumFragmentLength();
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)
53 LOG.debug("Listener for stream with name {} was not found.", streamName);
54 throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
57 LOG.debug("Listener for stream with name {} has been found, SSE session handler will be created.", streamName);
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);