a1440961f86f17d9ec20b32f1e384cba62fa5768
[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.List;
14 import java.util.Stack;
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.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     /**
61      * @param protocol communication protocol to be used during test
62      * @throws Exception
63      */
64     public void setUp(TransportProtocol protocol) throws Exception {
65         LOGGER.debug("\n starting test -------------------------------");
66         
67         String currentDir = System.getProperty("user.dir");
68         LOGGER.debug("Current dir using System:" +currentDir);
69         startupAddress = InetAddress.getLocalHost();
70         tlsConfiguration = null;
71         if (protocol.equals(TransportProtocol.TLS)) {
72             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
73                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
74                     "/selfSignedController", PathType.CLASSPATH) ;
75         }
76         connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
77         connConfig.setTransferProtocol(protocol);
78         mockPlugin = new MockPlugin();
79         
80         switchConnectionProvider = new SwitchConnectionProviderImpl();
81         switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
82         switchConnectionProvider.setConfiguration(connConfig);
83         switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
84         if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
85             TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
86             port = tcpHandler.getPort();
87         } else {
88             UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
89             port = udpHandler.getPort();
90         }
91     }
92
93     /**
94      * @throws Exception
95      */
96     @After
97     public void tearDown() throws Exception {
98         switchConnectionProvider.close();
99         LOGGER.debug("\n ending test -------------------------------");
100     }
101
102     /**
103      * Library integration and communication test with handshake
104      * @throws Exception 
105      */
106     @Test
107     public void testHandshake() throws Exception {
108         setUp(TransportProtocol.TCP);
109         int amountOfCLients = 1;
110         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
111         ScenarioHandler handler = new ScenarioHandler(scenario);
112         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP);
113         OFClient firstClient = clients.get(0);
114         firstClient.getScenarioDone().get();
115         Thread.sleep(1000);
116         
117         LOGGER.debug("testHandshake() Finished") ;
118     }
119
120     /**
121      * Library integration and secured communication test with handshake
122      * @throws Exception 
123      */
124     @Test
125     public void testTlsHandshake() throws Exception {
126         setUp(TransportProtocol.TLS);
127         int amountOfCLients = 1;
128         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
129         ScenarioHandler handler = new ScenarioHandler(scenario);
130         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS);
131         OFClient firstClient = clients.get(0);
132         firstClient.getScenarioDone().get();
133         Thread.sleep(1000);
134         
135         LOGGER.debug("testTlsHandshake() Finished") ;
136     }
137
138     /**
139      * Library integration and communication test with handshake + echo exchange
140      * @throws Exception 
141      */
142     @Test
143     public void testHandshakeAndEcho() throws Exception {
144         setUp(TransportProtocol.TCP);
145         int amountOfCLients = 1;
146         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
147         scenario.add(0, new SleepEvent(1000));
148         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
149         scenario.add(0, new SleepEvent(1000));
150         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
151         ScenarioHandler handler = new ScenarioHandler(scenario);
152         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP);
153         OFClient firstClient = clients.get(0);
154         firstClient.getScenarioDone().get();
155
156         LOGGER.debug("testHandshakeAndEcho() Finished") ;
157     }
158
159     /**
160      * Library integration and secured communication test with handshake + echo exchange
161      * @throws Exception 
162      */
163     @Test
164     public void testTlsHandshakeAndEcho() throws Exception {
165         setUp(TransportProtocol.TLS);
166         int amountOfCLients = 1;
167         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
168         scenario.add(0, new SleepEvent(1000));
169         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
170         scenario.add(0, new SleepEvent(1000));
171         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
172         ScenarioHandler handler = new ScenarioHandler(scenario);
173         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS);
174         OFClient firstClient = clients.get(0);
175         firstClient.getScenarioDone().get();
176
177         LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
178     }
179
180     /**
181      * Library udp integration and communication test with handshake + echo exchange
182      * @throws Exception 
183      */
184     @Test
185     public void testUdpHandshakeAndEcho() throws Exception {
186         setUp(TransportProtocol.UDP);
187         int amountOfCLients = 1;
188         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
189         scenario.add(0, new SleepEvent(1000));
190         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
191         scenario.add(0, new SleepEvent(1000));
192         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
193         ScenarioHandler handler = new ScenarioHandler(scenario);
194         List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP);
195         OFClient firstClient = clients.get(0);
196         firstClient.getScenarioDone().get();
197
198         LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
199     }
200
201     /**
202      * Library integration and communication test (with virtual machine)
203      * @throws Exception
204      */
205     //@Test
206     public void testCommunicationWithVM() throws Exception {
207         mockPlugin.getFinishedFuture().get();
208     }
209
210     /**
211      * @param amountOfCLients 
212      * @param protocol true if encrypted connection should be used
213      * @return new clients up and running
214      * @throws ExecutionException if some client could not start
215      */
216     private List<OFClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler,
217             TransportProtocol protocol) throws ExecutionException {
218         List<OFClient> clientsHorde = new ArrayList<>();
219         for (int i = 0; i < amountOfCLients; i++) {
220             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
221             OFClient sc = null;
222             if (protocol.equals(TransportProtocol.TCP)) {
223                 sc = new SimpleClient(startupAddress.getHostAddress(), port);
224                 sc.setSecuredClient(false);
225             } else if (protocol.equals(TransportProtocol.TLS)) {
226                 sc = new SimpleClient(startupAddress.getHostAddress(), port);
227                 sc.setSecuredClient(true);
228             } else {
229                 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
230             }
231             sc.setScenarioHandler(scenarioHandler);
232             clientsHorde.add(sc);
233             sc.run();
234         }
235         for (OFClient sc : clientsHorde) {
236             try {
237                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
238             } catch (Exception e) {
239                 LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
240                 throw new ExecutionException(e);
241             }
242         }
243         return clientsHorde;
244     }
245
246 }