--- /dev/null
+/* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
+package org.opendaylight.openflowjava.protocol.impl.integration;\r
+\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.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
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;\r
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;\r
+import org.opendaylight.yangtools.yang.common.RpcError;\r
+import org.opendaylight.yangtools.yang.common.RpcResult;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.google.common.util.concurrent.SettableFuture;\r
+\r
+/**\r
+ * @author michal.polkorab\r
+ *\r
+ */\r
+public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, SystemNotificationsListener {\r
+\r
+ private static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);\r
+ private ConnectionAdapter adapter;\r
+ private SettableFuture<Void> finishedFuture;\r
+ 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.info("onSwitchConnected: " + connection);\r
+ this.adapter = connection;\r
+ connection.setMessageListener(this);\r
+ connection.setSystemListener(this);\r
+ }\r
+\r
+ @Override\r
+ public boolean accept(InetAddress switchAddress) {\r
+ // TODO Auto-generated method stub\r
+ return true;\r
+ }\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
+ }\r
+\r
+ @Override\r
+ public void onErrorMessage(ErrorMessage notification) {\r
+ LOGGER.debug("Error message received");\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void onExperimenterMessage(ExperimenterMessage notification) {\r
+ LOGGER.debug("Experimenter message received");\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void onFlowRemovedMessage(FlowRemovedMessage notification) {\r
+ LOGGER.debug("FlowRemoved message received");\r
+ \r
+ }\r
+\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
+ 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
+ RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(\r
+ featuresInput).get(2500, TimeUnit.MILLISECONDS);\r
+ if (rpcResult.isSuccessful()) {\r
+ byte[] byteArray = rpcResult.getResult().getDatapathId()\r
+ .toByteArray();\r
+ LOGGER.info("DatapathId: " + Arrays.toString(byteArray));\r
+ } else {\r
+ RpcError rpcError = rpcResult.getErrors().iterator().next();\r
+ LOGGER.warn("rpcResult failed: "\r
+ + rpcError.getCause().getMessage(), rpcError.getCause());\r
+ }\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
+ finishedFuture.set(null);\r
+ }\r
+\r
+ @Override\r
+ public void onMultipartReplyMessage(MultipartReplyMessage notification) {\r
+ LOGGER.debug("MultipartReply message received");\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void onMultipartRequestMessage(MultipartRequestMessage notification) {\r
+ LOGGER.debug("MultipartRequest message received");\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void onPacketInMessage(PacketInMessage notification) {\r
+ LOGGER.debug("PacketIn message received");\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void onPortStatusMessage(PortStatusMessage notification) {\r
+ LOGGER.debug("PortStatus message received");\r
+ \r
+ }\r
+\r
+ @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
+ return finishedFuture;\r
+ }\r
+\r
+ @Override\r
+ public void onSwitchIdleEvent(SwitchIdleEvent notification) {\r
+ LOGGER.debug("switch status: "+notification.getInfo());\r
+ idleCounter ++;\r
+ }\r
+ \r
+ public int getIdleCounter() {\r
+ return idleCounter;\r
+ }\r
+\r
+\r
+}\r