Register only one events servlet 60/108760/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Oct 2023 22:02:34 +0000 (23:02 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Oct 2023 22:20:12 +0000 (23:20 +0100)
SubscribeToStreamUtil and the corresponding servlet are strongly
related -- the former produces references to the latter.

Express this dependency by allocating both object in the same place and
pass them to their respective users.

This has the nice end result that we do not enable WS servlet when
configured to use SSE and vice-versa -- making it clear the two belong
together and paving the way towards making the two proper components.

Change-Id: I42b0878fa9c9ac4e4b5800bfde8719134a409c7a
JIRA: NETCONF-1102
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/JaxRsNorthbound.java
restconf/restconf-nb/src/main/java/org/opendaylight/restconf/nb/rfc8040/RestconfApplication.java

index 3a0d25815ba2762425220637fe9535841044b461..2042e29d061d8e6d5a623f405c6d6df056bf6139 100644 (file)
@@ -28,6 +28,7 @@ import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.MdsalRestconfServer;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.RestconfDataStreamServiceImpl;
+import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.SubscribeToStreamUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
 import org.opendaylight.restconf.nb.rfc8040.streams.WebSocketInitializer;
@@ -97,19 +98,11 @@ public final class JaxRsNorthbound implements AutoCloseable {
         final var scheduledThreadPool = new ScheduledThreadPoolWrapper(pingMaxThreadCount,
             new NamingThreadPoolFactory(pingNamePrefix));
 
-        final var restconfBuilder = WebContext.builder()
-            .name("RFC8040 RESTCONF")
-            .contextPath("/" + URLConstants.BASE_PATH)
-            .supportsSessions(false)
-            .addServlet(ServletDetails.builder()
-                .addUrlPattern("/*")
-                .servlet(servletSupport.createHttpServletBuilder(
-                    new RestconfApplication(databindProvider, server, mountPointService, dataBroker, rpcService,
-                        actionService, notificationService, schemaService, listenersBroker, streamsConfiguration))
-                    .build())
-                .asyncSupported(true)
-                .build())
-            .addServlet(ServletDetails.builder()
+        final SubscribeToStreamUtil streamUtils;
+        final ServletDetails streamServlet;
+        if (streamsConfiguration.useSSE()) {
+            streamUtils = SubscribeToStreamUtil.serverSentEvents(listenersBroker);
+            streamServlet = ServletDetails.builder()
                 .addUrlPattern("/" + URLConstants.SSE_SUBPATH + "/*")
                 .servlet(servletSupport.createHttpServletBuilder(
                     new DataStreamApplication(databindProvider,
@@ -117,13 +110,30 @@ public final class JaxRsNorthbound implements AutoCloseable {
                     .build())
                 .name("notificationServlet")
                 .asyncSupported(true)
-                .build())
-            .addServlet(ServletDetails.builder()
+                .build();
+        } else {
+            streamUtils = SubscribeToStreamUtil.webSockets(listenersBroker);
+            streamServlet = ServletDetails.builder()
                 .addUrlPattern("/" + RestconfStreamsConstants.DATA_SUBSCRIPTION + "/*")
                 .addUrlPattern("/" + RestconfStreamsConstants.NOTIFICATION_STREAM + "/*")
                 .addUrlPattern("/" + RestconfStreamsConstants.DEVICE_NOTIFICATION_STREAM + "/*")
                 .servlet(new WebSocketInitializer(scheduledThreadPool, listenersBroker, streamsConfiguration))
+                .build();
+        }
+
+        final var restconfBuilder = WebContext.builder()
+            .name("RFC8040 RESTCONF")
+            .contextPath("/" + URLConstants.BASE_PATH)
+            .supportsSessions(false)
+            .addServlet(ServletDetails.builder()
+                .addUrlPattern("/*")
+                .servlet(servletSupport.createHttpServletBuilder(
+                    new RestconfApplication(databindProvider, server, mountPointService, dataBroker, actionService,
+                        notificationService, schemaService, streamUtils))
+                    .build())
+                .asyncSupported(true)
                 .build())
+            .addServlet(streamServlet)
 
             // Allows user to add javax.servlet.Filter(s) in front of REST services
             .addFilter(FilterDetails.builder()
index 245ad33b55733c50323cf8e2c25228e491888e4a..d9fe05a4e7ccd87c729e02ceec3eddded7e8cfc2 100644 (file)
@@ -14,7 +14,6 @@ import org.opendaylight.mdsal.dom.api.DOMActionService;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
-import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
@@ -26,8 +25,6 @@ import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.RestconfOperatio
 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.RestconfSchemaServiceImpl;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.RestconfStreamsSubscriptionServiceImpl;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.SubscribeToStreamUtil;
-import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
-import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
 
 @Singleton
 public class RestconfApplication extends AbstractRestconfApplication {
@@ -45,7 +42,8 @@ public class RestconfApplication extends AbstractRestconfApplication {
             new RestconfImpl(databindProvider)));
     }
 
-    private RestconfApplication(final DatabindProvider databindProvider, final MdsalRestconfServer server,
+    @Inject
+    public RestconfApplication(final DatabindProvider databindProvider, final MdsalRestconfServer server,
             final DOMMountPointService mountPointService, final DOMDataBroker dataBroker,
             final DOMActionService actionService, final DOMNotificationService notificationService,
             final DOMSchemaService domSchemaService, final SubscribeToStreamUtil streamUtils) {
@@ -53,15 +51,4 @@ public class RestconfApplication extends AbstractRestconfApplication {
             new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService, databindProvider, streamUtils),
             dataBroker, actionService, notificationService, domSchemaService, streamUtils);
     }
-
-    @Inject
-    public RestconfApplication(final DatabindProvider databindProvider, final MdsalRestconfServer server,
-            final DOMMountPointService mountPointService, final DOMDataBroker dataBroker,
-            final DOMRpcService rpcService, final DOMActionService actionService,
-            final DOMNotificationService notificationService, final DOMSchemaService domSchemaService,
-            final ListenersBroker listenersBroker, final StreamsConfiguration configuration) {
-        this(databindProvider, server, mountPointService, dataBroker, actionService, notificationService,
-            domSchemaService, configuration.useSSE() ? SubscribeToStreamUtil.serverSentEvents(listenersBroker)
-                : SubscribeToStreamUtil.webSockets(listenersBroker));
-    }
 }