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.impl.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 ConnectionAdapter adapter;
56 private SettableFuture<Void> finishedFuture;
57 private int idleCounter = 0;
59 /** Creates MockPlugin */
61 LOGGER.info("Creating MockPlugin");
62 finishedFuture = SettableFuture.create();
63 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
67 public void onSwitchConnected(ConnectionAdapter connection) {
68 LOGGER.info("onSwitchConnected: " + connection);
69 this.adapter = connection;
70 connection.setMessageListener(this);
71 connection.setSystemListener(this);
72 connection.setConnectionReadyListener(this);
76 public boolean accept(InetAddress switchAddress) {
81 public void onEchoRequestMessage(final EchoRequestMessage notification) {
82 new Thread(new Runnable() {
85 LOGGER.debug("EchoRequest message received");
86 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
87 replyBuilder.setVersion((short) 4);
88 replyBuilder.setXid(notification.getXid());
89 EchoReplyInput echoReplyInput = replyBuilder.build();
90 adapter.echoReply(echoReplyInput);
91 LOGGER.debug("EchoReplyInput sent");
92 LOGGER.debug("adapter: "+adapter);
98 public void onErrorMessage(ErrorMessage notification) {
99 LOGGER.debug("Error message received");
104 public void onExperimenterMessage(ExperimenterMessage notification) {
105 LOGGER.debug("Experimenter message received");
110 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
111 LOGGER.debug("FlowRemoved message received");
116 public void onHelloMessage(HelloMessage notification) {
117 new Thread(new Runnable() {
120 LOGGER.debug("Hello message received");
121 HelloInputBuilder hib = new HelloInputBuilder();
122 hib.setVersion((short) 4);
124 HelloInput hi = hib.build();
126 LOGGER.debug("hello msg sent");
127 new Thread(new Runnable() {
133 LOGGER.debug("adapter: "+adapter);
138 protected void sendFeaturesReply() {
139 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
140 featuresBuilder.setVersion((short) 4);
141 featuresBuilder.setXid(3L);
142 GetFeaturesInput featuresInput = featuresBuilder.build();
144 LOGGER.debug("Going to send featuresRequest");
145 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
146 featuresInput).get(2500, TimeUnit.MILLISECONDS);
147 if (rpcResult.isSuccessful()) {
148 byte[] byteArray = rpcResult.getResult().getDatapathId()
150 LOGGER.info("DatapathId: " + Arrays.toString(byteArray));
152 RpcError rpcError = rpcResult.getErrors().iterator().next();
153 LOGGER.warn("rpcResult failed: "
154 + rpcError.getCause().getMessage(), rpcError.getCause());
156 } catch (InterruptedException | ExecutionException | TimeoutException e) {
157 LOGGER.error(e.getMessage(), e);
159 LOGGER.info("After FeaturesReply message");
162 protected void shutdown() {
163 LOGGER.debug("adapter: "+adapter);
165 LOGGER.info("mockPlugin: "+System.identityHashCode(this));
167 if (adapter != null) {
168 Future<Boolean> disconnect = adapter.disconnect();
170 LOGGER.info("Disconnected");
172 } catch (Exception e) {
173 LOGGER.error(e.getMessage(), e);
175 finishedFuture.set(null);
179 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
180 LOGGER.debug("MultipartReply message received");
185 public void onPacketInMessage(PacketInMessage notification) {
186 LOGGER.debug("PacketIn message received");
187 LOGGER.debug("BufferId: " + notification.getBufferId());
188 LOGGER.debug("TotalLength: " + notification.getTotalLen());
189 LOGGER.debug("Reason: " + notification.getReason());
190 LOGGER.debug("TableId: " + notification.getTableId());
191 LOGGER.debug("Cookie: " + notification.getCookie());
192 LOGGER.debug("Class: " + notification.getMatch().getMatchEntries().get(0).getOxmClass());
193 LOGGER.debug("Field: " + notification.getMatch().getMatchEntries().get(0).getOxmMatchField());
194 LOGGER.debug("Datasize: " + notification.getData().length);
198 public void onPortStatusMessage(PortStatusMessage notification) {
199 LOGGER.debug("PortStatus message received");
204 public void onDisconnectEvent(DisconnectEvent notification) {
205 LOGGER.debug("disconnection ocured: "+notification.getInfo());
206 LOGGER.debug("adapter: "+adapter);
210 * @return finishedFuture object
212 public SettableFuture<Void> getFinishedFuture() {
213 return finishedFuture;
217 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
218 LOGGER.debug("switch status: "+notification.getInfo());
223 * @return number of occured idleEvents
225 public int getIdleCounter() {
230 public void onConnectionReady() {
231 LOGGER.debug("connection ready notification arrived");