Ordered message execution ensured
[openflowjava.git] / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / impl / integration / MockPlugin.java
index 73d88fa5e9de8f7722a57364250563b81d13024c..340cba23f2aa05730f59730d3b31d9c11a73c4e6 100644 (file)
@@ -9,6 +9,7 @@ import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;\r
 \r
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;\r
+import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;\r
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;\r
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;\r
@@ -41,10 +42,11 @@ import com.google.common.util.concurrent.SettableFuture;
  * @author michal.polkorab\r
  *\r
  */\r
-public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, SystemNotificationsListener {\r
+public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, \r
+        SystemNotificationsListener, ConnectionReadyListener {\r
 \r
-    private static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);\r
-    private ConnectionAdapter adapter;\r
+    protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);\r
+    protected ConnectionAdapter adapter;\r
     private SettableFuture<Void> finishedFuture;\r
     private int idleCounter = 0;\r
 \r
@@ -61,6 +63,7 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
         this.adapter = connection;\r
         connection.setMessageListener(this);\r
         connection.setSystemListener(this);\r
+        connection.setConnectionReadyListener(this);\r
     }\r
 \r
     @Override\r
@@ -70,18 +73,20 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
     }\r
 \r
     @Override\r
-    public void onEchoRequestMessage(EchoRequestMessage notification) {\r
-        LOGGER.debug("EchoRequest message received");\r
-        LOGGER.debug("Building EchoReplyInput");\r
-        EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
-        replyBuilder.setVersion((short) 4);\r
-        replyBuilder.setXid(notification.getXid());\r
-        EchoReplyInput echoReplyInput = replyBuilder.build();\r
-        LOGGER.debug("EchoReplyInput built");\r
-        LOGGER.debug("Going to send EchoReplyInput");\r
-        adapter.echoReply(echoReplyInput);\r
-        LOGGER.debug("EchoReplyInput sent");\r
-        LOGGER.debug("adapter: "+adapter);\r
+    public void onEchoRequestMessage(final EchoRequestMessage notification) {\r
+        new Thread(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                LOGGER.debug("EchoRequest message received");\r
+                EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
+                replyBuilder.setVersion((short) 4);\r
+                replyBuilder.setXid(notification.getXid());\r
+                EchoReplyInput echoReplyInput = replyBuilder.build();\r
+                adapter.echoReply(echoReplyInput);\r
+                LOGGER.debug("EchoReplyInput sent");\r
+                LOGGER.debug("adapter: "+adapter);\r
+            }\r
+        }).start();\r
     }\r
 \r
     @Override\r
@@ -104,17 +109,31 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
 \r
     @Override\r
     public void onHelloMessage(HelloMessage notification) {\r
-        LOGGER.debug("adapter: "+adapter);\r
-        LOGGER.debug("Hello message received");\r
-        HelloInputBuilder hib = new HelloInputBuilder();\r
+        new Thread(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                LOGGER.debug("Hello message received");\r
+                HelloInputBuilder hib = new HelloInputBuilder();\r
+                hib.setVersion((short) 4);\r
+                hib.setXid(2L);\r
+                HelloInput hi = hib.build();\r
+                adapter.hello(hi);\r
+                LOGGER.debug("hello msg sent");\r
+                new Thread(new Runnable() {\r
+                    @Override\r
+                    public void run() {\r
+                        sendFeaturesReply();\r
+                    }\r
+                }).start();\r
+                LOGGER.debug("adapter: "+adapter);\r
+            }\r
+        }).start();\r
+    }\r
+    \r
+    protected void sendFeaturesReply() {\r
         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();\r
-        hib.setVersion((short) 4);\r
-        hib.setXid(2L);\r
         featuresBuilder.setVersion((short) 4);\r
         featuresBuilder.setXid(3L);\r
-        HelloInput hi = hib.build();\r
-        adapter.hello(hi);\r
-        LOGGER.debug("hello msg sent");\r
         GetFeaturesInput featuresInput = featuresBuilder.build();\r
         try {\r
             LOGGER.debug("Going to send featuresRequest");\r
@@ -131,10 +150,8 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
             }\r
         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
             LOGGER.error(e.getMessage(), e);\r
-            // TODO - Collect exceptions and check for existence in tests\r
         }\r
         LOGGER.info("After FeaturesReply message");\r
-        LOGGER.debug("adapter: "+adapter);\r
     }\r
 \r
     protected void shutdown() {\r
@@ -146,7 +163,7 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
                 Future<Boolean> disconnect = adapter.disconnect();\r
                 disconnect.get();\r
                 LOGGER.info("Disconnected");\r
-            }\r
+            } \r
         } catch (Exception e) {\r
             LOGGER.error(e.getMessage(), e);\r
         }\r
@@ -202,6 +219,11 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
     public int getIdleCounter() {\r
         return idleCounter;\r
     }\r
+    \r
+    @Override\r
+    public void onConnectionReady() {\r
+        LOGGER.debug("connection ready notification arrived");\r
+    }\r
 \r
 \r
 }\r