diff options
Diffstat (limited to 'sdnr/wt/websocketmanager/provider')
6 files changed, 192 insertions, 20 deletions
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======================================================= ~ --> + <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -29,7 +30,7 @@ <groupId>org.onap.ccsdk.parent</groupId> <artifactId>binding-parent</artifactId> <version>2.5.4-SNAPSHOT</version> - <relativePath/> + <relativePath /> </parent> <groupId>org.onap.ccsdk.features.sdnr.wt</groupId> @@ -51,13 +52,14 @@ <dependencies> <dependency> <groupId>${project.groupId}</groupId> - <artifactId>sdnr-wt-websocketmanager-model</artifactId> + <artifactId>sdnr-wt-common</artifactId> <version>${project.version}</version> + <scope>provided</scope> </dependency> <dependency> - <groupId>org.opendaylight.mdsal.model</groupId> - <artifactId>ietf-topology</artifactId> - <scope>provided</scope> + <groupId>${project.groupId}</groupId> + <artifactId>sdnr-wt-websocketmanager-model</artifactId> + <version>${project.version}</version> </dependency> <dependency> <groupId>${project.groupId}</groupId> @@ -66,9 +68,21 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>org.opendaylight.mdsal.model</groupId> + <artifactId>ietf-topology</artifactId> + <scope>provided</scope> + </dependency> + <dependency> <groupId>org.eclipse.jetty.websocket</groupId> <artifactId>websocket-servlet</artifactId> - <scope>provided</scope> + </dependency> + <dependency> + <groupId>org.eclipse.jetty.websocket</groupId> + <artifactId>websocket-server</artifactId> + </dependency> + <dependency> + <groupId>org.eclipse.jetty</groupId> + <artifactId>jetty-util</artifactId> </dependency> <dependency> <groupId>org.json</groupId> @@ -130,6 +144,5 @@ <version>${project.version}</version> <scope>test</scope> </dependency> - </dependencies> </project> 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<Long> 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"> + + <!-- <bean id="wsServlet" + class="org.onap.ccsdk.features.sdnr.wt.websocketmanager.WebSocketManager"> + </bean> --> + <!-- <service interface="javax.servlet.http.HttpServlet" ref="wsServlet"> + <service-properties> + <entry key="alias" value="/websocket"/> + </service-properties> + </service> --> <bean id="provider" class="org.onap.ccsdk.features.sdnr.wt.websocketmanager.WebSocketManagerProvider" init-method="init" destroy-method="close"> + <!-- <property name="wsServlet" ref="wsServlet"/> --> </bean> - <reference id="onBindService" availability="mandatory" activation="eager" interface="org.osgi.service.http.HttpService"> + <!-- <reference id="onBindService" availability="mandatory" activation="eager" interface="org.osgi.service.http.HttpService"> <reference-listener ref="provider" bind-method="onBindService" unbind-method="onUnbindService"/> - </reference> + </reference> --> <service id="registerWebsocketmanagerService" interface="org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService" |