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