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 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;
27 * Implementation of {@link RestconfDataStreamService}.
30 public class RestconfDataStreamServiceImpl implements RestconfDataStreamService {
31 private static final Logger LOG = LoggerFactory.getLogger(RestconfDataStreamServiceImpl.class);
33 private final ListenersBroker listenersBroker = ListenersBroker.getInstance();
34 private final ScheduledExecutorService executorService;
35 private final int maximumFragmentLength;
36 private final int heartbeatInterval;
39 public RestconfDataStreamServiceImpl(final SSEInitializer configuration) {
40 executorService = configuration.getExecutorService();
41 heartbeatInterval = configuration.getHeartbeatInterval();
42 maximumFragmentLength = configuration.getMaximumFragmentLength();
46 public EventOutput getSSE(final String identifier, final UriInfo uriInfo) {
47 final String streamName = ListenersBroker.createStreamNameFromUri(identifier);
48 final BaseListenerInterface listener = listenersBroker.getListenerFor(streamName)
50 LOG.debug("Listener for stream with name {} was not found.", streamName);
51 throw new RestconfDocumentedException("Data missing", ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
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);