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.Future;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
17 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
18 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
19 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SslConnectionError;
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;
48 * @author michal.polkorab
50 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
51 SystemNotificationsListener, ConnectionReadyListener {
53 protected static final Logger LOGGER = LoggerFactory.getLogger(MockPlugin.class);
54 protected volatile ConnectionAdapter adapter;
55 private final SettableFuture<Void> finishedFuture;
56 private int idleCounter = 0;
59 LOGGER.trace("Creating MockPlugin");
60 finishedFuture = SettableFuture.create();
61 LOGGER.debug("mockPlugin: {}", System.identityHashCode(this));
65 public void onSwitchConnected(ConnectionAdapter connection) {
66 LOGGER.debug("onSwitchConnected: {}", connection);
67 this.adapter = connection;
68 connection.setMessageListener(this);
69 connection.setSystemListener(this);
70 connection.setConnectionReadyListener(this);
74 public boolean accept(InetAddress switchAddress) {
75 LOGGER.debug("MockPlugin.accept(): {}", switchAddress.toString());
81 public void onEchoRequestMessage(final EchoRequestMessage notification) {
82 LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: {}", adapter);
84 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() started adapter: {}", adapter);
85 EchoReplyInputBuilder replyBuilder = new EchoReplyInputBuilder();
86 replyBuilder.setVersion((short) 4);
87 replyBuilder.setXid(notification.getXid());
88 EchoReplyInput echoReplyInput = replyBuilder.build();
89 adapter.echoReply(echoReplyInput);
90 LOGGER.debug("adapter.EchoReply(Input) sent : ", echoReplyInput.toString());
91 LOGGER.debug("MockPlugin.onEchoRequestMessage().run() finished adapter: {}", adapter);
96 public void onErrorMessage(ErrorMessage notification) {
97 LOGGER.debug("Error message received");
102 public void onExperimenterMessage(ExperimenterMessage notification) {
103 LOGGER.debug("Experimenter message received");
108 public void onFlowRemovedMessage(FlowRemovedMessage notification) {
109 LOGGER.debug("FlowRemoved message received");
114 public void onHelloMessage(HelloMessage notification) {
116 LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
117 HelloInputBuilder hib = new HelloInputBuilder();
118 hib.setVersion((short) 4);
120 HelloInput hi = hib.build();
122 LOGGER.debug("hello msg sent");
123 new Thread(this::getSwitchFeatures).start();
128 protected void getSwitchFeatures() {
129 GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
130 featuresBuilder.setVersion((short) 4);
131 featuresBuilder.setXid(3L);
132 GetFeaturesInput featuresInput = featuresBuilder.build();
134 LOGGER.debug("Requesting features ");
135 RpcResult<GetFeaturesOutput> rpcResult = adapter.getFeatures(
136 featuresInput).get(2500, TimeUnit.MILLISECONDS);
137 if (rpcResult.isSuccessful()) {
138 LOGGER.debug("DatapathId: {}", rpcResult.getResult().getDatapathId());
140 RpcError rpcError = rpcResult.getErrors().iterator().next();
141 LOGGER.warn("rpcResult failed", rpcError.getCause());
143 } catch (InterruptedException | ExecutionException | TimeoutException e) {
144 LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
148 protected void shutdown() throws InterruptedException, ExecutionException {
149 LOGGER.debug("MockPlugin.shutdown() sleeping 5... : {}", System.identityHashCode(this));
151 if (adapter != null) {
152 Future<Boolean> disconnect = adapter.disconnect();
154 LOGGER.debug("MockPlugin.shutdown() Disconnected");
157 finishedFuture.set(null);
161 public void onMultipartReplyMessage(MultipartReplyMessage notification) {
162 LOGGER.debug("MultipartReply message received");
167 public void onPacketInMessage(PacketInMessage notification) {
168 LOGGER.debug("PacketIn message received");
169 LOGGER.debug("BufferId: {}", notification.getBufferId());
170 LOGGER.debug("TotalLength: {}", notification.getTotalLen());
171 LOGGER.debug("Reason: {}", notification.getReason());
172 LOGGER.debug("TableId: {}", notification.getTableId());
173 LOGGER.debug("Cookie: {}", notification.getCookie());
174 LOGGER.debug("Class: {}", notification.getMatch().getMatchEntry().get(0).getOxmClass());
175 LOGGER.debug("Field: {}", notification.getMatch().getMatchEntry().get(0).getOxmMatchField());
176 LOGGER.debug("Datasize: {}", notification.getData().length);
180 public void onPortStatusMessage(PortStatusMessage notification) {
181 LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
186 public void onDisconnectEvent(DisconnectEvent notification) {
187 LOGGER.debug("disconnection occured: {}", notification.getInfo());
190 public SettableFuture<Void> getFinishedFuture() {
191 return finishedFuture;
195 public void onSwitchIdleEvent(SwitchIdleEvent notification) {
196 LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", notification.getInfo());
201 * Returns number of occurred idleEvents.
203 public int getIdleCounter() {
208 public void onConnectionReady() {
209 LOGGER.trace("MockPlugin().onConnectionReady()");
213 * Initiates connection to device.
215 * @param switchConnectionProvider the SwitchConnectionProviderImpl
216 * @param host - host IP
217 * @param port - port number
219 public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) {
220 LOGGER.trace("MockPlugin().initiateConnection()");
221 switchConnectionProvider.initiateConnection(host, port);
225 public void onSslConnectionError(SslConnectionError notification) {
226 LOGGER.debug("Ssl error occured: {}", notification.getInfo());