5603a473f3e26cb54ceae1fe61f4bcf211d57a51
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / integration / MockPlugin.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.integration;\r
3 \r
4 import java.lang.reflect.InvocationTargetException;\r
5 import java.net.InetAddress;\r
6 import java.util.Arrays;\r
7 import java.util.concurrent.ExecutionException;\r
8 import java.util.concurrent.TimeUnit;\r
9 import java.util.concurrent.TimeoutException;\r
10 \r
11 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;\r
12 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
13 import org.opendaylight.openflowjava.protocol.impl.util.BufferHelper;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;\r
33 import org.opendaylight.yangtools.yang.common.RpcError;\r
34 import org.opendaylight.yangtools.yang.common.RpcResult;\r
35 import org.slf4j.Logger;\r
36 import org.slf4j.LoggerFactory;\r
37 \r
38 import com.google.common.util.concurrent.SettableFuture;\r
39 \r
40 /**\r
41  * @author michal.polkorab\r
42  *\r
43  */\r
44 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, SystemNotificationsListener {\r
45 \r
46     private static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);\r
47     private ConnectionAdapter adapter;\r
48     private SettableFuture<Void> finishedFuture;\r
49     \r
50     public MockPlugin() {\r
51         finishedFuture = SettableFuture.create();\r
52     }\r
53     \r
54     @Override\r
55     public void onSwitchConnected(ConnectionAdapter connection) {\r
56         LOGGER.debug("onSwitchConnected");\r
57         this.adapter = connection;\r
58         connection.setMessageListener(this);\r
59         connection.setSystemListener(this);\r
60     }\r
61 \r
62     @Override\r
63     public boolean accept(InetAddress switchAddress) {\r
64         // TODO Auto-generated method stub\r
65         return true;\r
66     }\r
67 \r
68     @Override\r
69     public void onEchoRequestMessage(EchoRequestMessage notification) {\r
70         LOGGER.debug("EchoRequest message received");\r
71         LOGGER.debug("Building EchoReplyInput");\r
72         EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
73         try {\r
74             BufferHelper.setupHeader(replyBuilder);\r
75         } catch (NoSuchMethodException | SecurityException\r
76                 | IllegalAccessException | IllegalArgumentException\r
77                 | InvocationTargetException e) {\r
78             LOGGER.error(e.getMessage(), e);\r
79         }\r
80         replyBuilder.setXid(notification.getXid());\r
81         EchoReplyInput echoReplyInput = replyBuilder.build();\r
82         LOGGER.debug("EchoReplyInput built");\r
83         LOGGER.debug("Going to send EchoReplyInput");\r
84         adapter.echoReply(echoReplyInput);\r
85         LOGGER.debug("EchoReplyInput sent");\r
86     }\r
87 \r
88     @Override\r
89     public void onErrorMessage(ErrorMessage notification) {\r
90         LOGGER.debug("Error message received");\r
91         \r
92     }\r
93 \r
94     @Override\r
95     public void onExperimenterMessage(ExperimenterMessage notification) {\r
96         LOGGER.debug("Experimenter message received");\r
97         \r
98     }\r
99 \r
100     @Override\r
101     public void onFlowRemovedMessage(FlowRemovedMessage notification) {\r
102         LOGGER.debug("FlowRemoved message received");\r
103         \r
104     }\r
105 \r
106     @Override\r
107     public void onHelloMessage(HelloMessage notification) {\r
108         LOGGER.debug("Hello message received");\r
109         HelloInputBuilder hib = new HelloInputBuilder();\r
110         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();\r
111         try {\r
112             BufferHelper.setupHeader(hib);\r
113             BufferHelper.setupHeader(featuresBuilder);\r
114         } catch (Exception e) {\r
115            LOGGER.error(e.getMessage(), e);\r
116         }\r
117         HelloInput hi = hib.build();\r
118         adapter.hello(hi);\r
119         LOGGER.debug("hello msg sent");\r
120         GetFeaturesInput featuresInput = featuresBuilder.build();\r
121         try {\r
122             LOGGER.debug("Going to send featuresRequest");\r
123             RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(\r
124                     featuresInput).get(2500, TimeUnit.MILLISECONDS);\r
125             if (rpcResult.isSuccessful()) {\r
126                 byte[] byteArray = rpcResult.getResult().getDatapathId()\r
127                         .toByteArray();\r
128                 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));\r
129             } else {\r
130                 RpcError rpcError = rpcResult.getErrors().iterator().next();\r
131                 LOGGER.warn("rpcResult failed: "\r
132                         + rpcError.getCause().getMessage(), rpcError.getCause());\r
133             }\r
134         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
135             LOGGER.error(e.getMessage(), e);\r
136         }\r
137         LOGGER.info("After FeaturesReply message - disconnecting");\r
138         adapter.disconnect();\r
139         finishedFuture.set(null);\r
140     }\r
141 \r
142     @Override\r
143     public void onMultipartReplyMessage(MultipartReplyMessage notification) {\r
144         LOGGER.debug("MultipartReply message received");\r
145         \r
146     }\r
147 \r
148     @Override\r
149     public void onMultipartRequestMessage(MultipartRequestMessage notification) {\r
150         LOGGER.debug("MultipartRequest message received");\r
151         \r
152     }\r
153 \r
154     @Override\r
155     public void onPacketInMessage(PacketInMessage notification) {\r
156         LOGGER.debug("PacketIn message received");\r
157         \r
158     }\r
159 \r
160     @Override\r
161     public void onPortStatusMessage(PortStatusMessage notification) {\r
162         LOGGER.debug("PortStatus message received");\r
163         \r
164     }\r
165 \r
166     @Override\r
167     public void onDisconnectEvent(DisconnectEvent notification) {\r
168         LOGGER.debug("disconnection ocured: "+notification.getInfo());\r
169     }\r
170 \r
171     public SettableFuture<Void> getFinishedFuture() {\r
172         return finishedFuture;\r
173     }\r
174 \r
175 \r
176 }\r