OPNFLWPLUG-1078: Notify device TLS authentication failure messages
[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.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;
44
45 /**
46  * Mock plugin.
47  *
48  * @author michal.polkorab
49  */
50 public class MockPlugin implements OpenflowProtocolListener, SwitchConnectionHandler,
51         SystemNotificationsListener, ConnectionReadyListener {
52
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;
57
58     public MockPlugin() {
59         LOGGER.trace("Creating MockPlugin");
60         finishedFuture = SettableFuture.create();
61         LOGGER.debug("mockPlugin: {}", System.identityHashCode(this));
62     }
63
64     @Override
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);
71     }
72
73     @Override
74     public boolean accept(InetAddress switchAddress) {
75         LOGGER.debug("MockPlugin.accept(): {}", switchAddress.toString());
76
77         return true;
78     }
79
80     @Override
81     public void onEchoRequestMessage(final EchoRequestMessage notification) {
82         LOGGER.debug("MockPlugin.onEchoRequestMessage() adapter: {}", adapter);
83         new Thread(() -> {
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);
92         }).start();
93     }
94
95     @Override
96     public void onErrorMessage(ErrorMessage notification) {
97         LOGGER.debug("Error message received");
98
99     }
100
101     @Override
102     public void onExperimenterMessage(ExperimenterMessage notification) {
103         LOGGER.debug("Experimenter message received");
104
105     }
106
107     @Override
108     public void onFlowRemovedMessage(FlowRemovedMessage notification) {
109         LOGGER.debug("FlowRemoved message received");
110
111     }
112
113     @Override
114     public void onHelloMessage(HelloMessage notification) {
115         new Thread(() -> {
116             LOGGER.debug("MockPlugin.onHelloMessage().run() Hello message received");
117             HelloInputBuilder hib = new HelloInputBuilder();
118             hib.setVersion((short) 4);
119             hib.setXid(2L);
120             HelloInput hi = hib.build();
121             adapter.hello(hi);
122             LOGGER.debug("hello msg sent");
123             new Thread(this::getSwitchFeatures).start();
124         }).start();
125
126     }
127
128     protected void getSwitchFeatures() {
129         GetFeaturesInputBuilder featuresBuilder = new GetFeaturesInputBuilder();
130         featuresBuilder.setVersion((short) 4);
131         featuresBuilder.setXid(3L);
132         GetFeaturesInput featuresInput = featuresBuilder.build();
133         try {
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());
139             } else {
140                 RpcError rpcError = rpcResult.getErrors().iterator().next();
141                 LOGGER.warn("rpcResult failed", rpcError.getCause());
142             }
143         } catch (InterruptedException | ExecutionException | TimeoutException e) {
144             LOGGER.error("getSwitchFeatures() exception caught: ", e.getMessage(), e);
145         }
146     }
147
148     protected void shutdown() throws InterruptedException, ExecutionException {
149         LOGGER.debug("MockPlugin.shutdown() sleeping 5... : {}", System.identityHashCode(this));
150         Thread.sleep(500);
151         if (adapter != null) {
152             Future<Boolean> disconnect = adapter.disconnect();
153             disconnect.get();
154             LOGGER.debug("MockPlugin.shutdown() Disconnected");
155         }
156
157         finishedFuture.set(null);
158     }
159
160     @Override
161     public void onMultipartReplyMessage(MultipartReplyMessage notification) {
162         LOGGER.debug("MultipartReply message received");
163
164     }
165
166     @Override
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);
177     }
178
179     @Override
180     public void onPortStatusMessage(PortStatusMessage notification) {
181         LOGGER.debug("MockPlugin.onPortStatusMessage() message received");
182
183     }
184
185     @Override
186     public void onDisconnectEvent(DisconnectEvent notification) {
187         LOGGER.debug("disconnection occured: {}", notification.getInfo());
188     }
189
190     public SettableFuture<Void> getFinishedFuture() {
191         return finishedFuture;
192     }
193
194     @Override
195     public void onSwitchIdleEvent(SwitchIdleEvent notification) {
196         LOGGER.debug("MockPlugin.onSwitchIdleEvent() switch status: {}", notification.getInfo());
197         idleCounter ++;
198     }
199
200     /**
201      * Returns number of occurred idleEvents.
202      */
203     public int getIdleCounter() {
204         return idleCounter;
205     }
206
207     @Override
208     public void onConnectionReady() {
209         LOGGER.trace("MockPlugin().onConnectionReady()");
210     }
211
212     /**
213      * Initiates connection to device.
214      *
215      * @param switchConnectionProvider the SwitchConnectionProviderImpl
216      * @param host - host IP
217      * @param port - port number
218      */
219     public void initiateConnection(SwitchConnectionProviderImpl switchConnectionProvider, String host, int port) {
220         LOGGER.trace("MockPlugin().initiateConnection()");
221         switchConnectionProvider.initiateConnection(host, port);
222     }
223
224     @Override
225     public void onSslConnectionError(SslConnectionError notification) {
226         LOGGER.debug("Ssl error occured: {}", notification.getInfo());
227
228     }
229 }