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