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