Eliminate AbstractRestconfApplication
[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 org.opendaylight.aaa.filterchain.configuration.CustomFilterAdapterConfiguration;
13 import org.opendaylight.aaa.filterchain.filters.CustomFilterAdapter;
14 import org.opendaylight.aaa.web.FilterDetails;
15 import org.opendaylight.aaa.web.ServletDetails;
16 import org.opendaylight.aaa.web.WebContext;
17 import org.opendaylight.aaa.web.WebContextSecurer;
18 import org.opendaylight.aaa.web.WebServer;
19 import org.opendaylight.aaa.web.servlet.ServletSupport;
20 import org.opendaylight.controller.config.threadpool.util.NamingThreadPoolFactory;
21 import org.opendaylight.controller.config.threadpool.util.ScheduledThreadPoolWrapper;
22 import org.opendaylight.mdsal.dom.api.DOMActionService;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
29 import org.opendaylight.restconf.nb.rfc8040.rests.services.impl.MdsalRestconfServer;
30 import org.opendaylight.restconf.nb.rfc8040.streams.ListenersBroker;
31 import org.opendaylight.restconf.nb.rfc8040.streams.RestconfStreamsConstants;
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 ServletDetails streamServlet;
99         if (streamsConfiguration.useSSE()) {
100             listenersBroker = new ListenersBroker.ServerSentEvents();
101             streamServlet = ServletDetails.builder()
102                 .addUrlPattern("/" + URLConstants.SSE_SUBPATH + "/*")
103                 .servlet(servletSupport.createHttpServletBuilder(
104                     new ServerSentEventsApplication(scheduledThreadPool, listenersBroker, streamsConfiguration))
105                     .build())
106                 .name("notificationServlet")
107                 .asyncSupported(true)
108                 .build();
109         } else {
110             listenersBroker = new ListenersBroker.WebSockets();
111             streamServlet = ServletDetails.builder()
112                 .addUrlPattern("/" + RestconfStreamsConstants.DATA_SUBSCRIPTION + "/*")
113                 .addUrlPattern("/" + RestconfStreamsConstants.NOTIFICATION_STREAM + "/*")
114                 .addUrlPattern("/" + RestconfStreamsConstants.DEVICE_NOTIFICATION_STREAM + "/*")
115                 .servlet(new WebSocketInitializer(scheduledThreadPool, listenersBroker, streamsConfiguration))
116                 .build();
117         }
118
119         final var restconfBuilder = WebContext.builder()
120             .name("RFC8040 RESTCONF")
121             .contextPath("/" + URLConstants.BASE_PATH)
122             .supportsSessions(false)
123             .addServlet(ServletDetails.builder()
124                 .addUrlPattern("/*")
125                 .servlet(servletSupport.createHttpServletBuilder(
126                     new RestconfApplication(databindProvider, server, mountPointService, dataBroker, actionService,
127                         notificationService, schemaService, listenersBroker))
128                     .build())
129                 .asyncSupported(true)
130                 .build())
131             .addServlet(streamServlet)
132
133             // Allows user to add javax.servlet.Filter(s) in front of REST services
134             .addFilter(FilterDetails.builder()
135                 .addUrlPattern("/*")
136                 .filter(new CustomFilterAdapter(filterAdapterConfiguration))
137                 .asyncSupported(true)
138                 .build());
139
140         webContextSecurer.requireAuthentication(restconfBuilder, true, "/*");
141
142         restconfReg = webServer.registerWebContext(restconfBuilder.build());
143
144         final var discoveryBuilder = WebContext.builder()
145             .name("RFC6415 Web Host Metadata")
146             .contextPath("/.well-known")
147             .supportsSessions(false)
148             .addServlet(ServletDetails.builder()
149                 .addUrlPattern("/*")
150                 .servlet(servletSupport.createHttpServletBuilder(new RootFoundApplication(URLConstants.BASE_PATH))
151                     .build())
152                 .name("Rootfound")
153                 .build());
154
155         webContextSecurer.requireAuthentication(discoveryBuilder, true, "/*");
156
157         discoveryReg = webServer.registerWebContext(discoveryBuilder.build());
158     }
159
160     @Deactivate
161     @Override
162     public void close() {
163         discoveryReg.close();
164         restconfReg.close();
165     }
166 }