Revert "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         }
79         connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
80         connConfig.setTransferProtocol(protocol);
81         mockPlugin = new MockPlugin();
82
83         switchConnectionProvider = new SwitchConnectionProviderImpl();
84         switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
85         switchConnectionProvider.setConfiguration(connConfig);
86         switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
87         if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
88             final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
89             port = tcpHandler.getPort();
90         } else {
91             final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
92             port = udpHandler.getPort();
93         }
94     }
95
96     /**
97      * @throws Exception
98      */
99     @After
100     public void tearDown() throws Exception {
101         switchConnectionProvider.close();
102         LOGGER.debug("\n ending test -------------------------------");
103     }
104
105     /**
106      * Library integration and communication test with handshake
107      * @throws Exception
108      */
109     @Test
110     public void testHandshake() throws Exception {
111         setUp(TransportProtocol.TCP);
112         final int amountOfCLients = 1;
113         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
114         final ScenarioHandler handler = new ScenarioHandler(scenario);
115         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
116         final OFClient firstClient = clients.get(0);
117         firstClient.getScenarioDone().get();
118         Thread.sleep(1000);
119
120         LOGGER.debug("testHandshake() Finished") ;
121     }
122
123     /**
124      * Library integration and secured communication test with handshake
125      * @throws Exception
126      */
127     @Test
128     public void testTlsHandshake() throws Exception {
129         setUp(TransportProtocol.TLS);
130         final int amountOfCLients = 1;
131         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
132         final ScenarioHandler handler = new ScenarioHandler(scenario);
133         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
134         final OFClient firstClient = clients.get(0);
135         firstClient.getScenarioDone().get();
136         Thread.sleep(1000);
137
138         LOGGER.debug("testTlsHandshake() Finished") ;
139     }
140
141     /**
142      * Library integration and communication test with handshake + echo exchange
143      * @throws Exception
144      */
145     @Test
146     public void testHandshakeAndEcho() throws Exception {
147         setUp(TransportProtocol.TCP);
148         final int amountOfCLients = 1;
149         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
150         scenario.addFirst(new SleepEvent(1000));
151         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
152         scenario.addFirst(new SleepEvent(1000));
153         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
154         final ScenarioHandler handler = new ScenarioHandler(scenario);
155         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
156         final OFClient firstClient = clients.get(0);
157         firstClient.getScenarioDone().get();
158
159         LOGGER.debug("testHandshakeAndEcho() Finished") ;
160     }
161
162     /**
163      * Library integration and secured communication test with handshake + echo exchange
164      * @throws Exception
165      */
166     @Test
167     public void testTlsHandshakeAndEcho() throws Exception {
168         setUp(TransportProtocol.TLS);
169         final int amountOfCLients = 1;
170         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
171         scenario.addFirst(new SleepEvent(1000));
172         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
173         scenario.addFirst(new SleepEvent(1000));
174         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
175         final ScenarioHandler handler = new ScenarioHandler(scenario);
176         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
177         final OFClient firstClient = clients.get(0);
178         firstClient.getScenarioDone().get();
179
180         LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
181     }
182
183     /**
184      * Library udp integration and communication test with handshake + echo exchange
185      * @throws Exception
186      */
187     @Test
188     public void testUdpHandshakeAndEcho() throws Exception {
189         setUp(TransportProtocol.UDP);
190         final int amountOfCLients = 1;
191         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
192         scenario.addFirst(new SleepEvent(1000));
193         scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
194         scenario.addFirst(new SleepEvent(1000));
195         scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
196         final ScenarioHandler handler = new ScenarioHandler(scenario);
197         final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP, ClientType.SIMPLE);
198         final OFClient firstClient = clients.get(0);
199         firstClient.getScenarioDone().get();
200
201         LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
202     }
203
204     /**
205      * Library integration and communication test (with virtual machine)
206      * @throws Exception
207      */
208     //@Test
209     public void testCommunicationWithVM() throws Exception {
210         mockPlugin.getFinishedFuture().get();
211     }
212
213     /**
214      * @param amountOfCLients
215      * @param protocol true if encrypted connection should be used
216      * @return new clients up and running
217      * @throws ExecutionException if some client could not start
218      */
219     private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
220             final TransportProtocol protocol, final ClientType clientType) throws ExecutionException {
221         final List<OFClient> clientsHorde = new ArrayList<>();
222         for (int i = 0; i < amountOfCLients; i++) {
223             LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
224             OFClient sc = null;
225             if (clientType == ClientType.SIMPLE) {
226                 if (protocol.equals(TransportProtocol.TCP)) {
227                     sc = new SimpleClient(startupAddress.getHostAddress(), port);
228                     sc.setSecuredClient(false);
229                 } else if (protocol.equals(TransportProtocol.TLS)) {
230                     sc = new SimpleClient(startupAddress.getHostAddress(), port);
231                     sc.setSecuredClient(true);
232                 } else {
233                     sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
234                 }
235             } else if (clientType == ClientType.LISTENING) {
236                 sc = new ListeningSimpleClient(0);
237                 sc.setScenarioHandler(scenarioHandler);
238                 sc.setSecuredClient(false);
239             } else {
240                 LOGGER.error("Unknown type of client.");
241                 throw new IllegalStateException("Unknown type of client.");
242             }
243
244             sc.setScenarioHandler(scenarioHandler);
245             clientsHorde.add(sc);
246             //sc.run();
247             t = new Thread(sc);
248             t.start();
249         }
250         for (final OFClient sc : clientsHorde) {
251             try {
252                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
253             } catch (final Exception e) {
254                 LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
255                 throw new ExecutionException(e);
256             }
257         }
258         return clientsHorde;
259     }
260
261     /**
262      * @throws Exception
263      */
264     @Test
265     public void testInitiateConnection() throws Exception {
266         setUp(TransportProtocol.TCP);
267
268         final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
269         final ScenarioHandler handler = new ScenarioHandler(scenario);
270         final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
271         final OFClient ofClient = clients.get(0);
272         ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
273         final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
274         mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
275         ofClient.getScenarioDone().get();
276         LOGGER.debug("testInitiateConnection() Finished") ;
277     }
278 }