From 5f161c233c684854cb5634be323eb29e13f176ae Mon Sep 17 00:00:00 2001 From: Tomas Cere Date: Tue, 10 Oct 2017 10:50:57 +0200 Subject: [PATCH] Bug 9256: Add websocket server config knob for ip Also cleans up a couple of ugly NPE catches. Change-Id: Ifd164aa76c406cc2082d603df8732d636f6e06a1 Signed-off-by: Tomas Cere --- .../sal/restconf/impl/RestconfImpl.java | 28 +++++--------- .../restconf/impl/RestconfProviderImpl.java | 8 +++- .../streams/websockets/WebSocketServer.java | 37 ++++++++++++++++--- .../blueprint/restconf-config.xml | 6 +++ .../src/main/resources/initial/restconf.cfg | 1 + 5 files changed, 54 insertions(+), 26 deletions(-) diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java index 99200cb845..818e76844e 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfImpl.java @@ -1241,17 +1241,13 @@ public class RestconfImpl implements RestconfService { } final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder(); - int notificationPort = NOTIFICATION_PORT; - try { - final WebSocketServer webSocketServerInstance = WebSocketServer.getInstance(); - notificationPort = webSocketServerInstance.getPort(); - } catch (final NullPointerException e) { - WebSocketServer.createInstance(NOTIFICATION_PORT); - } + + final WebSocketServer webSocketServerInstance = WebSocketServer.getInstance(NOTIFICATION_PORT); + final int notificationPort = webSocketServerInstance.getPort(); + final UriBuilder uriToWebsocketServerBuilder = uriBuilder.port(notificationPort).scheme("ws"); - final URI uriToWebsocketServer = uriToWebsocketServerBuilder.replacePath(streamName).build(); - return uriToWebsocketServer; + return uriToWebsocketServerBuilder.replacePath(streamName).build(); } /** @@ -1301,17 +1297,13 @@ public class RestconfImpl implements RestconfService { this.broker.registerToListenDataChanges(datastore, scope, listener); final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder(); - int notificationPort = NOTIFICATION_PORT; - try { - final WebSocketServer webSocketServerInstance = WebSocketServer.getInstance(); - notificationPort = webSocketServerInstance.getPort(); - } catch (final NullPointerException e) { - WebSocketServer.createInstance(NOTIFICATION_PORT); - } + + final WebSocketServer webSocketServerInstance = WebSocketServer.getInstance(NOTIFICATION_PORT); + final int notificationPort = webSocketServerInstance.getPort(); + final UriBuilder uriToWebsocketServerBuilder = uriBuilder.port(notificationPort).scheme("ws"); - final URI uriToWebsocketServer = uriToWebsocketServerBuilder.replacePath(streamName).build(); - return uriToWebsocketServer; + return uriToWebsocketServerBuilder.replacePath(streamName).build(); } @SuppressWarnings("checkstyle:IllegalCatch") diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java index 248863dca8..614133bdd6 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/RestconfProviderImpl.java @@ -25,6 +25,7 @@ import org.opendaylight.netconf.sal.restconf.impl.jmx.Put; import org.opendaylight.netconf.sal.restconf.impl.jmx.RestConnectorRuntimeMXBean; import org.opendaylight.netconf.sal.restconf.impl.jmx.Rpcs; import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.opendaylight.yangtools.yang.model.api.SchemaContextListener; @@ -36,6 +37,7 @@ public class RestconfProviderImpl extends AbstractMXBean private final DOMRpcService rpcService; private final DOMNotificationService notificationService; private final DOMMountPointService mountPointService; + private final IpAddress websocketAddress; private final PortNumber websocketPort; private final StatisticsRestconfServiceWrapper stats = StatisticsRestconfServiceWrapper.getInstance(); private ListenerRegistration listenerRegistration; @@ -43,13 +45,14 @@ public class RestconfProviderImpl extends AbstractMXBean public RestconfProviderImpl(DOMDataBroker domDataBroker, SchemaService schemaService, DOMRpcService rpcService, DOMNotificationService notificationService, DOMMountPointService mountPointService, - PortNumber websocketPort) { + IpAddress websocketAddress, PortNumber websocketPort) { super("Draft02ProviderStatistics", "restconf-connector", null); this.domDataBroker = Preconditions.checkNotNull(domDataBroker); this.schemaService = Preconditions.checkNotNull(schemaService); this.rpcService = Preconditions.checkNotNull(rpcService); this.notificationService = Preconditions.checkNotNull(notificationService); this.mountPointService = Preconditions.checkNotNull(mountPointService); + this.websocketAddress = Preconditions.checkNotNull(websocketAddress); this.websocketPort = Preconditions.checkNotNull(websocketPort); } @@ -63,7 +66,8 @@ public class RestconfProviderImpl extends AbstractMXBean ControllerContext.getInstance().setSchemas(schemaService.getGlobalContext()); ControllerContext.getInstance().setMountService(mountPointService); - this.webSocketServerThread = new Thread(WebSocketServer.createInstance(websocketPort.getValue().intValue())); + this.webSocketServerThread = new Thread(WebSocketServer.createInstance( + new String(websocketAddress.getValue()), websocketPort.getValue())); this.webSocketServerThread.setName("Web socket server on port " + websocketPort); this.webSocketServerThread.start(); diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServer.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServer.java index 23bff91a53..1fdfd4d63d 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServer.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/streams/websockets/WebSocketServer.java @@ -26,15 +26,19 @@ public class WebSocketServer implements Runnable { private static final Logger LOG = LoggerFactory.getLogger(WebSocketServer.class); + private static final String DEFAULT_ADDRESS = "0.0.0.0"; + private static WebSocketServer instance = null; + private final String address; private final int port; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; - private WebSocketServer(final int port) { + private WebSocketServer(final String address, final int port) { + this.address = address; this.port = port; } @@ -44,11 +48,17 @@ public class WebSocketServer implements Runnable { * @param port TCP port used for this server * @return instance of {@link WebSocketServer} */ - public static WebSocketServer createInstance(final int port) { + private static WebSocketServer createInstance(final int port) { + instance = createInstance(DEFAULT_ADDRESS, port); + return instance; + } + + public static WebSocketServer createInstance(final String address, final int port) { Preconditions.checkState(instance == null, "createInstance() has already been called"); + Preconditions.checkNotNull(address, "Address cannot be null."); Preconditions.checkArgument(port >= 1024, "Privileged port (below 1024) is not allowed"); - instance = new WebSocketServer(port); + instance = new WebSocketServer(address, port); return instance; } @@ -71,6 +81,21 @@ public class WebSocketServer implements Runnable { return instance; } + /** + * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}. + * If an instance doesnt exist create one with the provided fallback port. + * + * @return instance of {@link WebSocketServer} + */ + public static WebSocketServer getInstance(final int fallbackPort) { + if (instance != null) { + return instance; + } + + LOG.warn("No instance for WebSocketServer found, creating one with a fallback port: {}", fallbackPort); + return createInstance(fallbackPort); + } + /** * Destroy the existing instance. */ @@ -90,8 +115,8 @@ public class WebSocketServer implements Runnable { serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer()); - final Channel channel = serverBootstrap.bind(port).sync().channel(); - LOG.info("Web socket server started at port {}.", port); + final Channel channel = serverBootstrap.bind(address, port).sync().channel(); + LOG.info("Web socket server started at address {}, port {}.", address, port); channel.closeFuture().sync(); } catch (final InterruptedException e) { @@ -105,7 +130,7 @@ public class WebSocketServer implements Runnable { * Stops the web socket server and removes all listeners. */ private void stop() { - LOG.debug("Stopping the web socket server instance on port {}", port); + LOG.info("Stopping the web socket server instance on port {}", port); Notificator.removeAllListeners(); if (bossGroup != null) { bossGroup.shutdownGracefully(); diff --git a/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml b/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml index d3d249c6d4..676ab128e6 100644 --- a/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml +++ b/restconf/restconf-nb-bierman02/src/main/resources/org/opendaylight/blueprint/restconf-config.xml @@ -16,6 +16,7 @@ + @@ -47,6 +48,10 @@ + + + + @@ -54,6 +59,7 @@ + diff --git a/restconf/sal-rest-connector-config/src/main/resources/initial/restconf.cfg b/restconf/sal-rest-connector-config/src/main/resources/initial/restconf.cfg index 088d976824..4439da35f9 100644 --- a/restconf/sal-rest-connector-config/src/main/resources/initial/restconf.cfg +++ b/restconf/sal-rest-connector-config/src/main/resources/initial/restconf.cfg @@ -1,2 +1,3 @@ # The port for the web socket server. +#websocket-address=0.0.0.0 #websocket-port=8185 -- 2.36.6