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