2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. 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.streams;
10 import static java.util.Objects.requireNonNull;
14 import javax.servlet.http.HttpServlet;
15 import javax.ws.rs.core.Application;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.aaa.web.servlet.ServletSupport;
18 import org.opendaylight.restconf.api.query.PrettyPrintParam;
19 import org.opendaylight.restconf.server.spi.RestconfStream;
20 import org.osgi.service.component.annotations.Activate;
21 import org.osgi.service.component.annotations.Component;
22 import org.osgi.service.component.annotations.Deactivate;
23 import org.osgi.service.component.annotations.Reference;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Auxiliary interface for instantiating JAX-RS streams.
30 * @deprecated This componet exists only to support SSE/Websocket delivery. It will be removed when support for
31 * WebSockets is removed.
33 @Component(factory = DefaultRestconfStreamServletFactory.FACTORY_NAME, service = RestconfStreamServletFactory.class)
34 @Deprecated(since = "7.0.0", forRemoval = true)
35 public final class DefaultRestconfStreamServletFactory implements RestconfStreamServletFactory, AutoCloseable {
36 private static final Logger LOG = LoggerFactory.getLogger(DefaultRestconfStreamServletFactory.class);
38 public static final String FACTORY_NAME =
39 "org.opendaylight.restconf.nb.rfc8040.streams.RestconfStreamServletFactory";
41 private static final String PROP_STREAM_REGISTRY = ".streamRegistry";
42 private static final String PROP_NAME_PREFIX = ".namePrefix";
43 private static final String PROP_CORE_POOL_SIZE = ".corePoolSize";
44 private static final String PROP_USE_WEBSOCKETS = ".useWebsockets";
45 private static final String PROP_STREAMS_CONFIGURATION = ".streamsConfiguration";
46 private static final String PROP_RESTCONF = ".restconf";
47 private static final String PROP_PRETTY_PRINT = ".prettyPrint";
49 private final @NonNull String restconf;
50 private final @NonNull PrettyPrintParam prettyPrint;
51 private final RestconfStream.Registry streamRegistry;
52 private final ServletSupport servletSupport;
54 private final DefaultPingExecutor pingExecutor;
55 private final StreamsConfiguration streamsConfiguration;
56 private final boolean useWebsockets;
58 public DefaultRestconfStreamServletFactory(final ServletSupport servletSupport, final String restconf,
59 final RestconfStream.Registry streamRegistry, final StreamsConfiguration streamsConfiguration,
60 final PrettyPrintParam prettyPrint, final String namePrefix, final int corePoolSize,
61 final boolean useWebsockets) {
62 this.servletSupport = requireNonNull(servletSupport);
63 this.restconf = requireNonNull(restconf);
64 if (restconf.endsWith("/")) {
65 throw new IllegalArgumentException("{+restconf} value ends with /");
67 this.streamRegistry = requireNonNull(streamRegistry);
68 this.streamsConfiguration = requireNonNull(streamsConfiguration);
69 this.prettyPrint = requireNonNull(prettyPrint);
70 pingExecutor = new DefaultPingExecutor(namePrefix, corePoolSize);
71 this.useWebsockets = useWebsockets;
74 RESTCONF event streams use WebSockets instead of Server-Sent Events. This option is will be removed in
75 the next major release.""");
80 public DefaultRestconfStreamServletFactory(@Reference final ServletSupport servletSupport,
81 final Map<String, ?> props) {
82 this(servletSupport, (String) props.get(PROP_RESTCONF),
83 (RestconfStream.Registry) props.get(PROP_STREAM_REGISTRY),
84 (StreamsConfiguration) props.get(PROP_STREAMS_CONFIGURATION),
85 (PrettyPrintParam) props.get(PROP_PRETTY_PRINT),
86 (String) props.get(PROP_NAME_PREFIX), (int) requireNonNull(props.get(PROP_CORE_POOL_SIZE)),
87 (boolean) requireNonNull(props.get(PROP_USE_WEBSOCKETS)));
91 public String restconf() {
96 public HttpServlet newStreamServlet() {
97 return useWebsockets ? new WebSocketInitializer(restconf, streamRegistry, pingExecutor, streamsConfiguration)
98 : servletSupport.createHttpServletBuilder(
101 public Set<Object> getSingletons() {
102 return Set.of(new SSEStreamService(streamRegistry, pingExecutor, streamsConfiguration));
108 public PrettyPrintParam prettyPrint() {
114 public void close() {
115 pingExecutor.close();
118 public static Map<String, ?> props(final String restconf, final RestconfStream.Registry streamRegistry,
119 final PrettyPrintParam prettyPrint, final boolean useSSE, final StreamsConfiguration streamsConfiguration,
120 final String namePrefix, final int corePoolSize) {
122 PROP_RESTCONF, restconf,
123 PROP_STREAM_REGISTRY, streamRegistry,
124 PROP_PRETTY_PRINT, prettyPrint,
125 PROP_USE_WEBSOCKETS, !useSSE,
126 PROP_STREAMS_CONFIGURATION, streamsConfiguration,
127 PROP_NAME_PREFIX, namePrefix,
128 PROP_CORE_POOL_SIZE, corePoolSize);