Copyright update
[openflowjava.git] / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / impl / 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.impl.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 ConnectionAdapter adapter;
56     private SettableFuture<Void> finishedFuture;
57     private int idleCounter = 0;
58
59     /** Creates MockPlugin */
60     public MockPlugin() {
61         LOGGER.info("Creating MockPlugin");
62         finishedFuture = SettableFuture.create();
63         LOGGER.info("mockPlugin: "+System.identityHashCode(this));
64     }
65     
66     @Override
67     public void onSwitchConnected(ConnectionAdapter connection) {
68         LOGGER.info("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         return true;
78     }
79
80     @Override
81     public void onEchoRequestMessage(final EchoRequestMessage notification) {
82         new Thread(new Runnable() {
83             @Override
84             public void run() {
85                 LOGGER.debug("EchoRequest message received");
86                 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
87                 replyBuilder.setVersion((short) 4);
88                 replyBuilder.setXid(notification.getXid());
89                 EchoReplyInput echoReplyInput = replyBuilder.build();
90                 adapter.echoReply(echoReplyInput);
91                 LOGGER.debug("EchoReplyInput sent");
92                 LOGGER.debug("adapter: "+adapter);
93             }
94         }).start();
95     }
96
97     @Override
98     public void onErrorMessage(ErrorMessage notification) {
99         LOGGER.debug("Error message received");
100         
101     }
102
103     @Override
104     public void onExperimenterMessage(ExperimenterMessage notification) {
105         LOGGER.debug("Experimenter message received");
106         
107     }
108
109     @Override
110     public void onFlowRemovedMessage(FlowRemovedMessage notification) {
111         LOGGER.debug("FlowRemoved message received");
112         
113     }
114
115     @Override
116     public void onHelloMessage(HelloMessage notification) {
117         new Thread(new Runnable() {
118             @Override
119             public void run() {
120                 LOGGER.debug("Hello message received");
121                 HelloInputBuilder hib = new HelloInputBuilder();
122                 hib.setVersion((short) 4);
123                 hib.setXid(2L);
124                 HelloInput hi = hib.build();
125                 adapter.hello(hi);
126                 LOGGER.debug("hello msg sent");
127                 new Thread(new Runnable() {
128                     @Override
129                     public void run() {
130                         sendFeaturesReply();
131                     }
132                 }).start();
133                 LOGGER.debug("adapter: "+adapter);
134             }
135         }).start();
136     }
137     
138     protected void sendFeaturesReply() {
139         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
140         featuresBuilder.setVersion((short) 4);
141         featuresBuilder.setXid(3L);
142         GetFeaturesInput featuresInput = featuresBuilder.build();
143         try {
144             LOGGER.debug("Going to send featuresRequest");
145             RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
146                     featuresInput).get(2500, TimeUnit.MILLISECONDS);
147             if (rpcResult.isSuccessful()) {
148                 byte[] byteArray = rpcResult.getResult().getDatapathId()
149                         .toByteArray();
150                 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));
151             } else {
152                 RpcError rpcError = rpcResult.getErrors().iterator().next();
153                 LOGGER.warn("rpcResult failed: "
154                         + rpcError.getCause().getMessage(), rpcError.getCause());
155             }
156         } catch (InterruptedException | ExecutionException | TimeoutException e) {
157             LOGGER.error(e.getMessage(), e);
158         }
159         LOGGER.info("After FeaturesReply message");
160     }
161
162     protected void shutdown() {
163         LOGGER.debug("adapter: "+adapter);
164         try {
165             LOGGER.info("mockPlugin: "+System.identityHashCode(this));
166             Thread.sleep(500);
167             if (adapter != null) {
168                 Future<Boolean> disconnect = adapter.disconnect();
169                 disconnect.get();
170                 LOGGER.info("Disconnected");
171             } 
172         } catch (Exception e) {
173             LOGGER.error(e.getMessage(), e);
174         }
175         finishedFuture.set(null);
176     }
177
178     @Override
179     public void onMultipartReplyMessage(MultipartReplyMessage notification) {
180         LOGGER.debug("MultipartReply message received");
181         
182     }
183
184     @Override
185     public void onPacketInMessage(PacketInMessage notification) {
186         LOGGER.debug("PacketIn message received");
187         LOGGER.debug("BufferId: " + notification.getBufferId());
188         LOGGER.debug("TotalLength: " + notification.getTotalLen());
189         LOGGER.debug("Reason: " + notification.getReason());
190         LOGGER.debug("TableId: " + notification.getTableId());
191         LOGGER.debug("Cookie: " + notification.getCookie());
192         LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass());
193         LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField());
194         LOGGER.debug("Datasize: " + notification.getData().length);
195     }
196
197     @Override
198     public void onPortStatusMessage(PortStatusMessage notification) {
199         LOGGER.debug("PortStatus message received");
200         
201     }
202
203     @Override
204     public void onDisconnectEvent(DisconnectEvent notification) {
205         LOGGER.debug("disconnection ocured: "+notification.getInfo());
206         LOGGER.debug("adapter: "+adapter);
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("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.debug("connection ready notification arrived");
232     }
233
234
235 }