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
8 package org.opendaylight.openflowjava.protocol.it.integration;
10 import com.google.common.util.concurrent.SettableFuture;
11 import java.net.InetAddress;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.ExecutorService;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
18 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
19 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
20 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
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.SslConnectionError;
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.opendaylight.yangtools.yang.common.Uint32;
44 import org.opendaylight.yangtools.yang.common.Uint8;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
51 * @author michal.polkorab
53 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
54 SystemNotificationsListener, ConnectionReadyListener {
56 protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
57 protected volatile ConnectionAdapter adapter;
58 private final SettableFuture<Void> finishedFuture;
59 private int idleCounter = 0;
60 private final ExecutorService executorService;
62 public MockPlugin(ExecutorService executorService) {
63 LOGGER.trace("Creating MockPlugin");
64 finishedFuture = SettableFuture.create();
65 this.executorService = executorService;
66 LOGGER.debug("mockPlugin: {}", System.identityHashCode(this));
70 public void onSwitchConnected(ConnectionAdapter connection) {
71 LOGGER.debug("onSwitchConnected: {}", connection);
72 this.adapter = connection;
73 connection.setMessageListener(this);
74 connection.setSystemListener(this);
75 connection.setConnectionReadyListener(this);
76 connection.setExecutorService(executorService);
80 public boolean accept(InetAddress switchAddress) {
81 LOGGER.debug("MockPlugin.accept(): {}", switchAddress.toString());
87 public void onEchoRequestMessage(final EchoRequestMessage notification) {
88 LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: {}", adapter);
90 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() started adapter: {}", adapter);
91 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
92 replyBuilder.setVersion(Uint8.valueOf(4));
93 replyBuilder.setXid(notification.getXid());
94 EchoReplyInput echoReplyInput = replyBuilder.build();
95 adapter.echoReply(echoReplyInput);
96 LOGGER.debug("adapter.EchoReply(Input) sent : ", echoReplyInput.toString());
97 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) {
122 LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
123 HelloInputBuilder hib = new HelloInputBuilder();
124 hib.setVersion(Uint8.valueOf(4));
125 hib.setXid(Uint32.TWO);
126 HelloInput hi = hib.build();
128 LOGGER.debug("hello msg sent");
129 new Thread(this::getSwitchFeatures).start();
134 protected void getSwitchFeatures() {
135 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
136 featuresBuilder.setVersion(Uint8.valueOf(4));
137 featuresBuilder.setXid(Uint32.valueOf(3));
138 GetFeaturesInput featuresInput = featuresBuilder.build();
140 LOGGER.debug("Requesting features ");
141 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
142 featuresInput).get(2500, TimeUnit.MILLISECONDS);
143 if (rpcResult.isSuccessful()) {
144 LOGGER.debug("DatapathId: {}", rpcResult.getResult().getDatapathId());
146 RpcError rpcError = rpcResult.getErrors().iterator().next();
147 LOGGER.warn("rpcResult failed", rpcError.getCause());
149 } catch (InterruptedException | ExecutionException | TimeoutException e) {
150 LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
154 protected void shutdown() throws InterruptedException, ExecutionException {
155 LOGGER.debug("MockPlugin.shutdown() sleeping 5... : {}", System.identityHashCode(this));
157 if (adapter != null) {
158 Future<Boolean> disconnect = adapter.disconnect();
160 LOGGER.debug("MockPlugin.shutdown() Disconnected");
163 finishedFuture.set(null);
167 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
168 LOGGER.debug("MultipartReply message received");
173 public void onPacketInMessage(PacketInMessage notification) {
174 LOGGER.debug("PacketIn message received");
175 LOGGER.debug("BufferId: {}", notification.getBufferId());
176 LOGGER.debug("TotalLength: {}", notification.getTotalLen());
177 LOGGER.debug("Reason: {}", notification.getReason());
178 LOGGER.debug("TableId: {}", notification.getTableId());
179 LOGGER.debug("Cookie: {}", notification.getCookie());
180 LOGGER.debug("Class: {}", notification.getMatch().getMatchEntry().get(0).getOxmClass());
181 LOGGER.debug("Field: {}", notification.getMatch().getMatchEntry().get(0).getOxmMatchField());
182 LOGGER.debug("Datasize: {}", notification.getData().length);
186 public void onPortStatusMessage(PortStatusMessage notification) {
187 LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
192 public void onDisconnectEvent(DisconnectEvent notification) {
193 LOGGER.debug("disconnection occured: {}", notification.getInfo());
196 public SettableFuture<Void> getFinishedFuture() {
197 return finishedFuture;
201 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
202 LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", notification.getInfo());
207 * Returns number of occurred idleEvents.
209 public int getIdleCounter() {
214 public void onConnectionReady() {
215 LOGGER.trace("MockPlugin().onConnectionReady()");
219 * Initiates connection to device.
221 * @param switchConnectionProvider the SwitchConnectionProviderImpl
222 * @param host - host IP
223 * @param port - port number
225 public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) {
226 LOGGER.trace("MockPlugin().initiateConnection()");
227 switchConnectionProvider.initiateConnection(host, port);
231 public void onSslConnectionError(SslConnectionError notification) {
232 LOGGER.debug("Ssl error occured: {}", notification.getInfo());