Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / it / integration / MockPlugin.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowjava.protocol.it.integration;
9
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;
47
48 /**
49  * Mock plugin.
50  *
51  * @author michal.polkorab
52  */
53 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
54         SystemNotificationsListener, ConnectionReadyListener {
55
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;
61
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));
67     }
68
69     @Override
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);
77     }
78
79     @Override
80     public boolean accept(InetAddress switchAddress) {
81         LOGGER.debug("MockPlugin.accept(): {}", switchAddress.toString());
82
83         return true;
84     }
85
86     @Override
87     public void onEchoRequestMessage(final EchoRequestMessage notification) {
88         LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: {}", adapter);
89         new Thread(() -> {
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);
98         }).start();
99     }
100
101     @Override
102     public void onErrorMessage(ErrorMessage notification) {
103         LOGGER.debug("Error message received");
104
105     }
106
107     @Override
108     public void onExperimenterMessage(ExperimenterMessage notification) {
109         LOGGER.debug("Experimenter message received");
110
111     }
112
113     @Override
114     public void onFlowRemovedMessage(FlowRemovedMessage notification) {
115         LOGGER.debug("FlowRemoved message received");
116
117     }
118
119     @Override
120     public void onHelloMessage(HelloMessage notification) {
121         new Thread(() -> {
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();
127             adapter.hello(hi);
128             LOGGER.debug("hello msg sent");
129             new Thread(this::getSwitchFeatures).start();
130         }).start();
131
132     }
133
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();
139         try {
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());
145             } else {
146                 RpcError rpcError = rpcResult.getErrors().iterator().next();
147                 LOGGER.warn("rpcResult failed", rpcError.getCause());
148             }
149         } catch (InterruptedException | ExecutionException | TimeoutException e) {
150             LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
151         }
152     }
153
154     protected void shutdown() throws InterruptedException, ExecutionException {
155         LOGGER.debug("MockPlugin.shutdown() sleeping 5... : {}", System.identityHashCode(this));
156         Thread.sleep(500);
157         if (adapter != null) {
158             Future<Boolean> disconnect = adapter.disconnect();
159             disconnect.get();
160             LOGGER.debug("MockPlugin.shutdown() Disconnected");
161         }
162
163         finishedFuture.set(null);
164     }
165
166     @Override
167     public void onMultipartReplyMessage(MultipartReplyMessage notification) {
168         LOGGER.debug("MultipartReply message received");
169
170     }
171
172     @Override
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);
183     }
184
185     @Override
186     public void onPortStatusMessage(PortStatusMessage notification) {
187         LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
188
189     }
190
191     @Override
192     public void onDisconnectEvent(DisconnectEvent notification) {
193         LOGGER.debug("disconnection occured: {}", notification.getInfo());
194     }
195
196     public SettableFuture<Void> getFinishedFuture() {
197         return finishedFuture;
198     }
199
200     @Override
201     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
202         LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", notification.getInfo());
203         idleCounter++;
204     }
205
206     /**
207      * Returns number of occurred idleEvents.
208      */
209     public int getIdleCounter() {
210         return idleCounter;
211     }
212
213     @Override
214     public void onConnectionReady() {
215         LOGGER.trace("MockPlugin().onConnectionReady()");
216     }
217
218     /**
219      * Initiates connection to device.
220      *
221      * @param switchConnectionProvider the SwitchConnectionProviderImpl
222      * @param host                     - host IP
223      * @param port                     - port number
224      */
225     public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) {
226         LOGGER.trace("MockPlugin().initiateConnection()");
227         switchConnectionProvider.initiateConnection(host, port);
228     }
229
230     @Override
231     public void onSslConnectionError(SslConnectionError notification) {
232         LOGGER.debug("Ssl error occured: {}", notification.getInfo());
233
234     }
235 }