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.lang.reflect.InvocationTargetException;
\r
5 import java.net.InetAddress;
\r
6 import java.util.Arrays;
\r
7 import java.util.concurrent.ExecutionException;
\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.openflowjava.protocol.impl.util.BufferHelper;
\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, SystemNotificationsListener {
\r
47 private static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
\r
48 private ConnectionAdapter adapter;
\r
49 private SettableFuture<Void> finishedFuture;
\r
50 private int idleCounter = 0;
\r
52 public MockPlugin() {
\r
53 finishedFuture = SettableFuture.create();
\r
57 public void onSwitchConnected(ConnectionAdapter connection) {
\r
58 LOGGER.debug("onSwitchConnected");
\r
59 this.adapter = connection;
\r
60 connection.setMessageListener(this);
\r
61 connection.setSystemListener(this);
\r
65 public boolean accept(InetAddress switchAddress) {
\r
66 // TODO Auto-generated method stub
\r
71 public void onEchoRequestMessage(EchoRequestMessage notification) {
\r
72 LOGGER.debug("EchoRequest message received");
\r
73 LOGGER.debug("Building EchoReplyInput");
\r
74 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
\r
76 BufferHelper.setupHeader(replyBuilder);
\r
77 } catch (NoSuchMethodException | SecurityException
\r
78 | IllegalAccessException | IllegalArgumentException
\r
79 | InvocationTargetException e) {
\r
80 LOGGER.error(e.getMessage(), e);
\r
82 replyBuilder.setXid(notification.getXid());
\r
83 EchoReplyInput echoReplyInput = replyBuilder.build();
\r
84 LOGGER.debug("EchoReplyInput built");
\r
85 LOGGER.debug("Going to send EchoReplyInput");
\r
86 adapter.echoReply(echoReplyInput);
\r
87 LOGGER.debug("EchoReplyInput sent");
\r
91 public void onErrorMessage(ErrorMessage notification) {
\r
92 LOGGER.debug("Error message received");
\r
97 public void onExperimenterMessage(ExperimenterMessage notification) {
\r
98 LOGGER.debug("Experimenter message received");
\r
103 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
\r
104 LOGGER.debug("FlowRemoved message received");
\r
109 public void onHelloMessage(HelloMessage notification) {
\r
110 LOGGER.debug("Hello message received");
\r
111 HelloInputBuilder hib = new HelloInputBuilder();
\r
112 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
\r
114 BufferHelper.setupHeader(hib);
\r
115 BufferHelper.setupHeader(featuresBuilder);
\r
116 } catch (Exception e) {
\r
117 LOGGER.error(e.getMessage(), e);
\r
119 HelloInput hi = hib.build();
\r
121 LOGGER.debug("hello msg sent");
\r
122 GetFeaturesInput featuresInput = featuresBuilder.build();
\r
124 LOGGER.debug("Going to send featuresRequest");
\r
125 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
\r
126 featuresInput).get(2500, TimeUnit.MILLISECONDS);
\r
127 if (rpcResult.isSuccessful()) {
\r
128 byte[] byteArray = rpcResult.getResult().getDatapathId()
\r
130 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));
\r
132 RpcError rpcError = rpcResult.getErrors().iterator().next();
\r
133 LOGGER.warn("rpcResult failed: "
\r
134 + rpcError.getCause().getMessage(), rpcError.getCause());
\r
136 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
137 LOGGER.error(e.getMessage(), e);
\r
139 LOGGER.info("After FeaturesReply message - disconnecting");
\r
140 adapter.disconnect();
\r
141 finishedFuture.set(null);
\r
145 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
\r
146 LOGGER.debug("MultipartReply message received");
\r
151 public void onMultipartRequestMessage(MultipartRequestMessage notification) {
\r
152 LOGGER.debug("MultipartRequest message received");
\r
157 public void onPacketInMessage(PacketInMessage notification) {
\r
158 LOGGER.debug("PacketIn message received");
\r
163 public void onPortStatusMessage(PortStatusMessage notification) {
\r
164 LOGGER.debug("PortStatus message received");
\r
169 public void onDisconnectEvent(DisconnectEvent notification) {
\r
170 LOGGER.debug("disconnection ocured: "+notification.getInfo());
\r
173 public SettableFuture<Void> getFinishedFuture() {
\r
174 return finishedFuture;
\r
178 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
\r
179 LOGGER.debug("switch status: "+notification.getInfo());
\r
183 public int getIdleCounter() {
\r
184 return idleCounter;
\r