Turn streams.Configuration into a record
[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.rests.utils.RestconfStreamsConstants.DATA_SUBSCRIPTION;
11 import static org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants.NOTIFICATION_STREAM;
12 import static org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants.BASE_URI_PATTERN;
13 import static org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants.NOTIF;
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
39 /**
40  * Main entrypoint into RFC8040 northbound. Take care of wiring up all applications activating them through JAX-RS.
41  */
42 @Beta
43 public final class JaxRsNorthbound implements AutoCloseable {
44     private final Registration discoveryReg;
45     private final Registration restconfReg;
46
47     public JaxRsNorthbound(final WebServer webServer, final WebContextSecurer webContextSecurer,
48             final ServletSupport servletSupport, final CustomFilterAdapterConfiguration filterAdapterConfiguration,
49             final DOMActionService actionService, final DOMDataBroker dataBroker,
50             final DOMMountPointService mountPointService, final DOMNotificationService notificationService,
51             final DOMRpcService rpcService, final DOMSchemaService schemaService,
52             final DatabindProvider databindProvider,
53             final String pingNamePrefix, final int pingMaxThreadCount, final int maximumFragmentLength,
54             final int heartbeatInterval, final int idleTimeout, final boolean useSSE) throws ServletException {
55         final var streamsConfiguration = new StreamsConfiguration(maximumFragmentLength, idleTimeout, heartbeatInterval,
56             useSSE);
57         final var scheduledThreadPool = new ScheduledThreadPoolWrapper(pingMaxThreadCount,
58             new NamingThreadPoolFactory(pingNamePrefix));
59
60         final var restconfBuilder = WebContext.builder()
61             .name("RFC8040 RESTCONF")
62             .contextPath("/" + BASE_URI_PATTERN)
63             .supportsSessions(false)
64             .addServlet(ServletDetails.builder()
65                 .addUrlPattern("/*")
66                 .servlet(servletSupport.createHttpServletBuilder(
67                     new RestconfApplication(databindProvider, mountPointService, dataBroker, rpcService, actionService,
68                         notificationService,schemaService, streamsConfiguration)).build())
69                 .asyncSupported(true)
70                 .build())
71             .addServlet(ServletDetails.builder()
72                 .addUrlPattern("/" + NOTIF + "/*")
73                 .servlet(servletSupport.createHttpServletBuilder(
74                     new DataStreamApplication(databindProvider, mountPointService,
75                         new RestconfDataStreamServiceImpl(scheduledThreadPool, streamsConfiguration))).build())
76                 .name("notificationServlet")
77                 .asyncSupported(true)
78                 .build())
79             .addServlet(ServletDetails.builder()
80                 .addUrlPattern("/" + DATA_SUBSCRIPTION + "/*")
81                 .addUrlPattern("/" + NOTIFICATION_STREAM + "/*")
82                 .servlet(new WebSocketInitializer(scheduledThreadPool, streamsConfiguration))
83                 .build())
84
85             // Allows user to add javax.servlet.Filter(s) in front of REST services
86             .addFilter(FilterDetails.builder()
87                 .addUrlPattern("/*")
88                 .filter(new CustomFilterAdapter(filterAdapterConfiguration))
89                 .asyncSupported(true)
90                 .build());
91
92         webContextSecurer.requireAuthentication(restconfBuilder, true, "/*");
93
94         restconfReg = webServer.registerWebContext(restconfBuilder.build());
95
96         final var discoveryBuilder = WebContext.builder()
97             .name("RFC6415 Web Host Metadata")
98             .contextPath("/.well-known")
99             .supportsSessions(false)
100             .addServlet(ServletDetails.builder()
101                 .addUrlPattern("/*")
102                 .servlet(servletSupport.createHttpServletBuilder(new RootFoundApplication(BASE_URI_PATTERN)).build())
103                 .name("Rootfound")
104                 .build());
105
106         webContextSecurer.requireAuthentication(discoveryBuilder, true, "/*");
107
108         discoveryReg = webServer.registerWebContext(discoveryBuilder.build());
109     }
110
111     @Override
112     public void close() {
113         discoveryReg.close();
114         restconfReg.close();
115     }
116 }