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.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
50 public MockPlugin() {
\r
51 finishedFuture = SettableFuture.create();
\r
55 public void onSwitchConnected(ConnectionAdapter connection) {
\r
56 LOGGER.debug("onSwitchConnected");
\r
57 this.adapter = connection;
\r
58 connection.setMessageListener(this);
\r
59 connection.setSystemListener(this);
\r
63 public boolean accept(InetAddress switchAddress) {
\r
64 // TODO Auto-generated method stub
\r
69 public void onEchoRequestMessage(EchoRequestMessage notification) {
\r
70 LOGGER.debug("EchoRequest message received");
\r
71 LOGGER.debug("Building EchoReplyInput");
\r
72 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
\r
74 BufferHelper.setupHeader(replyBuilder);
\r
75 } catch (NoSuchMethodException | SecurityException
\r
76 | IllegalAccessException | IllegalArgumentException
\r
77 | InvocationTargetException e) {
\r
78 LOGGER.error(e.getMessage(), e);
\r
80 replyBuilder.setXid(notification.getXid());
\r
81 EchoReplyInput echoReplyInput = replyBuilder.build();
\r
82 LOGGER.debug("EchoReplyInput built");
\r
83 LOGGER.debug("Going to send EchoReplyInput");
\r
84 adapter.echoReply(echoReplyInput);
\r
85 LOGGER.debug("EchoReplyInput sent");
\r
89 public void onErrorMessage(ErrorMessage notification) {
\r
90 LOGGER.debug("Error message received");
\r
95 public void onExperimenterMessage(ExperimenterMessage notification) {
\r
96 LOGGER.debug("Experimenter message received");
\r
101 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
\r
102 LOGGER.debug("FlowRemoved message received");
\r
107 public void onHelloMessage(HelloMessage notification) {
\r
108 LOGGER.debug("Hello message received");
\r
109 HelloInputBuilder hib = new HelloInputBuilder();
\r
110 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
\r
112 BufferHelper.setupHeader(hib);
\r
113 BufferHelper.setupHeader(featuresBuilder);
\r
114 } catch (Exception e) {
\r
115 LOGGER.error(e.getMessage(), e);
\r
117 HelloInput hi = hib.build();
\r
119 LOGGER.debug("hello msg sent");
\r
120 GetFeaturesInput featuresInput = featuresBuilder.build();
\r
122 LOGGER.debug("Going to send featuresRequest");
\r
123 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
\r
124 featuresInput).get(2500, TimeUnit.MILLISECONDS);
\r
125 if (rpcResult.isSuccessful()) {
\r
126 byte[] byteArray = rpcResult.getResult().getDatapathId()
\r
128 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));
\r
130 RpcError rpcError = rpcResult.getErrors().iterator().next();
\r
131 LOGGER.warn("rpcResult failed: "
\r
132 + rpcError.getCause().getMessage(), rpcError.getCause());
\r
134 } catch (InterruptedException | ExecutionException | TimeoutException e) {
\r
135 LOGGER.error(e.getMessage(), e);
\r
137 LOGGER.info("After FeaturesReply message - disconnecting");
\r
138 adapter.disconnect();
\r
139 finishedFuture.set(null);
\r
143 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
\r
144 LOGGER.debug("MultipartReply message received");
\r
149 public void onMultipartRequestMessage(MultipartRequestMessage notification) {
\r
150 LOGGER.debug("MultipartRequest message received");
\r
155 public void onPacketInMessage(PacketInMessage notification) {
\r
156 LOGGER.debug("PacketIn message received");
\r
161 public void onPortStatusMessage(PortStatusMessage notification) {
\r
162 LOGGER.debug("PortStatus message received");
\r
167 public void onDisconnectEvent(DisconnectEvent notification) {
\r
168 LOGGER.debug("disconnection ocured: "+notification.getInfo());
\r
171 public SettableFuture<Void> getFinishedFuture() {
\r
172 return finishedFuture;
\r