Fix of bug in Notificator class 03/5103/4
authorJozef Gloncak <jgloncak@cisco.com>
Mon, 3 Feb 2014 14:58:26 +0000 (15:58 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Thu, 6 Feb 2014 07:03:15 +0000 (08:03 +0100)
Deleting of listner from listenersByStreamName map was corrected
try, finally block was added for locking, unlocking of access to map

Change-Id: I60ecb9c247af18436f348d4f4827c7f75fdfaeaa
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/streams/listeners/Notificator.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java [deleted file]
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java

index b0140ec83a178f6bc9f9bbaecf5a415ff812bb24..d1cb25861ae0496c299cc9f4c67ebf5314a871e4 100644 (file)
@@ -30,10 +30,13 @@ public class Notificator {
 
     public static ListenerAdapter createListener(InstanceIdentifier path, String streamName) {
         ListenerAdapter listener = new ListenerAdapter(path, streamName);
-        lock.lock();
-        listenersByInstanceIdentifier.put(path, listener);
-        listenersByStreamName.put(streamName, listener);
-        lock.unlock();
+        try {
+            lock.lock();
+            listenersByInstanceIdentifier.put(path, listener);
+            listenersByStreamName.put(streamName, listener);
+        } finally {
+            lock.unlock();
+        }
         return listener;
     }
 
@@ -63,10 +66,13 @@ public class Notificator {
             } catch (Exception e) {
             }
         }
-        lock.lock();
-        listenersByStreamName = new ConcurrentHashMap<>();
-        listenersByInstanceIdentifier = new ConcurrentHashMap<>();
-        lock.unlock();
+        try {
+            lock.lock();
+            listenersByStreamName = new ConcurrentHashMap<>();
+            listenersByInstanceIdentifier = new ConcurrentHashMap<>();
+        } finally {
+            lock.unlock();
+        }
     }
 
     public static void removeListenerIfNoSubscriberExists(ListenerAdapter listener) {
@@ -81,10 +87,13 @@ public class Notificator {
                 listener.close();
             } catch (Exception e) {
             }
-            lock.lock();
-            listenersByInstanceIdentifier.remove(listener.getPath());
-            listenersByStreamName.remove(listener).getStreamName();
-            lock.unlock();
+            try {
+                lock.lock();
+                listenersByInstanceIdentifier.remove(listener.getPath());
+                listenersByStreamName.remove(listener.getStreamName());
+            } finally {
+                lock.unlock();
+            }
         }
     }
 
diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/iml/varioustests/VariousTest.java
deleted file mode 100644 (file)
index cdfed8e..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.controller.sal.restconf.iml.varioustests;
-
-import org.junit.Ignore;
-import org.junit.Test;
-import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
-import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
-import org.opendaylight.yangtools.yang.data.api.CompositeNode;
-import org.w3c.dom.Document;
-
-
-public class VariousTest {
-
-    @Ignore
-    @Test
-    public void test() {
-        String[] split = "/something:dfsa/s:sda".split("/");
-        System.out.println(split.length);
-        for (String str : split) {
-            System.out.println(">"+str+"<");    
-        }        
-        
-    }
-    
-    @Test
-    public void loadXml() {
-        TestUtils.readInputToCnSn("/varioustest/xmldata.xml", XmlToCompositeNodeProvider.INSTANCE);
-//        TestUtils.normalizeCompositeNode(compositeNode, modules, schemaNodePath)
-    }
-    
-    @Test
-    public void buildXml() {
-//        Document doc;
-//        doc.createElementNS(namespaceURI, qualifiedName)
-    }
-    
-
-}
index bff63b88ad5ba71cadf5642f51cf16d0edb8a754..845d54ea1f568388ff6c7ba1400f0e76cac9ccf5 100644 (file)
@@ -24,6 +24,8 @@ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
 import io.netty.handler.codec.http.websocketx.WebSocketVersion;
 
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
 import java.net.URI;
 
 import org.slf4j.Logger;
@@ -81,8 +83,9 @@ public class WebSocketClient  {
         clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6})));
     }
 
-    public void close() throws InterruptedException {
-        clientChannel.writeAndFlush(new CloseWebSocketFrame());
+    public void close(String reasonText) throws InterruptedException {
+        CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000,reasonText);
+        clientChannel.writeAndFlush(closeWebSocketFrame);
 
         // WebSocketClientHandler will close the connection when the server
         // responds to the CloseWebSocketFrame.
@@ -95,17 +98,32 @@ public class WebSocketClient  {
         if (args.length > 0) {
             uri = new URI(args[0]);
         } else {
-            uri = new URI("http://192.168.11.1:8181/opendaylight-inventory:nodes");
+            uri = new URI("http://192.168.1.101:8181/opendaylight-inventory:nodes");
         }
         IClientMessageCallback messageCallback = new ClientMessageCallback();
         WebSocketClient webSocketClient = new WebSocketClient(uri, messageCallback);
         webSocketClient.connect();
+
+        while (true) {
+            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
+            String input = br.readLine();
+            if (input.equals("q")) {
+                System.out.print("Would you like to close stream? (Y = yes, empty = yes)\n");
+                input = br.readLine();
+                if (input.equals("yes") || input.isEmpty()) {
+                    webSocketClient.close("opendaylight-inventory:nodes");
+                    break;
+                }
+            }
+        }
     }
 
     private static class ClientMessageCallback implements IClientMessageCallback {
         @Override
         public void onMessageReceived(Object message) {
-            logger.info("received message {}", ((TextWebSocketFrame)message).text());
+            if (message instanceof TextWebSocketFrame) {
+                logger.info("received message {}"+ ((TextWebSocketFrame)message).text());
+            }
         }
     }