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.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import com.google.common.util.concurrent.SettableFuture;
49 * @author michal.polkorab
52 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
53 SystemNotificationsListener, ConnectionReadyListener {
55 protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
56 protected volatile ConnectionAdapter adapter;
57 private SettableFuture<Void> finishedFuture;
58 private int idleCounter = 0;
60 /** Creates MockPlugin */
62 LOGGER.trace("Creating MockPlugin");
63 finishedFuture = SettableFuture.create();
64 LOGGER.debug("mockPlugin: "+System.identityHashCode(this));
68 public void onSwitchConnected(ConnectionAdapter connection) {
69 LOGGER.debug("onSwitchConnected: " + connection);
70 this.adapter = connection;
71 connection.setMessageListener(this);
72 connection.setSystemListener(this);
73 connection.setConnectionReadyListener(this);
77 public boolean accept(InetAddress switchAddress) {
78 LOGGER.debug("MockPlugin.accept(): " + switchAddress.toString());
84 public void onEchoRequestMessage(final EchoRequestMessage notification) {
85 LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: "+adapter);
86 new Thread(new Runnable() {
89 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() started adapter: "+adapter);
90 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
91 replyBuilder.setVersion((short) 4);
92 replyBuilder.setXid(notification.getXid());
93 EchoReplyInput echoReplyInput = replyBuilder.build();
94 adapter.echoReply(echoReplyInput);
95 LOGGER.debug("adapter.EchoReply(Input) sent : ", echoReplyInput.toString());
96 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() finished adapter: "+adapter);
102 public void onErrorMessage(ErrorMessage notification) {
103 LOGGER.debug("Error message received");
108 public void onExperimenterMessage(ExperimenterMessage notification) {
109 LOGGER.debug("Experimenter message received");
114 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
115 LOGGER.debug("FlowRemoved message received");
120 public void onHelloMessage(HelloMessage notification) {
121 new Thread(new Runnable() {
124 LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
125 HelloInputBuilder hib = new HelloInputBuilder();
126 hib.setVersion((short) 4);
128 HelloInput hi = hib.build();
130 LOGGER.debug("hello msg sent");
131 new Thread(new Runnable() {
142 protected void getSwitchFeatures() {
143 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
144 featuresBuilder.setVersion((short) 4);
145 featuresBuilder.setXid(3L);
146 GetFeaturesInput featuresInput = featuresBuilder.build();
148 LOGGER.debug("Requesting features ");
149 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
150 featuresInput).get(2500, TimeUnit.MILLISECONDS);
151 if (rpcResult.isSuccessful()) {
152 byte[] byteArray = rpcResult.getResult().getDatapathId()
154 LOGGER.debug("DatapathId: " + Arrays.toString(byteArray));
156 RpcError rpcError = rpcResult.getErrors().iterator().next();
157 LOGGER.warn("rpcResult failed: "
158 + rpcError.getCause().getMessage(), rpcError.getCause());
160 } catch (InterruptedException | ExecutionException | TimeoutException e) {
161 LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
165 protected void shutdown() {
167 LOGGER.debug("MockPlugin.shutdown() sleeping 5... : "+System.identityHashCode(this));
169 if (adapter != null) {
170 Future<Boolean> disconnect = adapter.disconnect();
172 LOGGER.debug("MockPlugin.shutdown() Disconnected");
174 } catch (Exception e) {
175 LOGGER.error("MockPlugin.shutdown() exception caught: ", e.getMessage(), e);
177 finishedFuture.set(null);
181 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
182 LOGGER.debug("MultipartReply message received");
187 public void onPacketInMessage(PacketInMessage notification) {
188 LOGGER.debug("PacketIn message received");
189 LOGGER.debug("BufferId: " + notification.getBufferId());
190 LOGGER.debug("TotalLength: " + notification.getTotalLen());
191 LOGGER.debug("Reason: " + notification.getReason());
192 LOGGER.debug("TableId: " + notification.getTableId());
193 LOGGER.debug("Cookie: " + notification.getCookie());
194 LOGGER.debug("Class: " + notification.getMatch().getMatchEntry().get(0).getOxmClass());
195 LOGGER.debug("Field: " + notification.getMatch().getMatchEntry().get(0).getOxmMatchField());
196 LOGGER.debug("Datasize: " + notification.getData().length);
200 public void onPortStatusMessage(PortStatusMessage notification) {
201 LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
206 public void onDisconnectEvent(DisconnectEvent notification) {
207 LOGGER.debug("disconnection occured: "+notification.getInfo());
211 * @return finishedFuture object
213 public SettableFuture<Void> getFinishedFuture() {
214 return finishedFuture;
218 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
219 LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: "+notification.getInfo());
224 * @return number of occured idleEvents
226 public int getIdleCounter() {
231 public void onConnectionReady() {
232 LOGGER.trace("MockPlugin().onConnectionReady()");
236 * Initiates connection to device
237 * @param switchConnectionProvider
238 * @param host - host IP
239 * @param port - port number
241 public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) {
242 LOGGER.trace("MockPlugin().initiateConnection()");
243 switchConnectionProvider.initiateConnection(host, port);