From 8644159923028969f88b212c4e46e995c2eed743 Mon Sep 17 00:00:00 2001 From: Richard Kosegi Date: Wed, 9 Jul 2014 15:31:05 +0200 Subject: [PATCH] Bug 1341 - Make RESTConf websocket port configurable Change-Id: I54f4bcadd864eb264b92f00b82b81489e9ebecf0 Signed-off-by: Richard Kosegi --- .../main/resources/configuration/config.ini | 3 + .../sal/rest/impl/RestconfProvider.java | 9 ++- .../sal/restconf/impl/RestconfImpl.java | 2 +- .../streams/websockets/WebSocketServer.java | 63 +++++++++++++++++-- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/opendaylight/distribution/opendaylight/src/main/resources/configuration/config.ini b/opendaylight/distribution/opendaylight/src/main/resources/configuration/config.ini index f4bf483579..b2fc3cb386 100644 --- a/opendaylight/distribution/opendaylight/src/main/resources/configuration/config.ini +++ b/opendaylight/distribution/opendaylight/src/main/resources/configuration/config.ini @@ -149,3 +149,6 @@ hosttracker.keyscheme=IP lisp.mappingOverwrite = true # Enable the Solicit-Map-Request (SMR) mechanism lisp.smr = false + +#RESTConf websocket listen port (default is 8181) +restconf.websocket.port=8181 diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestconfProvider.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestconfProvider.java index 2abd4b6a3a..1c95f1327b 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestconfProvider.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/rest/impl/RestconfProvider.java @@ -51,12 +51,15 @@ public class RestconfProvider implements BundleActivator, Provider, ServiceTrack @Override public void start(BundleContext context) throws Exception { + String websocketPortStr = context.getProperty(WebSocketServer.WEBSOCKET_SERVER_CONFIG_PROPERTY); + int websocketPort = (websocketPortStr != null && !"".equals(websocketPortStr)) + ? Integer.parseInt(websocketPortStr) : WebSocketServer.DEFAULT_PORT; bundleContext = context; - brokerServiceTrancker = new ServiceTracker<>(context, Broker.class, this); - brokerServiceTrancker.open(); - webSocketServerThread = new Thread(new WebSocketServer()); + webSocketServerThread = new Thread(WebSocketServer.createInstance(websocketPort)); webSocketServerThread.setName("Web socket server"); webSocketServerThread.start(); + brokerServiceTrancker = new ServiceTracker<>(context, Broker.class, this); + brokerServiceTrancker.open(); } @Override diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java index 4e1adbc598..c8440c0a16 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/RestconfImpl.java @@ -878,7 +878,7 @@ public class RestconfImpl implements RestconfService { broker.registerToListenDataChanges(listener); final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder(); - UriBuilder port = uriBuilder.port(WebSocketServer.PORT); + UriBuilder port = uriBuilder.port(WebSocketServer.getInstance().getPort()); final URI uriToWebsocketServer = port.replacePath(streamName).build(); return Response.status(Status.OK).location(uriToWebsocketServer).build(); diff --git a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java index fcfa8858ee..20951b01e2 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java +++ b/opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/websockets/WebSocketServer.java @@ -10,18 +10,69 @@ import org.opendaylight.controller.sal.streams.listeners.Notificator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Preconditions; + /** - * {@link WebSocketServer} is responsible to start and stop web socket server at - * {@link #PORT}. + * {@link WebSocketServer} is responsible to start and stop web socket server */ public class WebSocketServer implements Runnable { private static final Logger logger = LoggerFactory .getLogger(WebSocketServer.class); - - public static final int PORT = 8181; + public static final String WEBSOCKET_SERVER_CONFIG_PROPERTY = "restconf.websocket.port"; + public static final int DEFAULT_PORT = 8181; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; + private static WebSocketServer singleton = null; + private int port = DEFAULT_PORT; + + private WebSocketServer(int port) { + this.port = port; + } + + /** + * Create instance of {@link WebSocketServer} + * @param port TCP port used for this server + * @return instance of {@link WebSocketServer} + */ + public static WebSocketServer createInstance(int port) { + if(singleton != null) { + throw new IllegalStateException("createInstance() has already been called"); + } + if(port < 1024) { + throw new IllegalArgumentException("Privileged port (below 1024) is not allowed"); + } + singleton = new WebSocketServer(port); + return singleton; + } + + /** + * Return websocket TCP port + */ + public int getPort() { + return port; + } + + /** + * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)} + * @return instance of {@link WebSocketServer} + */ + public static WebSocketServer getInstance() { + Preconditions.checkNotNull(singleton, + "createInstance() must be called prior to getInstance()"); + return singleton; + } + + /** + * Destroy this already created instance + */ + public static void destroyInstance() { + if(singleton == null) { + throw new IllegalStateException( + "createInstance() must be called prior to destroyInstance()"); + } + getInstance().stop(); + } @Override public void run() { @@ -33,8 +84,8 @@ public class WebSocketServer implements Runnable { .channel(NioServerSocketChannel.class) .childHandler(new WebSocketServerInitializer()); - Channel ch = b.bind(PORT).sync().channel(); - logger.info("Web socket server started at port {}.", PORT); + Channel ch = b.bind(port).sync().channel(); + logger.info("Web socket server started at port {}.", port); ch.closeFuture().sync(); } catch (InterruptedException e) { -- 2.36.6