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