3ba78a42be5baac6ae612ee057a444cc3cf11b1c
[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.SwitchConnectionHandler;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;\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     private int idleCounter = 0;\r
50     \r
51     public MockPlugin() {\r
52         LOGGER.info("Creating MockPlugin");\r
53         finishedFuture = SettableFuture.create();\r
54         LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
55     }\r
56     \r
57     @Override\r
58     public void onSwitchConnected(ConnectionAdapter connection) {\r
59         LOGGER.info("onSwitchConnected: " + connection);\r
60         this.adapter = connection;\r
61         connection.setMessageListener(this);\r
62         connection.setSystemListener(this);\r
63     }\r
64 \r
65     @Override\r
66     public boolean accept(InetAddress switchAddress) {\r
67         // TODO Auto-generated method stub\r
68         return true;\r
69     }\r
70 \r
71     @Override\r
72     public void onEchoRequestMessage(EchoRequestMessage notification) {\r
73         LOGGER.debug("EchoRequest message received");\r
74         LOGGER.debug("Building EchoReplyInput");\r
75         EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();\r
76         replyBuilder.setVersion((short) 4);\r
77         replyBuilder.setXid(notification.getXid());\r
78         EchoReplyInput echoReplyInput = replyBuilder.build();\r
79         LOGGER.debug("EchoReplyInput built");\r
80         LOGGER.debug("Going to send EchoReplyInput");\r
81         adapter.echoReply(echoReplyInput);\r
82         LOGGER.debug("EchoReplyInput sent");\r
83         LOGGER.debug("adapter: "+adapter);\r
84     }\r
85 \r
86     @Override\r
87     public void onErrorMessage(ErrorMessage notification) {\r
88         LOGGER.debug("Error message received");\r
89         \r
90     }\r
91 \r
92     @Override\r
93     public void onExperimenterMessage(ExperimenterMessage notification) {\r
94         LOGGER.debug("Experimenter message received");\r
95         \r
96     }\r
97 \r
98     @Override\r
99     public void onFlowRemovedMessage(FlowRemovedMessage notification) {\r
100         LOGGER.debug("FlowRemoved message received");\r
101         \r
102     }\r
103 \r
104     @Override\r
105     public void onHelloMessage(HelloMessage notification) {\r
106         LOGGER.debug("adapter: "+adapter);\r
107         LOGGER.debug("Hello message received");\r
108         HelloInputBuilder hib = new HelloInputBuilder();\r
109         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();\r
110         hib.setVersion((short) 4);\r
111         hib.setXid(2L);\r
112         featuresBuilder.setVersion((short) 4);\r
113         featuresBuilder.setXid(3L);\r
114         HelloInput hi = hib.build();\r
115         adapter.hello(hi);\r
116         LOGGER.debug("hello msg sent");\r
117         GetFeaturesInput featuresInput = featuresBuilder.build();\r
118         try {\r
119             LOGGER.debug("Going to send featuresRequest");\r
120             RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(\r
121                     featuresInput).get(2500, TimeUnit.MILLISECONDS);\r
122             if (rpcResult.isSuccessful()) {\r
123                 byte[] byteArray = rpcResult.getResult().getDatapathId()\r
124                         .toByteArray();\r
125                 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));\r
126             } else {\r
127                 RpcError rpcError = rpcResult.getErrors().iterator().next();\r
128                 LOGGER.warn("rpcResult failed: "\r
129                         + rpcError.getCause().getMessage(), rpcError.getCause());\r
130             }\r
131         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
132             LOGGER.error(e.getMessage(), e);\r
133             // TODO - Collect exceptions and check for existence in tests\r
134         }\r
135         LOGGER.info("After FeaturesReply message");\r
136         LOGGER.debug("adapter: "+adapter);\r
137     }\r
138 \r
139     protected void shutdown() {\r
140         LOGGER.debug("adapter: "+adapter);\r
141         try {\r
142             LOGGER.info("mockPlugin: "+System.identityHashCode(this));\r
143             Thread.sleep(500);\r
144             if (adapter != null) {\r
145                 Future<Boolean> disconnect = adapter.disconnect();\r
146                 disconnect.get();\r
147                 LOGGER.info("Disconnected");\r
148             }\r
149         } catch (Exception e) {\r
150             LOGGER.error(e.getMessage(), e);\r
151         }\r
152         finishedFuture.set(null);\r
153     }\r
154 \r
155     @Override\r
156     public void onMultipartReplyMessage(MultipartReplyMessage notification) {\r
157         LOGGER.debug("MultipartReply message received");\r
158         \r
159     }\r
160 \r
161     @Override\r
162     public void onMultipartRequestMessage(MultipartRequestMessage notification) {\r
163         LOGGER.debug("MultipartRequest message received");\r
164         \r
165     }\r
166 \r
167     @Override\r
168     public void onPacketInMessage(PacketInMessage notification) {\r
169         LOGGER.debug("PacketIn message received");\r
170         \r
171     }\r
172 \r
173     @Override\r
174     public void onPortStatusMessage(PortStatusMessage notification) {\r
175         LOGGER.debug("PortStatus message received");\r
176         \r
177     }\r
178 \r
179     @Override\r
180     public void onDisconnectEvent(DisconnectEvent notification) {\r
181         LOGGER.debug("disconnection ocured: "+notification.getInfo());\r
182         LOGGER.debug("adapter: "+adapter);\r
183     }\r
184 \r
185     public SettableFuture<Void> getFinishedFuture() {\r
186         return finishedFuture;\r
187     }\r
188 \r
189     @Override\r
190     public void onSwitchIdleEvent(SwitchIdleEvent notification) {\r
191         LOGGER.debug("switch status: "+notification.getInfo());\r
192         idleCounter ++;\r
193     }\r
194     \r
195     public int getIdleCounter() {\r
196         return idleCounter;\r
197     }\r
198 \r
199 \r
200 }\r