2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.it.integration;
11 import java.net.InetAddress;
12 import java.util.Arrays;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
19 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
20 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
40 import org.opendaylight.yangtools.yang.common.RpcError;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 import com.google.common.util.concurrent.SettableFuture;
48 * @author michal.polkorab
51 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
52 SystemNotificationsListener, ConnectionReadyListener {
54 protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
55 protected volatile ConnectionAdapter adapter;
56 private SettableFuture<Void> finishedFuture;
57 private int idleCounter = 0;
59 /** Creates MockPlugin */
61 LOGGER.trace("Creating MockPlugin");
62 finishedFuture = SettableFuture.create();
63 LOGGER.debug("mockPlugin: "+System.identityHashCode(this));
67 public void onSwitchConnected(ConnectionAdapter connection) {
68 LOGGER.debug("onSwitchConnected: " + connection);
69 this.adapter = connection;
70 connection.setMessageListener(this);
71 connection.setSystemListener(this);
72 connection.setConnectionReadyListener(this);
76 public boolean accept(InetAddress switchAddress) {
77 LOGGER.debug("MockPlugin.accept(): " + switchAddress.toString());
83 public void onEchoRequestMessage(final EchoRequestMessage notification) {
84 LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: "+adapter);
85 new Thread(new Runnable() {
88 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() started adapter: "+adapter);
89 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
90 replyBuilder.setVersion((short) 4);
91 replyBuilder.setXid(notification.getXid());
92 EchoReplyInput echoReplyInput = replyBuilder.build();
93 adapter.echoReply(echoReplyInput);
94 LOGGER.debug("adapter.EchoReply(Input) sent : ", echoReplyInput.toString());
95 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() finished adapter: "+adapter);
101 public void onErrorMessage(ErrorMessage notification) {
102 LOGGER.debug("Error message received");
107 public void onExperimenterMessage(ExperimenterMessage notification) {
108 LOGGER.debug("Experimenter message received");
113 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
114 LOGGER.debug("FlowRemoved message received");
119 public void onHelloMessage(HelloMessage notification) {
120 new Thread(new Runnable() {
123 LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
124 HelloInputBuilder hib = new HelloInputBuilder();
125 hib.setVersion((short) 4);
127 HelloInput hi = hib.build();
129 LOGGER.debug("hello msg sent");
130 new Thread(new Runnable() {
141 protected void getSwitchFeatures() {
142 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
143 featuresBuilder.setVersion((short) 4);
144 featuresBuilder.setXid(3L);
145 GetFeaturesInput featuresInput = featuresBuilder.build();
147 LOGGER.debug("Requesting features ");
148 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
149 featuresInput).get(2500, TimeUnit.MILLISECONDS);
150 if (rpcResult.isSuccessful()) {
151 byte[] byteArray = rpcResult.getResult().getDatapathId()
153 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));
155 RpcError rpcError = rpcResult.getErrors().iterator().next();
156 LOGGER.warn("rpcResult failed: "
157 + rpcError.getCause().getMessage(), rpcError.getCause());
159 } catch (InterruptedException | ExecutionException | TimeoutException e) {
160 LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
164 protected void shutdown() {
166 LOGGER.debug("MockPlugin.shutdown() sleeping 5... : "+System.identityHashCode(this));
168 if (adapter != null) {
169 Future<Boolean> disconnect = adapter.disconnect();
171 LOGGER.debug("MockPlugin.shutdown() Disconnected");
173 } catch (Exception e) {
174 LOGGER.error("MockPlugin.shutdown() exception caught: ", e.getMessage(), e);
176 finishedFuture.set(null);
180 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
181 LOGGER.debug("MultipartReply message received");
186 public void onPacketInMessage(PacketInMessage notification) {
187 LOGGER.debug("PacketIn message received");
188 LOGGER.debug("BufferId: " + notification.getBufferId());
189 LOGGER.debug("TotalLength: " + notification.getTotalLen());
190 LOGGER.debug("Reason: " + notification.getReason());
191 LOGGER.debug("TableId: " + notification.getTableId());
192 LOGGER.debug("Cookie: " + notification.getCookie());
193 LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass());
194 LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField());
195 LOGGER.debug("Datasize: " + notification.getData().length);
199 public void onPortStatusMessage(PortStatusMessage notification) {
200 LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
205 public void onDisconnectEvent(DisconnectEvent notification) {
206 LOGGER.debug("disconnection occured: "+notification.getInfo());
210 * @return finishedFuture object
212 public SettableFuture<Void> getFinishedFuture() {
213 return finishedFuture;
217 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
218 LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: "+notification.getInfo());
223 * @return number of occured idleEvents
225 public int getIdleCounter() {
230 public void onConnectionReady() {
231 LOGGER.trace("MockPlugin().onConnectionReady()");