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