From 6f386e7a227b73d3c77786460e1e0a11fc8cd26a Mon Sep 17 00:00:00 2001 From: highstreetherbert Date: Fri, 10 Jul 2020 16:22:33 +0200 Subject: Reformat sdnr websocketmanager2 to ONAP code style Reformat to ONAP code style Issue-ID: SDNC-1274 Signed-off-by: highstreetherbert Change-Id: Ib766ceabe2f5cf3a1a7fe7492dbd844afc63d5e9 Signed-off-by: highstreetherbert --- .../sdnr/wt/websocketmanager2/Blueprint.java | 4 +- .../wt/websocketmanager2/WebSocketManager.java | 14 +- .../WebSocketManagerProvider.java | 8 +- .../websocketmanager2/WebSocketManagerSocket.java | 28 ++-- .../wt/websocketmanager2/utils/AkkaConfig.java | 167 ++++++++++---------- .../wt/websocketmanager2/utils/UserScopes.java | 40 ++--- .../websocket/SyncWebSocketClient.java | 172 +++++++++++---------- .../wt/websocketmanager2/test/AkkaConfigTest.java | 19 +-- .../wt/websocketmanager2/test/UserScopeTest.java | 14 +- .../test/WebsockerProviderTest.java | 6 +- .../test/WebsocketClientTest.java | 6 +- .../test/WebsocketMessageTest.java | 13 +- .../test/WebsocketServerConnectTest.java | 4 +- 13 files changed, 252 insertions(+), 243 deletions(-) (limited to 'sdnr/wt') diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/Blueprint.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/Blueprint.java index bd84043a2..7deefa940 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/Blueprint.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/Blueprint.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2; import org.opendaylight.mdsal.binding.api.RpcProviderService; diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManager.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManager.java index ea06ffcd3..1c4d924cb 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManager.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManager.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2; import com.google.common.util.concurrent.ListenableFuture; @@ -37,9 +37,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocke import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutput; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketEventOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.websocketmanager.rev150105.WebsocketmanagerService; +import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,9 +54,9 @@ public class WebSocketManager extends WebSocketServlet implements Websocketmanag private final AkkaConfig akkaConfig; /** * timeout for websocket with no messages in ms - */ + */ //private static final long IDLE_TIMEOUT = 5 * 60 * 1000L; - private static final long IDLE_TIMEOUT =0L; + private static final long IDLE_TIMEOUT = 0L; private final ArrayList clusterNodeClients = new ArrayList<>(); @@ -142,12 +142,12 @@ public class WebSocketManager extends WebSocketServlet implements Websocketmanag /********************************************************** * Private functions - */ + */ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { if (req.getHeader("Upgrade") != null) { - /* Accept upgrade request */ + /* Accept upgrade request */ resp.setStatus(101); resp.setHeader("Upgrade", "XYZP"); resp.setHeader("Connection", "Upgrade"); diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerProvider.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerProvider.java index 881ee1a82..7e82b5e0f 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerProvider.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerProvider.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2; import com.google.common.util.concurrent.ListenableFuture; @@ -107,8 +107,8 @@ public class WebSocketManagerProvider extends Blueprint implements Websocketmana @Override public ListenableFuture> websocketEvent(WebsocketEventInput input) { if (wsServlet != null) { - return wsServlet.websocketEvent(input); - } else { + return wsServlet.websocketEvent(input); + } else { RpcResultBuilder result = RpcResultBuilder.failed(); return result.withError(ErrorType.APPLICATION, "Not intialized").buildFuture(); } diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerSocket.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerSocket.java index 0fd2cae4e..70810a7ac 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerSocket.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/WebSocketManagerSocket.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2; import java.util.ArrayList; @@ -50,15 +50,15 @@ public class WebSocketManagerSocket extends WebSocketAdapter { /** * list of all sessionids - */ + */ private static final List sessionIds = new ArrayList<>(); /** * map of sessionid <=> UserScopes - */ + */ private static final HashMap userScopesList = new HashMap<>(); /** * map of class.hashCode <=> class - */ + */ private static final HashMap clientList = new HashMap<>(); private final String myUniqueSessionId; @@ -88,7 +88,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { @Override public void onWebSocketText(String message) { - LOG.info("{} has sent {}",this.getRemoteAdr(), message); + LOG.info("{} has sent {}", this.getRemoteAdr(), message); if (!this.manageClientRequest(message)) { this.manageClientRequest2(message); } @@ -130,10 +130,9 @@ public class WebSocketManagerSocket extends WebSocketAdapter { /** * - * @param request is a json object - * {"data":"scopes","scopes":["scope1","scope2",...]} + * @param request is a json object {"data":"scopes","scopes":["scope1","scope2",...]} * @return if handled - */ + */ private boolean manageClientRequest(String request) { boolean ret = false; try { @@ -160,7 +159,7 @@ public class WebSocketManagerSocket extends WebSocketAdapter { /* * broadcast message to all your clients - */ + */ private void manageClientRequest2(String request) { try { JSONObject o = new JSONObject(request); @@ -180,7 +179,8 @@ public class WebSocketManagerSocket extends WebSocketAdapter { LOG.warn("problem sending message: " + e.getMessage()); } } - public String getSessionId() { + + public String getSessionId() { return this.myUniqueSessionId; } @@ -209,12 +209,12 @@ public class WebSocketManagerSocket extends WebSocketAdapter { } } } + public static void broadCast(String nodeName, String eventType, String xmlEvent) { - if(clientList.size()>0) { + if (clientList.size() > 0) { Set> e = clientList.entrySet(); WebSocketManagerSocket s = e.iterator().next().getValue(); - if(s!=null) - { + if (s != null) { s.sendToAll(nodeName, eventType, xmlEvent); } } diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/AkkaConfig.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/AkkaConfig.java index 4381595a7..93a941d20 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/AkkaConfig.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/AkkaConfig.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -6,17 +6,19 @@ * ================================================================================================= * 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.websocketmanager2.utils; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; import java.io.File; import java.util.ArrayList; import java.util.List; @@ -26,42 +28,40 @@ import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.WebSocketManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.typesafe.config.Config; -import com.typesafe.config.ConfigFactory; - -public class AkkaConfig{ +public class AkkaConfig { private static final Logger LOG = LoggerFactory.getLogger(WebSocketManager.class.getName()); - public static class ClusterNodeInfo - { + public static class ClusterNodeInfo { @Override public String toString() { return "ClusterNodeInfo [protocol=" + protocol + ", clusterName=" + clusterName + ", remoteAdr=" + remoteAdr + ", port=" + port + "]"; } + private final String protocol; private final String clusterName; private final String remoteAdr; private final int port; - public String getRemoteAddress() {return this.remoteAdr;} - public ClusterNodeInfo(String s) throws Exception - { - final String regex ="([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)"; + public String getRemoteAddress() { + return this.remoteAdr; + } + + public ClusterNodeInfo(String s) throws Exception { + final String regex = "([a-z.]*):\\/\\/([a-zA-Z0-9-]*)@([a-zA-Z0-9.-]*):([0-9]*)"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(s); - if(!matcher.find()) { + if (!matcher.find()) { throw new Exception("invalid seedNode format"); } - this.protocol=matcher.group(1); - this.clusterName=matcher.group(2); - this.remoteAdr=matcher.group(3); - this.port=Integer.parseInt(matcher.group(4)); + this.protocol = matcher.group(1); + this.clusterName = matcher.group(2); + this.remoteAdr = matcher.group(3); + this.port = Integer.parseInt(matcher.group(4)); } } - public static class ClusterRoleInfo - { + public static class ClusterRoleInfo { @Override public String toString() { return "ClusterRoleInfo [Role=" + Role + ", Index=" + Index + "]"; @@ -69,58 +69,61 @@ public class AkkaConfig{ private final String Role; private final int Index; + public ClusterRoleInfo(String s) throws Exception { final String regex = "([a-z]*)-([0-9]*)"; final Pattern pattern = Pattern.compile(regex); final Matcher matcher = pattern.matcher(s); - if(!matcher.find()) { + if (!matcher.find()) { throw new Exception("invalid role format"); } - this.Role=matcher.group(1); - this.Index=Integer.parseInt(matcher.group(2)); + this.Role = matcher.group(1); + this.Index = Integer.parseInt(matcher.group(2)); } } - public static class ClusterConfig - { + public static class ClusterConfig { @Override public String toString() { return "ClusterConfig [seedNodes=" + seedNodes + ", roles=" + roles + "]"; } + private final List seedNodes; private final List roles; private final ClusterNodeInfo ismeInfo; + public ClusterConfig(Config o) throws Exception { { - this.seedNodes = new ArrayList<>(); - List a= o.getStringList("seed-nodes"); - for(int i=0;i(); - a=o.getStringList("roles"); - for(int i=0;i=0 && idx(); + List a = o.getStringList("seed-nodes"); + for (int i = 0; i < a.size(); i++) { + ClusterNodeInfo info = new ClusterNodeInfo(a.get(i)); + this.seedNodes.add(info); + } + this.roles = new ArrayList<>(); + a = o.getStringList("roles"); + for (int i = 0; i < a.size(); i++) { + ClusterRoleInfo s = new ClusterRoleInfo(a.get(i)); + this.roles.add(s); + } + int idx = this.roles.get(0).Index - 1; + if (idx >= 0 && idx < this.seedNodes.size()) { + this.ismeInfo = this.seedNodes.get(idx); + } else { + this.ismeInfo = null; + } } + } - } public boolean isCluster() { - return this.seedNodes!=null?this.seedNodes.size()>1:false; + return this.seedNodes != null ? this.seedNodes.size() > 1 : false; } + public boolean isMe(ClusterNodeInfo i) { - return this.ismeInfo!=null?this.ismeInfo.equals(i):false; + return this.ismeInfo != null ? this.ismeInfo.equals(i) : false; } + public List getSeedNodes() { return this.seedNodes; } @@ -131,13 +134,17 @@ public class AkkaConfig{ private final String resourceFilename; private final String fileContent; private ClusterConfig cluserConfig; - public ClusterConfig getClusterConfig() {return this.cluserConfig;} - private AkkaConfig(File file,boolean isResource) { - this.file=isResource?null:file; - this.fileContent=null; - this.resourceFilename=isResource?file.getName():null; + public ClusterConfig getClusterConfig() { + return this.cluserConfig; } + + private AkkaConfig(File file, boolean isResource) { + this.file = isResource ? null : file; + this.fileContent = null; + this.resourceFilename = isResource ? file.getName() : null; + } + private AkkaConfig(String fileContent) { this.file = null; this.fileContent = fileContent; @@ -151,47 +158,45 @@ public class AkkaConfig{ } private void loadFromFile() throws Exception { - Config cfg=null; - if(this.file!=null) { - cfg=ConfigFactory.parseFile(this.file); - } else if(this.fileContent!=null) { - cfg=ConfigFactory.parseString(this.fileContent); - } else if(this.resourceFilename!=null) { - cfg=ConfigFactory.parseResources(this.getClass(), this.resourceFilename); + Config cfg = null; + if (this.file != null) { + cfg = ConfigFactory.parseFile(this.file); + } else if (this.fileContent != null) { + cfg = ConfigFactory.parseString(this.fileContent); + } else if (this.resourceFilename != null) { + cfg = ConfigFactory.parseResources(this.getClass(), this.resourceFilename); } - if(cfg!=null) { - this.cluserConfig=new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster")); - } else - { + if (cfg != null) { + this.cluserConfig = + new ClusterConfig(cfg.getConfig("odl-cluster-data").getConfig("akka").getConfig("cluster")); + } else { LOG.warn("unable to parse config file"); - this.cluserConfig=null; + this.cluserConfig = null; } } - public boolean isCluster() - { - return this.cluserConfig!=null?this.cluserConfig.isCluster():false; + public boolean isCluster() { + return this.cluserConfig != null ? this.cluserConfig.isCluster() : false; } - public static AkkaConfig load() throws Exception - { + + public static AkkaConfig load() throws Exception { return load(DEFAULT_FILENAME); } - public static AkkaConfig load(String filename) throws Exception - { - return load(filename,false); + public static AkkaConfig load(String filename) throws Exception { + return load(filename, false); } - public static AkkaConfig load(String filename,boolean isResource) throws Exception - { - AkkaConfig cfg=new AkkaConfig(new File(filename),isResource); + + public static AkkaConfig load(String filename, boolean isResource) throws Exception { + AkkaConfig cfg = new AkkaConfig(new File(filename), isResource); cfg.loadFromFile(); return cfg; } - public static AkkaConfig loadContent(String content) throws Exception - { - AkkaConfig cfg=new AkkaConfig(content); + + public static AkkaConfig loadContent(String content) throws Exception { + AkkaConfig cfg = new AkkaConfig(content); cfg.loadFromFile(); return cfg; @@ -199,8 +204,4 @@ public class AkkaConfig{ - - - - } diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/UserScopes.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/UserScopes.java index 5dd503c73..c7772d8cb 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/UserScopes.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/utils/UserScopes.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,33 +14,33 @@ * 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.websocketmanager2.utils; import org.json.JSONArray; public class UserScopes { - private JSONArray scopes; + private JSONArray scopes; - /** - * - * @param jsonArray array of Strings - */ - public void setScopes(JSONArray jsonArray) { - this.scopes = jsonArray; - } + /** + * + * @param jsonArray array of Strings + */ + public void setScopes(JSONArray jsonArray) { + this.scopes = jsonArray; + } - public boolean hasScope(String scope) { - if (this.scopes == null) - return false; - for (int i = 0, l = this.scopes.length(); i < l; i++) { - if (this.scopes.get(i).toString().equals(scope)) { - return true; - } - } + public boolean hasScope(String scope) { + if (this.scopes == null) + return false; + for (int i = 0, l = this.scopes.length(); i < l; i++) { + if (this.scopes.get(i).toString().equals(scope)) { + return true; + } + } - return false; - } + return false; + } } diff --git a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/websocket/SyncWebSocketClient.java b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/websocket/SyncWebSocketClient.java index fb2384ee5..6512e7171 100644 --- a/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/websocket/SyncWebSocketClient.java +++ b/sdnr/wt/websocketmanager2/provider/src/main/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/websocket/SyncWebSocketClient.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.websocket; import java.net.URI; @@ -29,87 +29,93 @@ import org.slf4j.LoggerFactory; public class SyncWebSocketClient extends WebSocketClient { - public interface WebsocketEventHandler{ - void onMessageReceived(String message); - void onOpen(ServerHandshake arg0); - void onClose(int arg0, String arg1, boolean arg2); - void onError(Exception e); - } - - private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName()); - private String messageToSend; - private final List handlers; - public SyncWebSocketClient(URI serverUri) { - super(serverUri); - this.handlers = new ArrayList(); - } - - public SyncWebSocketClient(String uri) throws URISyntaxException { - this(new URI(uri)); - } - public void addEventHandler(WebsocketEventHandler h) { - this.handlers.add(h); - } - public void removeEventHandler(WebsocketEventHandler h) { - this.handlers.remove(h); - } - - @Override - public void onClose(int arg0, String arg1, boolean arg2) { - LOG.debug("socket closed: {} {} {}", arg0, arg1, arg2); - for(WebsocketEventHandler h:this.handlers) { - h.onClose(arg0,arg1,arg2); - } - } - - @Override - public void onError(Exception arg0) { - LOG.warn("error on socket: {}", arg0.getMessage()); - for(WebsocketEventHandler h:this.handlers) { - h.onError(arg0); - } - } - - @Override - public void onMessage(String arg0) { - LOG.debug("received message: {}", arg0); - for(WebsocketEventHandler h:this.handlers) { - h.onMessageReceived(arg0); - } - } - - @Override - public void onOpen(ServerHandshake arg0) { - LOG.debug("socket opened"); - if (this.messageToSend != null) { - LOG.debug("try to send: " + this.messageToSend); - this.send(this.messageToSend); - this.messageToSend = null; - } - for(WebsocketEventHandler h:this.handlers) { - h.onOpen(arg0); - } - } - - public void openAndSendAsync(String message) { - this.messageToSend = message; - this.connect(); - } - - public void openAndSendAndCloseSync(String message) { - try { - this.connectBlocking(); - } catch (InterruptedException e) { - LOG.warn("problem connecting:" + e.getMessage()); - Thread.currentThread().interrupt(); - } - this.send(message); - try { - this.closeBlocking(); - } catch (InterruptedException e) { - LOG.warn("problem disconnecting:" + e.getMessage()); + public interface WebsocketEventHandler { + void onMessageReceived(String message); + + void onOpen(ServerHandshake arg0); + + void onClose(int arg0, String arg1, boolean arg2); + + void onError(Exception e); + } + + private static final Logger LOG = LoggerFactory.getLogger(SyncWebSocketClient.class.getName()); + private String messageToSend; + private final List handlers; + + public SyncWebSocketClient(URI serverUri) { + super(serverUri); + this.handlers = new ArrayList(); + } + + public SyncWebSocketClient(String uri) throws URISyntaxException { + this(new URI(uri)); + } + + public void addEventHandler(WebsocketEventHandler h) { + this.handlers.add(h); + } + + public void removeEventHandler(WebsocketEventHandler h) { + this.handlers.remove(h); + } + + @Override + public void onClose(int arg0, String arg1, boolean arg2) { + LOG.debug("socket closed: {} {} {}", arg0, arg1, arg2); + for (WebsocketEventHandler h : this.handlers) { + h.onClose(arg0, arg1, arg2); + } + } + + @Override + public void onError(Exception arg0) { + LOG.warn("error on socket: {}", arg0.getMessage()); + for (WebsocketEventHandler h : this.handlers) { + h.onError(arg0); + } + } + + @Override + public void onMessage(String arg0) { + LOG.debug("received message: {}", arg0); + for (WebsocketEventHandler h : this.handlers) { + h.onMessageReceived(arg0); + } + } + + @Override + public void onOpen(ServerHandshake arg0) { + LOG.debug("socket opened"); + if (this.messageToSend != null) { + LOG.debug("try to send: " + this.messageToSend); + this.send(this.messageToSend); + this.messageToSend = null; + } + for (WebsocketEventHandler h : this.handlers) { + h.onOpen(arg0); + } + } + + public void openAndSendAsync(String message) { + this.messageToSend = message; + this.connect(); + } + + public void openAndSendAndCloseSync(String message) { + try { + this.connectBlocking(); + } catch (InterruptedException e) { + LOG.warn("problem connecting:" + e.getMessage()); + Thread.currentThread().interrupt(); + } + this.send(message); + try { + this.closeBlocking(); + } catch (InterruptedException e) { + LOG.warn("problem disconnecting:" + e.getMessage()); Thread.currentThread().interrupt(); - } - } + } + } } diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/AkkaConfigTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/AkkaConfigTest.java index 2460ca42f..486aaaccc 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/AkkaConfigTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/AkkaConfigTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.*; @@ -34,7 +34,7 @@ public class AkkaConfigTest { @Test public void test() throws URISyntaxException, IOException { - AkkaConfig config=null; + AkkaConfig config = null; try { //config = AkkaConfig.load("akka-singlenode.cfg", true); config = AkkaConfig.loadContent(loadResourceContentAsString("akka-singlenode.cfg")); @@ -42,25 +42,26 @@ public class AkkaConfigTest { e.printStackTrace(); fail("error loading singlenode config"); } - assertEquals("no singlenode config detected",false,config.isCluster()); - assertEquals("more than one node detected",1,config.getClusterConfig().getSeedNodes().size()); + assertEquals("no singlenode config detected", false, config.isCluster()); + assertEquals("more than one node detected", 1, config.getClusterConfig().getSeedNodes().size()); try { config = AkkaConfig.loadContent(loadResourceContentAsString("akka-cluster.cfg")); } catch (Exception e) { fail("error loading cluster config"); } - assertEquals("no cluster config detected",true,config.isCluster()); - assertTrue("only one node detected",config.getClusterConfig().getSeedNodes().size()>1); + assertEquals("no cluster config detected", true, config.isCluster()); + assertTrue("only one node detected", config.getClusterConfig().getSeedNodes().size() > 1); } - public static String loadResourceContentAsString(String resourceName) throws URISyntaxException, FileNotFoundException, IOException { + public static String loadResourceContentAsString(String resourceName) + throws URISyntaxException, FileNotFoundException, IOException { StringBuilder sb = new StringBuilder(); ClassLoader classLoader = AkkaConfigTest.class.getClassLoader(); File file = Paths.get(classLoader.getResource(resourceName).toURI()).toFile(); - try(BufferedReader br = new BufferedReader(new FileReader(file))) { + try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line = br.readLine(); while (line != null) { diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/UserScopeTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/UserScopeTest.java index 52eedb994..3f04ac09d 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/UserScopeTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/UserScopeTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.*; @@ -25,15 +25,15 @@ import org.onap.ccsdk.features.sdnr.wt.websocketmanager2.utils.UserScopes; public class UserScopeTest { - private static final String SCOPE1 ="scope1"; - private static final String SCOPE2 ="scope2"; - private static final String SCOPE3 ="scope3"; - private static final String SCOPE4 ="scope4"; + private static final String SCOPE1 = "scope1"; + private static final String SCOPE2 = "scope2"; + private static final String SCOPE3 = "scope3"; + private static final String SCOPE4 = "scope4"; @Test public void test() { UserScopes scopes1 = new UserScopes(); - JSONArray json1=new JSONArray(); + JSONArray json1 = new JSONArray(); json1.put(SCOPE1); json1.put(SCOPE2); json1.put(SCOPE3); diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsockerProviderTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsockerProviderTest.java index 57d79602b..d6f0bcf4c 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsockerProviderTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsockerProviderTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.*; @@ -28,7 +28,7 @@ public class WebsockerProviderTest extends Mockito { @Test public void test() { - RpcProviderService rpcProviderServiceMock = mock(RpcProviderService.class); + RpcProviderService rpcProviderServiceMock = mock(RpcProviderService.class); HttpService httpService = mock(HttpService.class); try (WebSocketManagerProvider provider = new WebSocketManagerProvider();) { diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketClientTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketClientTest.java index f56e1a686..df68ff3a2 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketClientTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketClientTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.*; @@ -41,7 +41,7 @@ public class WebsocketClientTest extends Mockito { client.close(); } catch (Exception e) { e.printStackTrace(); - fail("Exception "+e.getMessage()); + fail("Exception " + e.getMessage()); } } diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketMessageTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketMessageTest.java index f769dd40d..0e4db163a 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketMessageTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketMessageTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.assertTrue; @@ -30,7 +30,8 @@ public class WebsocketMessageTest { private static final String MSG1 = "{\"data\":\"scopes\",\"scopes\":[\"scope1\"]}"; private static final String MSG2 = "{}"; - private static final String MSG3 = "{\""+WebSocketManagerSocket.KEY_NODENAME+":\"xy\","+WebSocketManagerSocket.KEY_EVENTTYPE+":\"zu\"}"; + private static final String MSG3 = "{\"" + WebSocketManagerSocket.KEY_NODENAME + ":\"xy\"," + + WebSocketManagerSocket.KEY_EVENTTYPE + ":\"zu\"}"; private static final String MSG4 = "{ Not correct messga}"; @Test @@ -58,8 +59,8 @@ public class WebsocketMessageTest { private String expected; - public MyWebSocketManagerSocket() { - } + public MyWebSocketManagerSocket() {} + void setExpected(String expected) { this.expected = expected; } @@ -67,7 +68,7 @@ public class WebsocketMessageTest { @Override public void send(String msg) { System.out.println(msg); - assertTrue("Expected '"+expected+"' answer '"+msg+"'", msg.contains(expected)); + assertTrue("Expected '" + expected + "' answer '" + msg + "'", msg.contains(expected)); } } diff --git a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketServerConnectTest.java b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketServerConnectTest.java index e59c343f2..c9a96f5f3 100644 --- a/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketServerConnectTest.java +++ b/sdnr/wt/websocketmanager2/provider/src/test/java/org/onap/ccsdk/features/sdnr/wt/websocketmanager2/test/WebsocketServerConnectTest.java @@ -1,4 +1,4 @@ -/******************************************************************************* +/* * ============LICENSE_START======================================================================== * ONAP : ccsdk feature sdnr wt * ================================================================================================= @@ -14,7 +14,7 @@ * 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.websocketmanager2.test; import static org.junit.Assert.assertEquals; -- cgit 1.2.3-korg