Skip to content

Commit 6816ac9

Browse files
committed
Run restconf using JaxRsEndpoint
There is no need to create restconf manually anymore thanks to JaxRsEndpoint. This patch relies on the previous JettyWebServerProvider patch since JaxRsEndpoint requires JettyWebServer instead of regular server. Now initialization order matters, and restconf has to be initialized after lighty services. JIRA: LIGHTY-329 Signed-off-by: tobias.pobocik <[email protected]>
1 parent 4964d5f commit 6816ac9

File tree

4 files changed

+63
-87
lines changed
  • lighty-applications/lighty-rcgnmi-app-aggregator/lighty-rcgnmi-app-module/src/main/java/io/lighty/applications/rcgnmi/module
  • lighty-examples/lighty-community-aaa-restconf-app/src/main/java/io/lighty/kit/examples/community/aaa/restconf
  • lighty-modules/lighty-restconf-nb-community/src/main/java/io/lighty/modules/northbound/restconf/community/impl

4 files changed

+63
-87
lines changed

lighty-applications/lighty-rcgnmi-app-aggregator/lighty-rcgnmi-app-module/src/main/java/io/lighty/applications/rcgnmi/module/RcGnmiAppModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ public boolean initModules() {
8282

8383
this.lightyRestconf = initRestconf(this.appModuleConfig.getRestconfConfig(),
8484
this.lightyController.getServices());
85+
lightyController.getServices().withJaxRsEndpoint(lightyRestconf.getJaxRsEndpoint());
8586
startAndWaitLightyModule(this.lightyRestconf);
8687

8788
final AAAEncryptionService encryptionService = createEncryptionServiceWithErrorHandling();
8889
this.gnmiSouthboundModule = initGnmiModule(this.lightyController.getServices(),
8990
this.gnmiExecutorService, this.appModuleConfig.getGnmiConfiguration(), encryptionService,
9091
this.customReactor);
9192
startAndWaitLightyModule(this.gnmiSouthboundModule);
93+
lightyRestconf.startServer();
9294

9395
} catch (RcGnmiAppException e) {
9496
LOG.error("Unable to initialize and start RCgNMI lighty.io module!", e);

lighty-examples/lighty-community-aaa-restconf-app/src/main/java/io/lighty/kit/examples/community/aaa/restconf/Main.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -107,21 +107,10 @@ private void startLighty(final ControllerConfiguration controllerConfiguration,
107107
throw new ModuleStartupException("Lighty.io Controller startup failed!");
108108
}
109109

110-
// 2. Initialize and start Restconf server
111110
final LightyJettyServerProvider jettyServerBuilder = new LightyJettyServerProvider(new InetSocketAddress(
112111
restconfConfiguration.getInetAddress(), restconfConfiguration.getHttpPort()));
113-
this.restconf = CommunityRestConfBuilder
114-
.from(RestConfConfigUtils.getRestConfConfiguration(restconfConfiguration,
115-
this.lightyController.getServices()))
116-
.withLightyServer(jettyServerBuilder)
117-
.build();
118-
final boolean restconfStartOk = this.restconf.start()
119-
.get(modulesConfig.getModuleTimeoutSeconds(), TimeUnit.SECONDS);
120-
if (!restconfStartOk) {
121-
throw new ModuleStartupException("Community Restconf startup failed!");
122-
}
123112

124-
// 3. Initialize and start Lighty AAA
113+
// 2. Initialize and start Lighty AAA
125114
final DataBroker bindingDataBroker = this.lightyController.getServices().getBindingDataBroker();
126115
Security.addProvider(new BouncyCastleProvider());
127116
aaaConfiguration.setCertificateManager(
@@ -134,6 +123,21 @@ private void startLighty(final ControllerConfiguration controllerConfiguration,
134123
throw new ModuleStartupException("AAA module startup failed!");
135124
}
136125

126+
// 3. Initialize and start Restconf server
127+
this.restconf = CommunityRestConfBuilder
128+
.from(RestConfConfigUtils.getRestConfConfiguration(restconfConfiguration,
129+
this.lightyController.getServices()))
130+
.withLightyServer(jettyServerBuilder)
131+
.withWebSecurer(aaaLighty.getWebContextSecurer())
132+
.build();
133+
final boolean restconfStartOk = this.restconf.start()
134+
.get(modulesConfig.getModuleTimeoutSeconds(), TimeUnit.SECONDS);
135+
if (!restconfStartOk) {
136+
throw new ModuleStartupException("Community Restconf startup failed!");
137+
}
138+
139+
// 4. Start Lighty jetty server
140+
this.restconf.startServer();
137141
}
138142

139143
private void closeLightyModule(final LightyModule module) {

lighty-modules/lighty-restconf-nb-community/src/main/java/io/lighty/modules/northbound/restconf/community/impl/CommunityRestConf.java

Lines changed: 28 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,11 @@
1010
import com.google.common.base.Stopwatch;
1111
import com.google.common.base.Throwables;
1212
import io.lighty.core.controller.api.AbstractLightyModule;
13-
import io.lighty.modules.northbound.restconf.community.impl.root.resource.discovery.RootFoundApplication;
1413
import io.lighty.modules.northbound.restconf.community.impl.util.RestConfConfigUtils;
14+
import io.lighty.server.LightyJettyServerProvider;
1515
import java.net.InetAddress;
1616
import java.net.InetSocketAddress;
17-
import java.util.Set;
18-
import io.lighty.server.LightyJettyServerProvider;
1917
import javax.servlet.ServletException;
20-
import javax.ws.rs.core.Application;
21-
import org.eclipse.jetty.server.Server;
22-
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
23-
import org.eclipse.jetty.servlet.ServletContextHandler;
24-
import org.eclipse.jetty.servlet.ServletHolder;
25-
import org.glassfish.jersey.server.ResourceConfig;
26-
import org.glassfish.jersey.servlet.ServletContainer;
2718
import org.opendaylight.aaa.filterchain.configuration.impl.CustomFilterAdapterConfigurationImpl;
2819
import org.opendaylight.aaa.web.WebContext;
2920
import org.opendaylight.aaa.web.WebContextSecurer;
@@ -32,20 +23,14 @@
3223
import org.opendaylight.mdsal.dom.api.DOMActionService;
3324
import org.opendaylight.mdsal.dom.api.DOMDataBroker;
3425
import org.opendaylight.mdsal.dom.api.DOMMountPointService;
35-
import org.opendaylight.mdsal.dom.api.DOMNotificationService;
3626
import org.opendaylight.mdsal.dom.api.DOMRpcService;
3727
import org.opendaylight.mdsal.dom.api.DOMSchemaService;
38-
import org.opendaylight.restconf.api.query.PrettyPrintParam;
3928
import org.opendaylight.restconf.server.jaxrs.JaxRsEndpoint;
4029
import org.opendaylight.restconf.server.jaxrs.JaxRsEndpointConfiguration;
4130
import org.opendaylight.restconf.server.jaxrs.JaxRsLocationProvider;
42-
import org.opendaylight.restconf.server.jaxrs.JaxRsRestconf;
43-
import org.opendaylight.restconf.server.jaxrs.JsonJaxRsFormattableBodyWriter;
44-
import org.opendaylight.restconf.server.jaxrs.XmlJaxRsFormattableBodyWriter;
4531
import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
4632
import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
4733
import org.opendaylight.restconf.server.mdsal.MdsalRestconfStreamRegistry;
48-
import org.opendaylight.restconf.server.spi.ErrorTagMapping;
4934
import org.slf4j.Logger;
5035
import org.slf4j.LoggerFactory;
5136

@@ -55,43 +40,41 @@ public class CommunityRestConf extends AbstractLightyModule {
5540

5641
private final DOMDataBroker domDataBroker;
5742
private final DOMRpcService domRpcService;
58-
private final DOMNotificationService domNotificationService;
5943
private final DOMMountPointService domMountPointService;
6044
private final DOMActionService domActionService;
6145
private final DOMSchemaService domSchemaService;
62-
private final int httpPort;
6346
private final InetAddress inetAddress;
64-
private final String restconfServletContextPath;
47+
private final int httpPort;
6548
private JettyWebServer jettyServer;
6649
private LightyJettyServerProvider lightyServerBuilder;
6750
private JaxRsEndpoint jaxRsEndpoint;
51+
private WebContextSecurer webContextSecurer;
6852

6953
public CommunityRestConf(final DOMDataBroker domDataBroker, final DOMRpcService domRpcService,
70-
final DOMActionService domActionService, final DOMNotificationService domNotificationService,
54+
final DOMActionService domActionService,
7155
final DOMMountPointService domMountPointService,
72-
final DOMSchemaService domSchemaService, final InetAddress inetAddress,
73-
final int httpPort, final String restconfServletContextPath,
74-
final LightyJettyServerProvider serverBuilder) {
56+
final DOMSchemaService domSchemaService,
57+
final InetAddress inetAddress,
58+
final int httpPort,
59+
final LightyJettyServerProvider serverBuilder,
60+
final WebContextSecurer webContextSecurer) {
7561
this.domDataBroker = domDataBroker;
7662
this.domRpcService = domRpcService;
7763
this.domActionService = domActionService;
78-
this.domNotificationService = domNotificationService;
7964
this.domMountPointService = domMountPointService;
8065
this.lightyServerBuilder = serverBuilder;
8166
this.domSchemaService = domSchemaService;
82-
this.httpPort = httpPort;
8367
this.inetAddress = inetAddress;
84-
this.restconfServletContextPath = restconfServletContextPath;
68+
this.httpPort = httpPort;
69+
this.webContextSecurer = (webContextSecurer == null) ? new LightyWebContextSecurer() : webContextSecurer;
8570
}
8671

8772
public CommunityRestConf(final DOMDataBroker domDataBroker,
8873
final DOMRpcService domRpcService, final DOMActionService domActionService,
89-
final DOMNotificationService domNotificationService, final DOMMountPointService domMountPointService,
90-
final DOMSchemaService domSchemaService, final InetAddress inetAddress, final int httpPort,
91-
final String restconfServletContextPath) {
92-
this(domDataBroker, domRpcService, domActionService, domNotificationService,
93-
domMountPointService, domSchemaService, inetAddress, httpPort,
94-
restconfServletContextPath, null);
74+
final DOMMountPointService domMountPointService, final DOMSchemaService domSchemaService,
75+
final InetAddress inetAddress, final int httpPort, final WebContextSecurer webContextSecurer) {
76+
this(domDataBroker, domRpcService, domActionService,
77+
domMountPointService, domSchemaService, inetAddress, httpPort, null, webContextSecurer);
9578
}
9679

9780
@Override
@@ -101,48 +84,24 @@ protected boolean initProcedure() throws ServletException {
10184

10285
LOG.info("Starting RestconfApplication with configuration {}", streamsConfiguration);
10386

104-
if (lightyServerBuilder == null){
105-
lightyServerBuilder = new LightyJettyServerProvider(new JettyWebServer());
87+
if (lightyServerBuilder == null) {
88+
lightyServerBuilder = new LightyJettyServerProvider(new InetSocketAddress(inetAddress, httpPort));
10689
}
10790

10891
final MdsalDatabindProvider databindProvider = new MdsalDatabindProvider(domSchemaService);
109-
final var server = new MdsalRestconfServer(databindProvider, domDataBroker, domRpcService, domActionService,
110-
domMountPointService);
92+
final var server = new MdsalRestconfServer(databindProvider, domDataBroker, domRpcService,
93+
domActionService, domMountPointService);
11194

11295
this.jettyServer = this.lightyServerBuilder.build();
113-
this.jaxRsEndpoint = new JaxRsEndpoint(new JettyWebServer(httpPort), new LightyWebContextSecurer(),
114-
new JerseyServletSupport(), new CustomFilterAdapterConfigurationImpl(), server,
96+
this.jaxRsEndpoint = new JaxRsEndpoint(
97+
jettyServer,
98+
this.webContextSecurer,
99+
new JerseyServletSupport(),
100+
new CustomFilterAdapterConfigurationImpl(),
101+
server,
115102
new MdsalRestconfStreamRegistry(new JaxRsLocationProvider(), domDataBroker),
116-
JaxRsEndpoint.props(streamsConfiguration));
117-
118-
final ServletContainer servletContainer8040 = new ServletContainer(ResourceConfig
119-
.forApplication(new Application() {
120-
@Override
121-
public Set<Object> getSingletons() {
122-
return Set.of(
123-
new JsonJaxRsFormattableBodyWriter(), new XmlJaxRsFormattableBodyWriter(),
124-
new JaxRsRestconf(server, new MdsalRestconfStreamRegistry(new JaxRsLocationProvider(),
125-
domDataBroker), jaxRsEndpoint, ErrorTagMapping.RFC8040, PrettyPrintParam.FALSE));
126-
}
127-
}));
128-
129-
final ServletHolder jaxrs = new ServletHolder(servletContainer8040);
130-
131-
LOG.info("RestConf init complete, starting Jetty");
132-
LOG.info("http address:port {}:{}, url prefix: {}", this.inetAddress.toString(), this.httpPort,
133-
this.restconfServletContextPath);
134-
final InetSocketAddress inetSocketAddress = new InetSocketAddress(this.inetAddress, this.httpPort);
135-
final ContextHandlerCollection contexts = new ContextHandlerCollection();
136-
final ServletContextHandler mainHandler =
137-
new ServletContextHandler(contexts, this.restconfServletContextPath, true, false);
138-
mainHandler.addServlet(jaxrs, "/*");
139-
140-
final ServletContextHandler rrdHandler =
141-
new ServletContextHandler(contexts, "/.well-known", true, false);
142-
final RootFoundApplication rootDiscoveryApp = new RootFoundApplication(restconfServletContextPath);
143-
rrdHandler.addServlet(new ServletHolder(new ServletContainer(ResourceConfig
144-
.forApplication(rootDiscoveryApp))), "/*");
145-
103+
JaxRsEndpoint.props(streamsConfiguration)
104+
);
146105

147106
LOG.info("Lighty RestConf started in {}", stopwatch.stop());
148107
return true;
@@ -196,7 +155,7 @@ public static class LightyWebContextSecurer implements WebContextSecurer {
196155
@Override
197156
public void requireAuthentication(WebContext.Builder webContextBuilder,
198157
boolean asyncSupported, String... urlPatterns) {
199-
158+
//do nothing since shiro is not used
200159
}
201160

202161
@Override

lighty-modules/lighty-restconf-nb-community/src/main/java/io/lighty/modules/northbound/restconf/community/impl/CommunityRestConfBuilder.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import io.lighty.modules.northbound.restconf.community.impl.config.RestConfConfiguration;
1111
import io.lighty.server.LightyJettyServerProvider;
12+
import org.opendaylight.aaa.web.WebContextSecurer;
1213

1314
/**
1415
* Builder for {@link CommunityRestConf}.
@@ -17,6 +18,7 @@ public final class CommunityRestConfBuilder {
1718

1819
private RestConfConfiguration restconfConfiguration = null;
1920
private LightyJettyServerProvider lightyServerBuilder = null;
21+
private WebContextSecurer webContextSecurer;
2022

2123

2224
private CommunityRestConfBuilder(final RestConfConfiguration configuration) {
@@ -44,6 +46,17 @@ public CommunityRestConfBuilder withLightyServer(final LightyJettyServerProvider
4446
return this;
4547
}
4648

49+
/**
50+
* Inject lighty web securer.
51+
*
52+
* @param webSecurer input web securer (can be null for no auth).
53+
* @return instance of {@link CommunityRestConfBuilder}.
54+
*/
55+
public CommunityRestConfBuilder withWebSecurer(final WebContextSecurer webSecurer) {
56+
this.webContextSecurer = webSecurer;
57+
return this;
58+
}
59+
4760
/**
4861
* Add ScheduledThreadPool.
4962
*
@@ -57,11 +70,9 @@ public CommunityRestConfBuilder withLightyServer(final LightyJettyServerProvider
5770
*/
5871
public CommunityRestConf build() {
5972
return new CommunityRestConf(this.restconfConfiguration.getDomDataBroker(),
60-
this.restconfConfiguration.getDomRpcService(),
61-
this.restconfConfiguration.getDomActionService(), this.restconfConfiguration.getDomNotificationService(),
62-
this.restconfConfiguration.getDomMountPointService(),
63-
this.restconfConfiguration.getDomSchemaService(),
64-
this.restconfConfiguration.getInetAddress(), this.restconfConfiguration.getHttpPort(),
65-
this.restconfConfiguration.getRestconfServletContextPath(), this.lightyServerBuilder);
73+
this.restconfConfiguration.getDomRpcService(), this.restconfConfiguration.getDomActionService(),
74+
this.restconfConfiguration.getDomMountPointService(), this.restconfConfiguration.getDomSchemaService(),
75+
this.restconfConfiguration.getInetAddress(),this.restconfConfiguration.getHttpPort(),
76+
this.lightyServerBuilder, this.webContextSecurer);
6677
}
6778
}

0 commit comments

Comments
 (0)