Expose streams with all supported encodings
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / JaxRsNorthbound.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040;
9
10 import com.google.common.annotations.Beta;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
14 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
15 import org.opendaylight.aaa.web.FilterDetails;
16 import org.opendaylight.aaa.web.ServletDetails;
17 import org.opendaylight.aaa.web.WebContext;
18 import org.opendaylight.aaa.web.WebContextSecurer;
19 import org.opendaylight.aaa.web.WebServer;
20 import org.opendaylight.aaa.web.servlet.ServletSupport;
21 import org.opendaylight.controller.config.threadpool.util.NamingThreadPoolFactory;
22 import org.opendaylight.controller.config.threadpool.util.ScheduledThreadPoolWrapper;
23 import org.opendaylight.mdsal.dom.api.DOMActionService;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
26 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
27 import org.opendaylight.mdsal.dom.api.DOMRpcService;
28 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
29 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
30 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.MdsalRestconfServer;
31 import org.opendaylight.restconf.nb.rfc8040.streams.ListenersBroker;
32 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
33 import org.opendaylight.restconf.nb.rfc8040.streams.WebSocketInitializer;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Deactivate;
38 import org.osgi.service.component.annotations.Reference;
39 import org.osgi.service.metatype.annotations.AttributeDefinition;
40 import org.osgi.service.metatype.annotations.Designate;
41 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
42
43 /**
44  * Main entrypoint into RFC8040 northbound. Take care of wiring up all applications activating them through JAX-RS.
45  */
46 @Beta
47 @Component(service = { }, configurationPid = "org.opendaylight.restconf.nb.rfc8040")
48 @Designate(ocd = JaxRsNorthbound.Configuration.class)
49 public final class JaxRsNorthbound implements AutoCloseable {
50     @ObjectClassDefinition
51     public @interface Configuration {
52         @AttributeDefinition(min = "0", max = "" + StreamsConfiguration.MAXIMUM_FRAGMENT_LENGTH_LIMIT)
53         int maximum$_$fragment$_$length() default 0;
54         @AttributeDefinition(min = "0")
55         int heartbeat$_$interval() default 10000;
56         @AttributeDefinition(min = "1")
57         int idle$_$timeout() default 30000;
58         @AttributeDefinition(min = "1")
59         String ping$_$executor$_$name$_$prefix() default "ping-executor";
60         // FIXME: this is a misnomer: it specifies the core pool size, i.e. minimum thread count, the maximum is set to
61         //        Integer.MAX_VALUE, which is not what we want
62         @AttributeDefinition(min = "0")
63         int max$_$thread$_$count() default 1;
64         @AttributeDefinition
65         boolean use$_$sse() default true;
66     }
67
68     private final Registration discoveryReg;
69     private final Registration restconfReg;
70
71     @Activate
72     public JaxRsNorthbound(@Reference final WebServer webServer, @Reference final WebContextSecurer webContextSecurer,
73             @Reference final ServletSupport servletSupport,
74             @Reference final CustomFilterAdapterConfiguration filterAdapterConfiguration,
75             @Reference final DOMActionService actionService, @Reference final DOMDataBroker dataBroker,
76             @Reference final DOMMountPointService mountPointService,
77             @Reference final DOMNotificationService notificationService, @Reference final DOMRpcService rpcService,
78             @Reference final DOMSchemaService schemaService, @Reference final DatabindProvider databindProvider,
79             @Reference final MdsalRestconfServer server, final Configuration configuration) throws ServletException {
80         this(webServer, webContextSecurer, servletSupport, filterAdapterConfiguration, actionService, dataBroker,
81             mountPointService, notificationService, rpcService, schemaService, databindProvider, server,
82             configuration.ping$_$executor$_$name$_$prefix(), configuration.max$_$thread$_$count(),
83             new StreamsConfiguration(configuration.maximum$_$fragment$_$length(),
84                 configuration.idle$_$timeout(), configuration.heartbeat$_$interval(), configuration.use$_$sse()));
85     }
86
87     public JaxRsNorthbound(final WebServer webServer, final WebContextSecurer webContextSecurer,
88             final ServletSupport servletSupport, final CustomFilterAdapterConfiguration filterAdapterConfiguration,
89             final DOMActionService actionService, final DOMDataBroker dataBroker,
90             final DOMMountPointService mountPointService, final DOMNotificationService notificationService,
91             final DOMRpcService rpcService, final DOMSchemaService schemaService,
92             final DatabindProvider databindProvider, final MdsalRestconfServer server, final String pingNamePrefix,
93             final int pingMaxThreadCount, final StreamsConfiguration streamsConfiguration) throws ServletException {
94         final var scheduledThreadPool = new ScheduledThreadPoolWrapper(pingMaxThreadCount,
95             new NamingThreadPoolFactory(pingNamePrefix));
96
97         final ListenersBroker listenersBroker;
98         final HttpServlet streamServlet;
99         if (streamsConfiguration.useSSE()) {
100             listenersBroker = new ListenersBroker.ServerSentEvents(dataBroker, notificationService, mountPointService);
101             streamServlet = servletSupport.createHttpServletBuilder(
102                 new ServerSentEventsApplication(scheduledThreadPool, listenersBroker, streamsConfiguration))
103                 .build();
104         } else {
105             listenersBroker = new ListenersBroker.WebSockets(dataBroker, notificationService, mountPointService);
106             streamServlet = new WebSocketInitializer(scheduledThreadPool, listenersBroker, streamsConfiguration);
107         }
108
109         final var restconfBuilder = WebContext.builder()
110             .name("RFC8040 RESTCONF")
111             .contextPath("/" + URLConstants.BASE_PATH)
112             .supportsSessions(false)
113             .addServlet(ServletDetails.builder()
114                 .addUrlPattern("/*")
115                 .servlet(servletSupport.createHttpServletBuilder(
116                     new RestconfApplication(databindProvider, server, mountPointService, dataBroker, actionService,
117                         notificationService, schemaService, listenersBroker))
118                     .build())
119                 .asyncSupported(true)
120                 .build())
121             .addServlet(ServletDetails.builder()
122                 .addUrlPattern("/" + URLConstants.STREAMS_SUBPATH + "/*")
123                 .servlet(streamServlet)
124                 .name("notificationServlet")
125                 .asyncSupported(true)
126                 .build())
127
128             // Allows user to add javax.servlet.Filter(s) in front of REST services
129             .addFilter(FilterDetails.builder()
130                 .addUrlPattern("/*")
131                 .filter(new CustomFilterAdapter(filterAdapterConfiguration))
132                 .asyncSupported(true)
133                 .build());
134
135         webContextSecurer.requireAuthentication(restconfBuilder, true, "/*");
136
137         restconfReg = webServer.registerWebContext(restconfBuilder.build());
138
139         final var discoveryBuilder = WebContext.builder()
140             .name("RFC6415 Web Host Metadata")
141             .contextPath("/.well-known")
142             .supportsSessions(false)
143             .addServlet(ServletDetails.builder()
144                 .addUrlPattern("/*")
145                 .servlet(servletSupport.createHttpServletBuilder(new RootFoundApplication(URLConstants.BASE_PATH))
146                     .build())
147                 .name("Rootfound")
148                 .build());
149
150         webContextSecurer.requireAuthentication(discoveryBuilder, true, "/*");
151
152         discoveryReg = webServer.registerWebContext(discoveryBuilder.build());
153     }
154
155     @Deactivate
156     @Override
157     public void close() {
158         discoveryReg.close();
159         restconfReg.close();
160     }
161 }