Also cleans up a couple of ugly NPE catches.
Change-Id: Ifd164aa76c406cc2082d603df8732d636f6e06a1
Signed-off-by: Tomas Cere <tcere@cisco.com>
}
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();
}
/**
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")
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;
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<SchemaContextListener> listenerRegistration;
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);
}
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();
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;
}
* @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;
}
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.
*/
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) {
* 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();
<cm:property-placeholder persistent-id="org.opendaylight.restconf" update-strategy="reload">
<cm:default-properties>
+ <cm:property name="websocket-address" value="0.0.0.0"/>
<cm:property name="websocket-port" value="8185"/>
</cm:default-properties>
</cm:property-placeholder>
<argument value="${websocket-port}"/>
</bean>
+ <bean id="webSocketAddress" class="org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress">
+ <argument value="${websocket-address}"/>
+ </bean>
+
<bean id="restconfProviderDraft02" class="org.opendaylight.netconf.sal.restconf.impl.RestconfProviderImpl"
init-method="start" destroy-method="close">
<argument ref="domDataBroker"/>
<argument ref="domRpcService"/>
<argument ref="domNotificationService"/>
<argument ref="domMountPointService"/>
+ <argument ref="webSocketAddress"/>
<argument ref="webSocketPort"/>
</bean>
# The port for the web socket server.
+#websocket-address=0.0.0.0
#websocket-port=8185