From 1d3188094a7c88a79425fd80ab24375346940486 Mon Sep 17 00:00:00 2001 From: Ravi Pendurty Date: Tue, 14 Mar 2023 19:31:54 +0530 Subject: websocketmanager now listens on a custom jetty port This is to overcome the issues of paxweb and jetty versions in karaf used in ODL Chlorine Issue-ID: CCSDK-3869 Signed-off-by: Ravi Pendurty Change-Id: Id9551c34bb9d1b6258c2784988eb0323cc73b7b2 Signed-off-by: Ravi Pendurty --- sdnr/wt/websocketmanager/feature/pom.xml | 1 + sdnr/wt/websocketmanager/installer/pom.xml | 1 + sdnr/wt/websocketmanager/model/pom.xml | 3 +- sdnr/wt/websocketmanager/pom.xml | 1 + sdnr/wt/websocketmanager/provider/pom.xml | 27 +++++++--- .../websocketmanager/WebSocketManagerCreator.java | 36 +++++++++++++ .../websocketmanager/WebSocketManagerProvider.java | 63 +++++++++++++++++++++- .../websocketmanager/WebSocketManagerSocket.java | 16 +++--- .../config/WebSocketManagerConfig.java | 56 +++++++++++++++++++ .../org/opendaylight/blueprint/impl-blueprint.xml | 14 ++++- 10 files changed, 197 insertions(+), 21 deletions(-) create mode 100644 sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerCreator.java create mode 100644 sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/config/WebSocketManagerConfig.java (limited to 'sdnr') diff --git a/sdnr/wt/websocketmanager/feature/pom.xml b/sdnr/wt/websocketmanager/feature/pom.xml index e0ef49b00..f97c1e5be 100644 --- a/sdnr/wt/websocketmanager/feature/pom.xml +++ b/sdnr/wt/websocketmanager/feature/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 diff --git a/sdnr/wt/websocketmanager/installer/pom.xml b/sdnr/wt/websocketmanager/installer/pom.xml index 09af75374..175a47225 100755 --- a/sdnr/wt/websocketmanager/installer/pom.xml +++ b/sdnr/wt/websocketmanager/installer/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 diff --git a/sdnr/wt/websocketmanager/model/pom.xml b/sdnr/wt/websocketmanager/model/pom.xml index b21533802..1a95d6d01 100644 --- a/sdnr/wt/websocketmanager/model/pom.xml +++ b/sdnr/wt/websocketmanager/model/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 @@ -29,7 +30,7 @@ org.onap.ccsdk.parent binding-parent 2.5.4-SNAPSHOT - + org.onap.ccsdk.features.sdnr.wt diff --git a/sdnr/wt/websocketmanager/pom.xml b/sdnr/wt/websocketmanager/pom.xml index bd30e2ac0..6cca46e01 100755 --- a/sdnr/wt/websocketmanager/pom.xml +++ b/sdnr/wt/websocketmanager/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 diff --git a/sdnr/wt/websocketmanager/provider/pom.xml b/sdnr/wt/websocketmanager/provider/pom.xml index db7ac81bb..f9590938f 100644 --- a/sdnr/wt/websocketmanager/provider/pom.xml +++ b/sdnr/wt/websocketmanager/provider/pom.xml @@ -22,6 +22,7 @@ ~ ============LICENSE_END======================================================= ~ --> + 4.0.0 @@ -29,7 +30,7 @@ org.onap.ccsdk.parent binding-parent 2.5.4-SNAPSHOT - + org.onap.ccsdk.features.sdnr.wt @@ -51,13 +52,14 @@ ${project.groupId} - sdnr-wt-websocketmanager-model + sdnr-wt-common ${project.version} + provided - org.opendaylight.mdsal.model - ietf-topology - provided + ${project.groupId} + sdnr-wt-websocketmanager-model + ${project.version} ${project.groupId} @@ -65,10 +67,22 @@ ${project.version} provided + + org.opendaylight.mdsal.model + ietf-topology + provided + org.eclipse.jetty.websocket websocket-servlet - provided + + + org.eclipse.jetty.websocket + websocket-server + + + org.eclipse.jetty + jetty-util org.json @@ -130,6 +144,5 @@ ${project.version} test - diff --git a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerCreator.java b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerCreator.java new file mode 100644 index 000000000..9c316a6d9 --- /dev/null +++ b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerCreator.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2023 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ + +package org.onap.ccsdk.features.sdnr.wt.websocketmanager; + +import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest; +import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse; +import org.eclipse.jetty.websocket.servlet.WebSocketCreator; + +public class WebSocketManagerCreator implements WebSocketCreator { + + @Override + public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) { + return new WebSocketManagerSocket(); + } + +} diff --git a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerProvider.java b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerProvider.java index 8af5cb1ee..55115908f 100644 --- a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerProvider.java +++ b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerProvider.java @@ -19,6 +19,13 @@ package org.onap.ccsdk.features.sdnr.wt.websocketmanager; import java.time.Instant; import javax.servlet.ServletException; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.websocket.server.NativeWebSocketServletContainerInitializer; +import org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; +import org.onap.ccsdk.features.sdnr.wt.websocketmanager.config.WebSocketManagerConfig; import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService; import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.data.DOMNotificationOutput; import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.data.NotificationOutput; @@ -37,22 +44,71 @@ public class WebSocketManagerProvider implements WebsocketManagerService, AutoCl private static final Logger LOG = LoggerFactory.getLogger(WebSocketManagerProvider.class); private static final String APPLICATION_NAME = WebSocketManagerProvider.class.getName(); + private static final String CONFIGURATIONFILE = "etc/websocketmanager.properties"; + private WebSocketManagerConfig wsConfig; private static final String ALIAS = "/websocket"; + private static final String DEFAULT_IP_ADDR = "0.0.0.0"; private WebSocketManager wsServlet = null; + private Server server = null; public WebSocketManagerProvider() { LOG.info("Creating provider for {}", APPLICATION_NAME); } - public void init() { LOG.info("Init provider for {}", APPLICATION_NAME); + ConfigurationFileRepresentation configFileRepresentation = + new ConfigurationFileRepresentation(CONFIGURATIONFILE); + + wsConfig = new WebSocketManagerConfig(configFileRepresentation); + + if (wsConfig.getWebsocketPort().isPresent() && !wsConfig.getWebsocketPort().isEmpty()) { + try { + startServer(DEFAULT_IP_ADDR, wsConfig.getWebsocketPort().get().intValue(), ALIAS); + } catch (Exception e) { + LOG.error("Failed in Websocker server startup {}", e); + } + } else { + LOG.error("WebSocket Port not configured, hence not starting WebSocket Manager"); + } + } + + public void startServer(String wsHost, int wsPort, String wsPath) throws Exception { + server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setHost(wsHost); + connector.setPort(wsPort); + server.addConnector(connector); + + ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS); + context.setContextPath("/"); + server.setHandler(context); + + NativeWebSocketServletContainerInitializer.configure(context, + (servletContext, nativeWebSocketConfiguration) -> { + // Configure default max size + nativeWebSocketConfiguration.getPolicy().setMaxTextMessageBufferSize(65535); + + // Add websockets + nativeWebSocketConfiguration.addMapping(wsPath, new WebSocketManagerCreator()); + }); + + // Add generic filter that will accept WebSocket upgrade. + WebSocketUpgradeFilter.configure(context); + + server.start(); + } + + public void stopServer() throws Exception { + if (server != null) + server.stop(); } @Override public void close() throws Exception { LOG.info("Close provider for {}", APPLICATION_NAME); + stopServer(); } public void onUnbindService(HttpService httpService) { @@ -60,11 +116,14 @@ public class WebSocketManagerProvider implements WebsocketManagerService, AutoCl wsServlet = null; } + public void setAboutServlet(WebSocketManager wsServlet) { + this.wsServlet = wsServlet; + } + public void onBindService(HttpService httpService) throws ServletException, NamespaceException { if (httpService == null) { LOG.warn("Unable to inject HttpService into DluxLoader. dlux modules won't work without httpService"); } else { - if (wsServlet == null) { wsServlet = new WebSocketManager(); httpService.registerServlet(ALIAS, wsServlet, null, null); diff --git a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerSocket.java b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerSocket.java index 0b64d476a..7e4043b18 100644 --- a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerSocket.java +++ b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/WebSocketManagerSocket.java @@ -18,13 +18,14 @@ package org.onap.ccsdk.features.sdnr.wt.websocketmanager; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import java.security.SecureRandom; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Random; import java.util.Set; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutionException; @@ -32,9 +33,6 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.regex.Matcher; import java.util.regex.Pattern; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; import org.eclipse.jetty.websocket.api.Session; import org.eclipse.jetty.websocket.api.WebSocketAdapter; import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.data.DOMNotificationOutput; @@ -82,7 +80,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { if (message != null) { WebSocketManagerSocket.this.session.getRemote().sendStringByFuture(message) .get(SEND_MESSAGE_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); - LOG.info("message sent"); + LOG.debug("message sent"); } } catch (ExecutionException | TimeoutException e) { LOG.warn("problem pushing message: ", e); @@ -155,7 +153,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { @Override public void onWebSocketText(String message) { - LOG.info("{} has sent {}", this.getRemoteAdr(), message); + LOG.debug("{} has sent {}", this.getRemoteAdr(), message); if (!this.manageClientRequest(message)) { this.manageClientRequest2(message); } @@ -185,7 +183,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { @Override public void onWebSocketError(Throwable cause) { - LOG.debug("error caused on {}: ",this.getRemoteAdr(), cause); + LOG.debug("error caused on {}: ", this.getRemoteAdr(), cause); } private String getRemoteAdr() { @@ -221,7 +219,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { } } catch (JsonProcessingException e) { - LOG.warn("problem set scope: {}" ,e.getMessage()); + LOG.warn("problem set scope: {}", e.getMessage()); try { this.send(mapper.writeValueAsString(ScopeRegistrationResponse.error(e.getMessage()))); } catch (JsonProcessingException e1) { @@ -241,7 +239,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { this.sendToAll(notification.getNodeId(), notification.getType(), request); } } catch (Exception e) { - LOG.warn("handle ws request failed:",e); + LOG.warn("handle ws request failed:", e); } } diff --git a/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/config/WebSocketManagerConfig.java b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/config/WebSocketManagerConfig.java new file mode 100644 index 000000000..0a31ae690 --- /dev/null +++ b/sdnr/wt/websocketmanager/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager/config/WebSocketManagerConfig.java @@ -0,0 +1,56 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : ccsdk features + * ================================================================================ + * Copyright (C) 2023 highstreet technologies GmbH Intellectual Property. + * All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * + */ +package org.onap.ccsdk.features.sdnr.wt.websocketmanager.config; + +import java.util.Optional; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.Configuration; +import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation; + +public class WebSocketManagerConfig implements Configuration { + + private static final String SECTION_MARKER = "websocket"; + private static final String PROPERTY_KEY_WEBSOCKET_PORT = "port"; + private static final String PROPERTY_VALUE_WEBSOCKET_PORT = "${SDNR_WEBSOCKET_PORT}"; + + private ConfigurationFileRepresentation configuration; + + public WebSocketManagerConfig(ConfigurationFileRepresentation configuration) { + this.configuration = configuration; + configuration.addSection(SECTION_MARKER); + defaults(); + } + + public Optional getWebsocketPort() { + return configuration.getPropertyLong(SECTION_MARKER, PROPERTY_KEY_WEBSOCKET_PORT); + } + + @Override + public String getSectionName() { + return SECTION_MARKER; + } + + @Override + public void defaults() { + configuration.setPropertyIfNotAvailable(SECTION_MARKER, PROPERTY_KEY_WEBSOCKET_PORT, PROPERTY_VALUE_WEBSOCKET_PORT); + } + +} diff --git a/sdnr/wt/websocketmanager/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml b/sdnr/wt/websocketmanager/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml index e3b36e3fe..76d9d79ff 100644 --- a/sdnr/wt/websocketmanager/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml +++ b/sdnr/wt/websocketmanager/provider/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml @@ -27,12 +27,22 @@ xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" odl:use-default-for-reference-types="true"> + + + + - +