1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.integration;
\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
11 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
\r
12 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
\r
13 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
\r
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
\r
34 import org.opendaylight.yangtools.yang.common.RpcError;
\r
35 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
36 import org.slf4j.Logger;
\r
37 import org.slf4j.LoggerFactory;
\r
39 import com.google.common.util.concurrent.SettableFuture;
\r
42 * @author michal.polkorab
\r
45 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
\r
46 SystemNotificationsListener, ConnectionReadyListener {
\r
48 protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
\r
49 protected ConnectionAdapter adapter;
\r
50 private SettableFuture<Void> finishedFuture;
\r
51 private int idleCounter = 0;
\r
53 /** Creates MockPlugin */
\r
54 public MockPlugin() {
\r
55 LOGGER.info("Creating MockPlugin");
\r
56 finishedFuture = SettableFuture.create();
\r
57 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
\r
61 public void onSwitchConnected(ConnectionAdapter connection) {
\r
62 LOGGER.info("onSwitchConnected: " + connection);
\r
63 this.adapter = connection;
\r
64 connection.setMessageListener(this);
\r
65 connection.setSystemListener(this);
\r
66 connection.setConnectionReadyListener(this);
\r
70 public boolean accept(InetAddress switchAddress) {
\r
75 public void onEchoRequestMessage(final EchoRequestMessage notification) {
\r
76 new Thread(new Runnable() {
\r
79 LOGGER.debug("EchoRequest message received");
\r
80 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
\r
81 replyBuilder.setVersion((short) 4);
\r
82 replyBuilder.setXid(notification.getXid());
\r
83 EchoReplyInput echoReplyInput = replyBuilder.build();
\r
84 adapter.echoReply(echoReplyInput);
\r
85 LOGGER.debug("EchoReplyInput sent");
\r
86 LOGGER.debug("adapter: "+adapter);
\r
92 public void onErrorMessage(ErrorMessage notification) {
\r
93 LOGGER.debug("Error message received");
\r
98 public void onExperimenterMessage(ExperimenterMessage notification) {
\r
99 LOGGER.debug("Experimenter message received");
\r
104 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
\r
105 LOGGER.debug("FlowRemoved message received");
\r
110 public void onHelloMessage(HelloMessage notification) {
\r
111 new Thread(new Runnable() {
\r
113 public void run() {
\r
114 LOGGER.debug("Hello message received");
\r
115 HelloInputBuilder hib = new HelloInputBuilder();
\r
116 hib.setVersion((short) 4);
\r
118 HelloInput hi = hib.build();
\r
120 LOGGER.debug("hello msg sent");
\r
121 new Thread(new Runnable() {
\r
123 public void run() {
\r
124 sendFeaturesReply();
\r
127 LOGGER.debug("adapter: "+adapter);
\r
132 protected void sendFeaturesReply() {
\r
133 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
\r
134 featuresBuilder.setVersion((short) 4);
\r
135 featuresBuilder.setXid(3L);
\r
136 GetFeaturesInput featuresInput = featuresBuilder.build();
\r
138 LOGGER.debug("Going to send featuresRequest");
\r
139 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
\r
140 featuresInput).get(2500, TimeUnit.MILLISECONDS);
\r
141 if (rpcResult.isSuccessful()) {
\r
142 byte[] byteArray = rpcResult.getResult().getDatapathId()
\r
144 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));
\r
146 RpcError rpcError = rpcResult.getErrors().iterator().next();
\r
147 LOGGER.warn("rpcResult failed: "
\r
148 + rpcError.getCause().getMessage(), rpcError.getCause());
\r
150 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
151 LOGGER.error(e.getMessage(), e);
\r
153 LOGGER.info("After FeaturesReply message");
\r
156 protected void shutdown() {
\r
157 LOGGER.debug("adapter: "+adapter);
\r
159 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
\r
161 if (adapter != null) {
\r
162 Future<Boolean> disconnect = adapter.disconnect();
\r
164 LOGGER.info("Disconnected");
\r
166 } catch (Exception e) {
\r
167 LOGGER.error(e.getMessage(), e);
\r
169 finishedFuture.set(null);
\r
173 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
\r
174 LOGGER.debug("MultipartReply message received");
\r
179 public void onMultipartRequestMessage(MultipartRequestMessage notification) {
\r
180 LOGGER.debug("MultipartRequest message received");
\r
185 public void onPacketInMessage(PacketInMessage notification) {
\r
186 LOGGER.debug("PacketIn message received");
\r
187 LOGGER.debug("BufferId: " + notification.getBufferId());
\r
188 LOGGER.debug("TotalLength: " + notification.getTotalLen());
\r
189 LOGGER.debug("Reason: " + notification.getReason());
\r
190 LOGGER.debug("TableId: " + notification.getTableId());
\r
191 LOGGER.debug("Cookie: " + notification.getCookie());
\r
192 LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass());
\r
193 LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField());
\r
194 LOGGER.debug("Datasize: " + notification.getData().length);
\r
198 public void onPortStatusMessage(PortStatusMessage notification) {
\r
199 LOGGER.debug("PortStatus message received");
\r
204 public void onDisconnectEvent(DisconnectEvent notification) {
\r
205 LOGGER.debug("disconnection ocured: "+notification.getInfo());
\r
206 LOGGER.debug("adapter: "+adapter);
\r
210 * @return finishedFuture object
\r
212 public SettableFuture<Void> getFinishedFuture() {
\r
213 return finishedFuture;
\r
217 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
\r
218 LOGGER.debug("switch status: "+notification.getInfo());
\r
223 * @return number of occured idleEvents
\r
225 public int getIdleCounter() {
\r
226 return idleCounter;
\r
230 public void onConnectionReady() {
\r
231 LOGGER.debug("connection ready notification arrived");
\r