3eedff52d3720b418da8424df562d1bdb32825f0
[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
18 import org.junit.After;
19 import org.junit.Test;
20 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
21 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
22 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
23 import org.opendaylight.openflowjava.protocol.impl.clients.ListeningSimpleClient;
24 import org.opendaylight.openflowjava.protocol.impl.clients.OFClient;
25 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
26 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
28 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
29 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
30 import org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClient;
31 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
32 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
33 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
34 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
35 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionConfigurationImpl;
36 import org.opendaylight.openflowjava.util.ByteBufUtils;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * @author michal.polkorab
45  * @author timotej.kubas
46  */
47 public class IntegrationTest {
48
49     private static final Logger LOGGER = LoggerFactory
50             .getLogger(IntegrationTest.class);
51
52     private static int port;
53     private TlsConfiguration tlsConfiguration;
54     private static final int SWITCH_IDLE_TIMEOUT = 2000;
55     private static final long CONNECTION_TIMEOUT = 2000;
56     private InetAddress startupAddress;
57     private MockPlugin mockPlugin;
58     private SwitchConnectionProviderImpl switchConnectionProvider;
59     private ConnectionConfigurationImpl connConfig;
60
61     private Thread t;
62
63     private enum ClientType {SIMPLE, LISTENING}
64     /**
65      * @param protocol communication protocol to be used during test
66      * @throws Exception
67      */
68     public void setUp(TransportProtocol protocol) throws Exception {
69         LOGGER.debug("\n starting test -------------------------------");
70
71         String currentDir = System.getProperty("user.dir");
72         LOGGER.debug("Current dir using System:" +currentDir);
73         startupAddress = InetAddress.getLocalHost();
74         tlsConfiguration = null;
75         if (protocol.equals(TransportProtocol.TLS)) {
76             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
77                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
78                     "/selfSignedController", PathType.CLASSPATH) ;
79         }
80         connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
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             TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
90             port = tcpHandler.getPort();
91         } else {
92             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         int amountOfCLients = 1;
114         Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
115         ScenarioHandler handler = new ScenarioHandler(scenario);
116         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
117         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         int amountOfCLients = 1;
132         Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
133         ScenarioHandler handler = new ScenarioHandler(scenario);
134         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
135         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         int amountOfCLients = 1;
150         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         ScenarioHandler handler = new ScenarioHandler(scenario);
156         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
157         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         int amountOfCLients = 1;
171         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         ScenarioHandler handler = new ScenarioHandler(scenario);
177         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
178         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         int amountOfCLients = 1;
192         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         ScenarioHandler handler = new ScenarioHandler(scenario);
198         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP, ClientType.SIMPLE);
199         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(int amountOfCLients, ScenarioHandler scenarioHandler,
221             TransportProtocol protocol, ClientType clientType) throws ExecutionException {
222         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 (OFClient sc : clientsHorde) {
252             try {
253                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
254             } catch (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         Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
270         ScenarioHandler handler = new ScenarioHandler(scenario);
271         List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
272         OFClient ofClient = clients.get(0);
273         ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
274         int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
275         mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
276         ofClient.getScenarioDone().get();
277         LOGGER.debug("testInitiateConnection() Finished") ;
278     }
279 }