Integration test, SimpleClient bundle
[openflowjava.git] / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / impl / integration / MockPlugin.java
similarity index 83%
rename from openflow-protocol-impl/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java
rename to openflow-protocol-it/src/test/java/org/opendaylight/openflowjava/protocol/impl/integration/MockPlugin.java
index 3ae7a9d8c78f3636be1b3129fbdaa14c211d4af3..40419621e81827e7d56d68ad23ffc2d295180dfd 100644 (file)
@@ -1,16 +1,15 @@
 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
 package org.opendaylight.openflowjava.protocol.impl.integration;\r
 \r
-import java.lang.reflect.InvocationTargetException;\r
 import java.net.InetAddress;\r
 import java.util.Arrays;\r
 import java.util.concurrent.ExecutionException;\r
+import java.util.concurrent.Future;\r
 import java.util.concurrent.TimeUnit;\r
 import java.util.concurrent.TimeoutException;\r
 \r
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;\r
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
-import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\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
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;\r
@@ -50,12 +49,14 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
     private int idleCounter = 0;\r
     \r
     public MockPlugin() {\r
+        LOGGER.info("Creating MockPlugin");\r
         finishedFuture = SettableFuture.create();\r
+        LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
     }\r
     \r
     @Override\r
     public void onSwitchConnected(ConnectionAdapter connection) {\r
-        LOGGER.debug("onSwitchConnected");\r
+        LOGGER.info("onSwitchConnected: " + connection);\r
         this.adapter = connection;\r
         connection.setMessageListener(this);\r
         connection.setSystemListener(this);\r
@@ -72,19 +73,14 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
         LOGGER.debug("EchoRequest message received");\r
         LOGGER.debug("Building EchoReplyInput");\r
         EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
-        try {\r
-            BufferHelper.setupHeader(replyBuilder);\r
-        } catch (NoSuchMethodException | SecurityException\r
-                | IllegalAccessException | IllegalArgumentException\r
-                | InvocationTargetException e) {\r
-            LOGGER.error(e.getMessage(), e);\r
-        }\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
     }\r
 \r
     @Override\r
@@ -107,15 +103,14 @@ 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
         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();\r
-        try {\r
-            BufferHelper.setupHeader(hib);\r
-            BufferHelper.setupHeader(featuresBuilder);\r
-        } catch (Exception e) {\r
-           LOGGER.error(e.getMessage(), e);\r
-        }\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
@@ -127,7 +122,7 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
             if (rpcResult.isSuccessful()) {\r
                 byte[] byteArray = rpcResult.getResult().getDatapathId()\r
                         .toByteArray();\r
-                LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));\r
+                LOGGER.info("DatapathId: " + Arrays.toString(byteArray));\r
             } else {\r
                 RpcError rpcError = rpcResult.getErrors().iterator().next();\r
                 LOGGER.warn("rpcResult failed: "\r
@@ -135,9 +130,25 @@ 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
+        LOGGER.debug("adapter: "+adapter);\r
+        try {\r
+            LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
+            Thread.sleep(500);\r
+//            if (adapter != null) {\r
+                Future<Boolean> disconnect = adapter.disconnect();\r
+                disconnect.get();\r
+                LOGGER.info("Disconnected");\r
+//            }\r
+        } catch (Exception e) {\r
+            LOGGER.error(e.getMessage(), e);\r
         }\r
-        LOGGER.info("After FeaturesReply message - disconnecting");\r
-        adapter.disconnect();\r
         finishedFuture.set(null);\r
     }\r
 \r
@@ -168,6 +179,7 @@ public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHan
     @Override\r
     public void onDisconnectEvent(DisconnectEvent notification) {\r
         LOGGER.debug("disconnection ocured: "+notification.getInfo());\r
+        LOGGER.debug("adapter: "+adapter);\r
     }\r
 \r
     public SettableFuture<Void> getFinishedFuture() {\r