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
71 // TODO Auto-generated method stub
\r
76 public void onEchoRequestMessage(final EchoRequestMessage notification) {
\r
77 new Thread(new Runnable() {
\r
80 LOGGER.debug("EchoRequest message received");
\r
81 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
\r
82 replyBuilder.setVersion((short) 4);
\r
83 replyBuilder.setXid(notification.getXid());
\r
84 EchoReplyInput echoReplyInput = replyBuilder.build();
\r
85 adapter.echoReply(echoReplyInput);
\r
86 LOGGER.debug("EchoReplyInput sent");
\r
87 LOGGER.debug("adapter: "+adapter);
\r
93 public void onErrorMessage(ErrorMessage notification) {
\r
94 LOGGER.debug("Error message received");
\r
99 public void onExperimenterMessage(ExperimenterMessage notification) {
\r
100 LOGGER.debug("Experimenter message received");
\r
105 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
\r
106 LOGGER.debug("FlowRemoved message received");
\r
111 public void onHelloMessage(HelloMessage notification) {
\r
112 new Thread(new Runnable() {
\r
114 public void run() {
\r
115 LOGGER.debug("Hello message received");
\r
116 HelloInputBuilder hib = new HelloInputBuilder();
\r
117 hib.setVersion((short) 4);
\r
119 HelloInput hi = hib.build();
\r
121 LOGGER.debug("hello msg sent");
\r
122 new Thread(new Runnable() {
\r
124 public void run() {
\r
125 sendFeaturesReply();
\r
128 LOGGER.debug("adapter: "+adapter);
\r
133 protected void sendFeaturesReply() {
\r
134 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
\r
135 featuresBuilder.setVersion((short) 4);
\r
136 featuresBuilder.setXid(3L);
\r
137 GetFeaturesInput featuresInput = featuresBuilder.build();
\r
139 LOGGER.debug("Going to send featuresRequest");
\r
140 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
\r
141 featuresInput).get(2500, TimeUnit.MILLISECONDS);
\r
142 if (rpcResult.isSuccessful()) {
\r
143 byte[] byteArray = rpcResult.getResult().getDatapathId()
\r
145 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));
\r
147 RpcError rpcError = rpcResult.getErrors().iterator().next();
\r
148 LOGGER.warn("rpcResult failed: "
\r
149 + rpcError.getCause().getMessage(), rpcError.getCause());
\r
151 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
152 LOGGER.error(e.getMessage(), e);
\r
154 LOGGER.info("After FeaturesReply message");
\r
157 protected void shutdown() {
\r
158 LOGGER.debug("adapter: "+adapter);
\r
160 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
\r
162 if (adapter != null) {
\r
163 Future<Boolean> disconnect = adapter.disconnect();
\r
165 LOGGER.info("Disconnected");
\r
167 } catch (Exception e) {
\r
168 LOGGER.error(e.getMessage(), e);
\r
170 finishedFuture.set(null);
\r
174 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
\r
175 LOGGER.debug("MultipartReply message received");
\r
180 public void onMultipartRequestMessage(MultipartRequestMessage notification) {
\r
181 LOGGER.debug("MultipartRequest message received");
\r
186 public void onPacketInMessage(PacketInMessage notification) {
\r
187 LOGGER.debug("PacketIn message received");
\r
192 public void onPortStatusMessage(PortStatusMessage notification) {
\r
193 LOGGER.debug("PortStatus message received");
\r
198 public void onDisconnectEvent(DisconnectEvent notification) {
\r
199 LOGGER.debug("disconnection ocured: "+notification.getInfo());
\r
200 LOGGER.debug("adapter: "+adapter);
\r
204 * @return finishedFuture object
\r
206 public SettableFuture<Void> getFinishedFuture() {
\r
207 return finishedFuture;
\r
211 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
\r
212 LOGGER.debug("switch status: "+notification.getInfo());
\r
217 * @return number of occured idleEvents
\r
219 public int getIdleCounter() {
\r
220 return idleCounter;
\r
224 public void onConnectionReady() {
\r
225 LOGGER.debug("connection ready notification arrived");
\r