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.SwitchConnectionHandler;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
\r
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
\r
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
\r
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
\r
33 import org.opendaylight.yangtools.yang.common.RpcError;
\r
34 import org.opendaylight.yangtools.yang.common.RpcResult;
\r
35 import org.slf4j.Logger;
\r
36 import org.slf4j.LoggerFactory;
\r
38 import com.google.common.util.concurrent.SettableFuture;
\r
41 * @author michal.polkorab
\r
44 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler, SystemNotificationsListener {
\r
46 private static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
\r
47 private ConnectionAdapter adapter;
\r
48 private SettableFuture<Void> finishedFuture;
\r
49 private int idleCounter = 0;
\r
51 /** Creates MockPlugin */
\r
52 public MockPlugin() {
\r
53 LOGGER.info("Creating MockPlugin");
\r
54 finishedFuture = SettableFuture.create();
\r
55 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
\r
59 public void onSwitchConnected(ConnectionAdapter connection) {
\r
60 LOGGER.info("onSwitchConnected: " + connection);
\r
61 this.adapter = connection;
\r
62 connection.setMessageListener(this);
\r
63 connection.setSystemListener(this);
\r
67 public boolean accept(InetAddress switchAddress) {
\r
68 // TODO Auto-generated method stub
\r
73 public void onEchoRequestMessage(EchoRequestMessage notification) {
\r
74 LOGGER.debug("EchoRequest message received");
\r
75 LOGGER.debug("Building EchoReplyInput");
\r
76 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
\r
77 replyBuilder.setVersion((short) 4);
\r
78 replyBuilder.setXid(notification.getXid());
\r
79 EchoReplyInput echoReplyInput = replyBuilder.build();
\r
80 LOGGER.debug("EchoReplyInput built");
\r
81 LOGGER.debug("Going to send EchoReplyInput");
\r
82 adapter.echoReply(echoReplyInput);
\r
83 LOGGER.debug("EchoReplyInput sent");
\r
84 LOGGER.debug("adapter: "+adapter);
\r
88 public void onErrorMessage(ErrorMessage notification) {
\r
89 LOGGER.debug("Error message received");
\r
94 public void onExperimenterMessage(ExperimenterMessage notification) {
\r
95 LOGGER.debug("Experimenter message received");
\r
100 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
\r
101 LOGGER.debug("FlowRemoved message received");
\r
106 public void onHelloMessage(HelloMessage notification) {
\r
107 LOGGER.debug("adapter: "+adapter);
\r
108 LOGGER.debug("Hello message received");
\r
109 HelloInputBuilder hib = new HelloInputBuilder();
\r
110 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
\r
111 hib.setVersion((short) 4);
\r
113 featuresBuilder.setVersion((short) 4);
\r
114 featuresBuilder.setXid(3L);
\r
115 HelloInput hi = hib.build();
\r
117 LOGGER.debug("hello msg sent");
\r
118 GetFeaturesInput featuresInput = featuresBuilder.build();
\r
120 LOGGER.debug("Going to send featuresRequest");
\r
121 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
\r
122 featuresInput).get(2500, TimeUnit.MILLISECONDS);
\r
123 if (rpcResult.isSuccessful()) {
\r
124 byte[] byteArray = rpcResult.getResult().getDatapathId()
\r
126 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));
\r
128 RpcError rpcError = rpcResult.getErrors().iterator().next();
\r
129 LOGGER.warn("rpcResult failed: "
\r
130 + rpcError.getCause().getMessage(), rpcError.getCause());
\r
132 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
133 LOGGER.error(e.getMessage(), e);
\r
134 // TODO - Collect exceptions and check for existence in tests
\r
136 LOGGER.info("After FeaturesReply message");
\r
137 LOGGER.debug("adapter: "+adapter);
\r
140 protected void shutdown() {
\r
141 LOGGER.debug("adapter: "+adapter);
\r
143 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
\r
145 if (adapter != null) {
\r
146 Future<Boolean> disconnect = adapter.disconnect();
\r
148 LOGGER.info("Disconnected");
\r
150 } catch (Exception e) {
\r
151 LOGGER.error(e.getMessage(), e);
\r
153 finishedFuture.set(null);
\r
157 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
\r
158 LOGGER.debug("MultipartReply message received");
\r
163 public void onMultipartRequestMessage(MultipartRequestMessage notification) {
\r
164 LOGGER.debug("MultipartRequest message received");
\r
169 public void onPacketInMessage(PacketInMessage notification) {
\r
170 LOGGER.debug("PacketIn message received");
\r
175 public void onPortStatusMessage(PortStatusMessage notification) {
\r
176 LOGGER.debug("PortStatus message received");
\r
181 public void onDisconnectEvent(DisconnectEvent notification) {
\r
182 LOGGER.debug("disconnection ocured: "+notification.getInfo());
\r
183 LOGGER.debug("adapter: "+adapter);
\r
187 * @return finishedFuture object
\r
189 public SettableFuture<Void> getFinishedFuture() {
\r
190 return finishedFuture;
\r
194 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
\r
195 LOGGER.debug("switch status: "+notification.getInfo());
\r
200 * @return number of occured idleEvents
\r
202 public int getIdleCounter() {
\r
203 return idleCounter;
\r