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