Move RestconfStreamsConstants
[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.rests.services.impl.RestconfDataStreamServiceImpl;
31 import org.opendaylight.restconf.nb.rfc8040.streams.ListenersBroker;
32 import org.opendaylight.restconf.nb.rfc8040.streams.RestconfStreamsConstants;
33 import org.opendaylight.restconf.nb.rfc8040.streams.StreamsConfiguration;
34 import org.opendaylight.restconf.nb.rfc8040.streams.WebSocketInitializer;
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;
43
44 /**
45  * Main entrypoint into RFC8040 northbound. Take care of wiring up all applications activating them through JAX-RS.
46  */
47 @Beta
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;
65         @AttributeDefinition
66         boolean use$_$sse() default true;
67     }
68
69     private final Registration discoveryReg;
70     private final Registration restconfReg;
71
72     @Activate
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, final Configuration configuration) throws ServletException {
81         this(webServer, webContextSecurer, servletSupport, filterAdapterConfiguration, actionService, dataBroker,
82             mountPointService, notificationService, rpcService, schemaService, databindProvider, server,
83             configuration.ping$_$executor$_$name$_$prefix(), configuration.max$_$thread$_$count(),
84             new StreamsConfiguration(configuration.maximum$_$fragment$_$length(),
85                 configuration.idle$_$timeout(), configuration.heartbeat$_$interval(), configuration.use$_$sse()));
86     }
87
88     public JaxRsNorthbound(final WebServer webServer, final WebContextSecurer webContextSecurer,
89             final ServletSupport servletSupport, final CustomFilterAdapterConfiguration filterAdapterConfiguration,
90             final DOMActionService actionService, final DOMDataBroker dataBroker,
91             final DOMMountPointService mountPointService, final DOMNotificationService notificationService,
92             final DOMRpcService rpcService, final DOMSchemaService schemaService,
93             final DatabindProvider databindProvider, final MdsalRestconfServer server, final String pingNamePrefix,
94             final int pingMaxThreadCount, final StreamsConfiguration streamsConfiguration) throws ServletException {
95         final var scheduledThreadPool = new ScheduledThreadPoolWrapper(pingMaxThreadCount,
96             new NamingThreadPoolFactory(pingNamePrefix));
97
98         final ListenersBroker listenersBroker;
99         final ServletDetails streamServlet;
100         if (streamsConfiguration.useSSE()) {
101             listenersBroker = new ListenersBroker.ServerSentEvents();
102             streamServlet = ServletDetails.builder()
103                 .addUrlPattern("/" + URLConstants.SSE_SUBPATH + "/*")
104                 .servlet(servletSupport.createHttpServletBuilder(
105                     new DataStreamApplication(databindProvider,
106                         new RestconfDataStreamServiceImpl(scheduledThreadPool, listenersBroker, streamsConfiguration)))
107                     .build())
108                 .name("notificationServlet")
109                 .asyncSupported(true)
110                 .build();
111         } else {
112             listenersBroker = new ListenersBroker.WebSockets();
113             streamServlet = ServletDetails.builder()
114                 .addUrlPattern("/" + RestconfStreamsConstants.DATA_SUBSCRIPTION + "/*")
115                 .addUrlPattern("/" + RestconfStreamsConstants.NOTIFICATION_STREAM + "/*")
116                 .addUrlPattern("/" + RestconfStreamsConstants.DEVICE_NOTIFICATION_STREAM + "/*")
117                 .servlet(new WebSocketInitializer(scheduledThreadPool, listenersBroker, streamsConfiguration))
118                 .build();
119         }
120
121         final var restconfBuilder = WebContext.builder()
122             .name("RFC8040 RESTCONF")
123             .contextPath("/" + URLConstants.BASE_PATH)
124             .supportsSessions(false)
125             .addServlet(ServletDetails.builder()
126                 .addUrlPattern("/*")
127                 .servlet(servletSupport.createHttpServletBuilder(
128                     new RestconfApplication(databindProvider, server, mountPointService, dataBroker, actionService,
129                         notificationService, schemaService, listenersBroker))
130                     .build())
131                 .asyncSupported(true)
132                 .build())
133             .addServlet(streamServlet)
134
135             // Allows user to add javax.servlet.Filter(s) in front of REST services
136             .addFilter(FilterDetails.builder()
137                 .addUrlPattern("/*")
138                 .filter(new CustomFilterAdapter(filterAdapterConfiguration))
139                 .asyncSupported(true)
140                 .build());
141
142         webContextSecurer.requireAuthentication(restconfBuilder, true, "/*");
143
144         restconfReg = webServer.registerWebContext(restconfBuilder.build());
145
146         final var discoveryBuilder = WebContext.builder()
147             .name("RFC6415 Web Host Metadata")
148             .contextPath("/.well-known")
149             .supportsSessions(false)
150             .addServlet(ServletDetails.builder()
151                 .addUrlPattern("/*")
152                 .servlet(servletSupport.createHttpServletBuilder(new RootFoundApplication(URLConstants.BASE_PATH))
153                     .build())
154                 .name("Rootfound")
155                 .build());
156
157         webContextSecurer.requireAuthentication(discoveryBuilder, true, "/*");
158
159         discoveryReg = webServer.registerWebContext(discoveryBuilder.build());
160     }
161
162     @Deactivate
163     @Override
164     public void close() {
165         discoveryReg.close();
166         restconfReg.close();
167     }
168 }