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