340cba23f2aa05730f59730d3b31d9c11a73c4e6
[openflowjava.git] / openflow-protocol-it / 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.net.InetAddress;\r
5 import java.util.Arrays;\r
6 import java.util.concurrent.ExecutionException;\r
7 import java.util.concurrent.Future;\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.ConnectionReadyListener;\r
13 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\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.SwitchIdleEvent;\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;\r
34 import org.opendaylight.yangtools.yang.common.RpcError;\r
35 import org.opendaylight.yangtools.yang.common.RpcResult;\r
36 import org.slf4j.Logger;\r
37 import org.slf4j.LoggerFactory;\r
38 \r
39 import com.google.common.util.concurrent.SettableFuture;\r
40 \r
41 /**\r
42  * @author michal.polkorab\r
43  *\r
44  */\r
45 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, \r
46         SystemNotificationsListener, ConnectionReadyListener {\r
47 \r
48     protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);\r
49     protected ConnectionAdapter adapter;\r
50     private SettableFuture<Void> finishedFuture;\r
51     private int idleCounter = 0;\r
52 \r
53     /** Creates MockPlugin */\r
54     public MockPlugin() {\r
55         LOGGER.info("Creating MockPlugin");\r
56         finishedFuture = SettableFuture.create();\r
57         LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
58     }\r
59     \r
60     @Override\r
61     public void onSwitchConnected(ConnectionAdapter connection) {\r
62         LOGGER.info("onSwitchConnected: " + connection);\r
63         this.adapter = connection;\r
64         connection.setMessageListener(this);\r
65         connection.setSystemListener(this);\r
66         connection.setConnectionReadyListener(this);\r
67     }\r
68 \r
69     @Override\r
70     public boolean accept(InetAddress switchAddress) {\r
71         // TODO Auto-generated method stub\r
72         return true;\r
73     }\r
74 \r
75     @Override\r
76     public void onEchoRequestMessage(final EchoRequestMessage notification) {\r
77         new Thread(new Runnable() {\r
78             @Override\r
79             public void run() {\r
80                 LOGGER.debug("EchoRequest message received");\r
81                 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
82                 replyBuilder.setVersion((short) 4);\r
83                 replyBuilder.setXid(notification.getXid());\r
84                 EchoReplyInput echoReplyInput = replyBuilder.build();\r
85                 adapter.echoReply(echoReplyInput);\r
86                 LOGGER.debug("EchoReplyInput sent");\r
87                 LOGGER.debug("adapter: "+adapter);\r
88             }\r
89         }).start();\r
90     }\r
91 \r
92     @Override\r
93     public void onErrorMessage(ErrorMessage notification) {\r
94         LOGGER.debug("Error message received");\r
95         \r
96     }\r
97 \r
98     @Override\r
99     public void onExperimenterMessage(ExperimenterMessage notification) {\r
100         LOGGER.debug("Experimenter message received");\r
101         \r
102     }\r
103 \r
104     @Override\r
105     public void onFlowRemovedMessage(FlowRemovedMessage notification) {\r
106         LOGGER.debug("FlowRemoved message received");\r
107         \r
108     }\r
109 \r
110     @Override\r
111     public void onHelloMessage(HelloMessage notification) {\r
112         new Thread(new Runnable() {\r
113             @Override\r
114             public void run() {\r
115                 LOGGER.debug("Hello message received");\r
116                 HelloInputBuilder hib = new HelloInputBuilder();\r
117                 hib.setVersion((short) 4);\r
118                 hib.setXid(2L);\r
119                 HelloInput hi = hib.build();\r
120                 adapter.hello(hi);\r
121                 LOGGER.debug("hello msg sent");\r
122                 new Thread(new Runnable() {\r
123                     @Override\r
124                     public void run() {\r
125                         sendFeaturesReply();\r
126                     }\r
127                 }).start();\r
128                 LOGGER.debug("adapter: "+adapter);\r
129             }\r
130         }).start();\r
131     }\r
132     \r
133     protected void sendFeaturesReply() {\r
134         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();\r
135         featuresBuilder.setVersion((short) 4);\r
136         featuresBuilder.setXid(3L);\r
137         GetFeaturesInput featuresInput = featuresBuilder.build();\r
138         try {\r
139             LOGGER.debug("Going to send featuresRequest");\r
140             RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(\r
141                     featuresInput).get(2500, TimeUnit.MILLISECONDS);\r
142             if (rpcResult.isSuccessful()) {\r
143                 byte[] byteArray = rpcResult.getResult().getDatapathId()\r
144                         .toByteArray();\r
145                 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));\r
146             } else {\r
147                 RpcError rpcError = rpcResult.getErrors().iterator().next();\r
148                 LOGGER.warn("rpcResult failed: "\r
149                         + rpcError.getCause().getMessage(), rpcError.getCause());\r
150             }\r
151         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
152             LOGGER.error(e.getMessage(), e);\r
153         }\r
154         LOGGER.info("After FeaturesReply message");\r
155     }\r
156 \r
157     protected void shutdown() {\r
158         LOGGER.debug("adapter: "+adapter);\r
159         try {\r
160             LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
161             Thread.sleep(500);\r
162             if (adapter != null) {\r
163                 Future<Boolean> disconnect = adapter.disconnect();\r
164                 disconnect.get();\r
165                 LOGGER.info("Disconnected");\r
166             } \r
167         } catch (Exception e) {\r
168             LOGGER.error(e.getMessage(), e);\r
169         }\r
170         finishedFuture.set(null);\r
171     }\r
172 \r
173     @Override\r
174     public void onMultipartReplyMessage(MultipartReplyMessage notification) {\r
175         LOGGER.debug("MultipartReply message received");\r
176         \r
177     }\r
178 \r
179     @Override\r
180     public void onMultipartRequestMessage(MultipartRequestMessage notification) {\r
181         LOGGER.debug("MultipartRequest message received");\r
182         \r
183     }\r
184 \r
185     @Override\r
186     public void onPacketInMessage(PacketInMessage notification) {\r
187         LOGGER.debug("PacketIn message received");\r
188         \r
189     }\r
190 \r
191     @Override\r
192     public void onPortStatusMessage(PortStatusMessage notification) {\r
193         LOGGER.debug("PortStatus message received");\r
194         \r
195     }\r
196 \r
197     @Override\r
198     public void onDisconnectEvent(DisconnectEvent notification) {\r
199         LOGGER.debug("disconnection ocured: "+notification.getInfo());\r
200         LOGGER.debug("adapter: "+adapter);\r
201     }\r
202 \r
203     /**\r
204      * @return finishedFuture object\r
205      */\r
206     public SettableFuture<Void> getFinishedFuture() {\r
207         return finishedFuture;\r
208     }\r
209 \r
210     @Override\r
211     public void onSwitchIdleEvent(SwitchIdleEvent notification) {\r
212         LOGGER.debug("switch status: "+notification.getInfo());\r
213         idleCounter ++;\r
214     }\r
215 \r
216     /**\r
217      * @return number of occured idleEvents\r
218      */\r
219     public int getIdleCounter() {\r
220         return idleCounter;\r
221     }\r
222     \r
223     @Override\r
224     public void onConnectionReady() {\r
225         LOGGER.debug("connection ready notification arrived");\r
226     }\r
227 \r
228 \r
229 }\r