Remove trailing whitespace
[openflowjava.git] / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / it / integration / MockPlugin.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowjava.protocol.it.integration;
10
11 import java.net.InetAddress;
12 import java.util.Arrays;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
20 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 import com.google.common.util.concurrent.SettableFuture;
46
47 /**
48  * @author michal.polkorab
49  *
50  */
51 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
52         SystemNotificationsListener, ConnectionReadyListener {
53
54     protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
55     protected volatile ConnectionAdapter adapter;
56     private SettableFuture<Void> finishedFuture;
57     private int idleCounter = 0;
58
59     /** Creates MockPlugin */
60     public MockPlugin() {
61         LOGGER.trace("Creating MockPlugin");
62         finishedFuture = SettableFuture.create();
63         LOGGER.debug("mockPlugin: "+System.identityHashCode(this));
64     }
65
66     @Override
67     public void onSwitchConnected(ConnectionAdapter connection) {
68         LOGGER.debug("onSwitchConnected: " + connection);
69         this.adapter = connection;
70         connection.setMessageListener(this);
71         connection.setSystemListener(this);
72         connection.setConnectionReadyListener(this);
73     }
74
75     @Override
76     public boolean accept(InetAddress switchAddress) {
77         LOGGER.debug("MockPlugin.accept(): " + switchAddress.toString());
78
79         return true;
80     }
81
82     @Override
83     public void onEchoRequestMessage(final EchoRequestMessage notification) {
84         LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: "+adapter);
85         new Thread(new Runnable() {
86             @Override
87             public void run() {
88                 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() started adapter: "+adapter);
89                 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
90                 replyBuilder.setVersion((short) 4);
91                 replyBuilder.setXid(notification.getXid());
92                 EchoReplyInput echoReplyInput = replyBuilder.build();
93                 adapter.echoReply(echoReplyInput);
94                 LOGGER.debug("adapter.EchoReply(Input) sent : ", echoReplyInput.toString());
95                 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() finished adapter: "+adapter);
96             }
97         }).start();
98     }
99
100     @Override
101     public void onErrorMessage(ErrorMessage notification) {
102         LOGGER.debug("Error message received");
103
104     }
105
106     @Override
107     public void onExperimenterMessage(ExperimenterMessage notification) {
108         LOGGER.debug("Experimenter message received");
109
110     }
111
112     @Override
113     public void onFlowRemovedMessage(FlowRemovedMessage notification) {
114         LOGGER.debug("FlowRemoved message received");
115
116     }
117
118     @Override
119     public void onHelloMessage(HelloMessage notification) {
120         new Thread(new Runnable() {
121             @Override
122             public void run() {
123                 LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
124                 HelloInputBuilder hib = new HelloInputBuilder();
125                 hib.setVersion((short) 4);
126                 hib.setXid(2L);
127                 HelloInput hi = hib.build();
128                 adapter.hello(hi);
129                 LOGGER.debug("hello msg sent");
130                 new Thread(new Runnable() {
131                     @Override
132                     public void run() {
133                         getSwitchFeatures();
134                     }
135                 }).start();
136             }
137         }).start();
138
139     }
140
141     protected void getSwitchFeatures() {
142         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
143         featuresBuilder.setVersion((short) 4);
144         featuresBuilder.setXid(3L);
145         GetFeaturesInput featuresInput = featuresBuilder.build();
146         try {
147             LOGGER.debug("Requesting features ");
148             RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
149                     featuresInput).get(2500, TimeUnit.MILLISECONDS);
150             if (rpcResult.isSuccessful()) {
151                 byte[] byteArray = rpcResult.getResult().getDatapathId()
152                         .toByteArray();
153                 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));
154             } else {
155                 RpcError rpcError = rpcResult.getErrors().iterator().next();
156                 LOGGER.warn("rpcResult failed: "
157                         + rpcError.getCause().getMessage(), rpcError.getCause());
158             }
159         } catch (InterruptedException | ExecutionException | TimeoutException e) {
160             LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
161         }
162     }
163
164     protected void shutdown() {
165         try {
166             LOGGER.debug("MockPlugin.shutdown() sleeping 5... : "+System.identityHashCode(this));
167             Thread.sleep(500);
168             if (adapter != null) {
169                 Future<Boolean> disconnect = adapter.disconnect();
170                 disconnect.get();
171                 LOGGER.debug("MockPlugin.shutdown() Disconnected");
172             }
173         } catch (Exception e) {
174             LOGGER.error("MockPlugin.shutdown() exception caught: ", e.getMessage(), e);
175         }
176         finishedFuture.set(null);
177     }
178
179     @Override
180     public void onMultipartReplyMessage(MultipartReplyMessage notification) {
181         LOGGER.debug("MultipartReply message received");
182
183     }
184
185     @Override
186     public void onPacketInMessage(PacketInMessage notification) {
187         LOGGER.debug("PacketIn message received");
188         LOGGER.debug("BufferId: " + notification.getBufferId());
189         LOGGER.debug("TotalLength: " + notification.getTotalLen());
190         LOGGER.debug("Reason: " + notification.getReason());
191         LOGGER.debug("TableId: " + notification.getTableId());
192         LOGGER.debug("Cookie: " + notification.getCookie());
193         LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass());
194         LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField());
195         LOGGER.debug("Datasize: " + notification.getData().length);
196     }
197
198     @Override
199     public void onPortStatusMessage(PortStatusMessage notification) {
200         LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
201
202     }
203
204     @Override
205     public void onDisconnectEvent(DisconnectEvent notification) {
206         LOGGER.debug("disconnection occured: "+notification.getInfo());
207     }
208
209     /**
210      * @return finishedFuture object
211      */
212     public SettableFuture<Void> getFinishedFuture() {
213         return finishedFuture;
214     }
215
216     @Override
217     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
218         LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: "+notification.getInfo());
219         idleCounter ++;
220     }
221
222     /**
223      * @return number of occured idleEvents
224      */
225     public int getIdleCounter() {
226         return idleCounter;
227     }
228
229     @Override
230     public void onConnectionReady() {
231         LOGGER.trace("MockPlugin().onConnectionReady()");
232     }
233
234
235 }