Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / it / integration / IntegrationTest.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 java.net.InetAddress;
11 import java.util.ArrayList;
12 import java.util.Deque;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.ExecutorService;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.junit.After;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentMatchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
26 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
27 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
28 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
29 import org.opendaylight.openflowjava.protocol.impl.clients.ListeningSimpleClient;
30 import org.opendaylight.openflowjava.protocol.impl.clients.OFClient;
31 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
32 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
33 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
34 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
35 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
36 import org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClient;
37 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
38 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
39 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
40 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
41 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionConfigurationImpl;
42 import org.opendaylight.openflowjava.util.ByteBufUtils;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 /**
50  * End-to-end integration test.
51  *
52  * @author michal.polkorab
53  * @author timotej.kubas
54  */
55 @RunWith(MockitoJUnitRunner.class)
56 public class IntegrationTest {
57
58     private static final Logger LOGGER = LoggerFactory
59             .getLogger(IntegrationTest.class);
60
61     private static int port;
62     private TlsConfiguration tlsConfiguration;
63     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
64     private static final int SWITCH_IDLE_TIMEOUT = 2000;
65     private static final long CONNECTION_TIMEOUT = 2000;
66     private InetAddress startupAddress;
67     private MockPlugin mockPlugin;
68     private SwitchConnectionProviderImpl switchConnectionProvider;
69     private ConnectionConfigurationImpl connConfig;
70     @Mock
71     private ExecutorService executorService;
72
73     private Thread thread;
74
75     private enum ClientType {
76         SIMPLE,
77         LISTENING
78     }
79
80     public void setUp(final TransportProtocol protocol) throws Exception {
81         LOGGER.debug("\n starting test -------------------------------");
82         Mockito.doAnswer(invocation -> {
83             ((Runnable)invocation.getArguments()[0]).run();
84             return null;
85         }).when(executorService).execute(ArgumentMatchers.any());
86
87         final String currentDir = System.getProperty("user.dir");
88         LOGGER.debug("Current dir using System: {}", currentDir);
89         startupAddress = InetAddress.getLocalHost();
90         tlsConfiguration = null;
91         if (protocol.equals(TransportProtocol.TLS)) {
92             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
93                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
94                     "/selfSignedController", PathType.CLASSPATH,
95                     List.of());
96         }
97         connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration,
98                 SWITCH_IDLE_TIMEOUT, true, false, CHANNEL_OUTBOUND_QUEUE_SIZE);
99         connConfig.setTransferProtocol(protocol);
100         mockPlugin = new MockPlugin(executorService);
101
102         switchConnectionProvider = new SwitchConnectionProviderImpl(connConfig,
103                 Mockito.mock(OpenflowDiagStatusProvider.class));
104         switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
105         switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
106         if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
107             final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
108             port = tcpHandler.getPort();
109         } else {
110             final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
111             port = udpHandler.getPort();
112         }
113     }
114
115     @After
116     public void tearDown() {
117         switchConnectionProvider.close();
118         LOGGER.debug("\n ending test -------------------------------");
119     }
120
121     /**
122      * Library integration and communication test with handshake.
123      */
124     @Test
125     public void testHandshake() throws Exception {
126         setUp(TransportProtocol.TCP);
127         final int amountOfCLients = 1;
128         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
129         final ScenarioHandler handler = new ScenarioHandler(scenario);
130         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
131                 TransportProtocol.TCP, ClientType.SIMPLE);
132         final OFClient firstClient = clients.get(0);
133         firstClient.getScenarioDone().get();
134         Thread.sleep(1000);
135
136         LOGGER.debug("testHandshake() Finished") ;
137     }
138
139     /**
140      * Library integration and secured communication test with handshake.
141      */
142     @Test
143     public void testTlsHandshake() throws Exception {
144         setUp(TransportProtocol.TLS);
145         final int amountOfCLients = 1;
146         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
147         final ScenarioHandler handler = new ScenarioHandler(scenario);
148         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
149                 TransportProtocol.TLS, ClientType.SIMPLE);
150         final OFClient firstClient = clients.get(0);
151         firstClient.getScenarioDone().get();
152         Thread.sleep(1000);
153
154         LOGGER.debug("testTlsHandshake() Finished") ;
155     }
156
157     /**
158      * Library integration and communication test with handshake + echo exchange.
159      */
160     @Test
161     public void testHandshakeAndEcho() throws Exception {
162         setUp(TransportProtocol.TCP);
163         final int amountOfCLients = 1;
164         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
165         scenario.addFirst(new SleepEvent(1000));
166         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
167         scenario.addFirst(new SleepEvent(1000));
168         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
169         final ScenarioHandler handler = new ScenarioHandler(scenario);
170         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
171                 TransportProtocol.TCP, ClientType.SIMPLE);
172         final OFClient firstClient = clients.get(0);
173         firstClient.getScenarioDone().get();
174
175         LOGGER.debug("testHandshakeAndEcho() Finished") ;
176     }
177
178     /**
179      * Library integration and secured communication test with handshake + echo exchange.
180      */
181     @Test
182     public void testTlsHandshakeAndEcho() throws Exception {
183         setUp(TransportProtocol.TLS);
184         final int amountOfCLients = 1;
185         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
186         scenario.addFirst(new SleepEvent(1000));
187         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
188         scenario.addFirst(new SleepEvent(1000));
189         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
190         final ScenarioHandler handler = new ScenarioHandler(scenario);
191         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
192                 TransportProtocol.TLS, ClientType.SIMPLE);
193         final OFClient firstClient = clients.get(0);
194         firstClient.getScenarioDone().get();
195
196         LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
197     }
198
199     /**
200      * Library udp integration and communication test with handshake + echo exchange.
201      */
202     @Test
203     public void testUdpHandshakeAndEcho() throws Exception {
204         setUp(TransportProtocol.UDP);
205         final int amountOfCLients = 1;
206         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
207         scenario.addFirst(new SleepEvent(1000));
208         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
209         scenario.addFirst(new SleepEvent(1000));
210         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
211         final ScenarioHandler handler = new ScenarioHandler(scenario);
212         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
213                 TransportProtocol.UDP, ClientType.SIMPLE);
214         final OFClient firstClient = clients.get(0);
215         firstClient.getScenarioDone().get();
216
217         LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
218     }
219
220     /**
221      * Library integration and communication test (with virtual machine).
222      */
223     //@Test
224     public void testCommunicationWithVM() throws Exception {
225         mockPlugin.getFinishedFuture().get();
226     }
227
228     /**
229      * Creates and start a client.
230      *
231      * @param amountOfCLients number of clients
232      * @param protocol true if encrypted connection should be used
233      * @return new clients up and running
234      * @throws ExecutionException if some client could not start
235      */
236     private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
237             final TransportProtocol protocol, final ClientType clientType)
238                     throws ExecutionException, InterruptedException, TimeoutException {
239         final List<OFClient> clientsHorde = new ArrayList<>();
240         for (int i = 0; i < amountOfCLients; i++) {
241             LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
242             OFClient sc = null;
243             if (clientType == ClientType.SIMPLE) {
244                 if (protocol.equals(TransportProtocol.TCP)) {
245                     sc = new SimpleClient(startupAddress.getHostAddress(), port);
246                     sc.setSecuredClient(false);
247                 } else if (protocol.equals(TransportProtocol.TLS)) {
248                     sc = new SimpleClient(startupAddress.getHostAddress(), port);
249                     sc.setSecuredClient(true);
250                 } else {
251                     sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
252                 }
253             } else if (clientType == ClientType.LISTENING) {
254                 sc = new ListeningSimpleClient(0);
255                 sc.setScenarioHandler(scenarioHandler);
256                 sc.setSecuredClient(false);
257             } else {
258                 LOGGER.error("Unknown type of client.");
259                 throw new IllegalStateException("Unknown type of client.");
260             }
261
262             sc.setScenarioHandler(scenarioHandler);
263             clientsHorde.add(sc);
264             //sc.run();
265             thread = new Thread(sc);
266             thread.start();
267         }
268         for (final OFClient sc : clientsHorde) {
269             sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
270         }
271         return clientsHorde;
272     }
273
274     @Test
275     public void testInitiateConnection() throws Exception {
276         setUp(TransportProtocol.TCP);
277
278         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
279         final ScenarioHandler handler = new ScenarioHandler(scenario);
280         final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
281         final OFClient ofClient = clients.get(0);
282         ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
283         final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
284         mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
285         ofClient.getScenarioDone().get();
286         LOGGER.debug("testInitiateConnection() Finished") ;
287     }
288 }