Merge "Bug 1277 - Move ByteBuffUtils to separate bundle"
[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.ScenarioFactory;
24 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
25 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
26 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
28 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
29 import org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl;
30 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
31 import org.opendaylight.openflowjava.util.ByteBufUtils;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * @author michal.polkorab
39  * @author timotej.kubas
40  */
41 public class IntegrationTest {
42
43     private static final Logger LOGGER = LoggerFactory
44             .getLogger(IntegrationTest.class);
45     
46     private static int port;
47     private TlsConfiguration tlsConfiguration ;
48     private static final int SWITCH_IDLE_TIMEOUT = 2000;
49     private static final long CONNECTION_TIMEOUT = 2000;
50     private InetAddress startupAddress;
51     private MockPlugin mockPlugin;
52     private SwitchConnectionProviderImpl switchConnectionProvider;
53     private ConnectionConfigurationImpl connConfig;
54
55     /**
56      * @param secured true if an encrypted connection should be used
57      * @throws Exception
58      */
59     public void setUp(boolean secured) throws Exception {
60         LOGGER.debug("\n starting test -------------------------------");
61         
62         String currentDir = System.getProperty("user.dir");
63         LOGGER.debug("Current dir using System:" +currentDir);
64         startupAddress = InetAddress.getLocalHost();
65         if (secured) {
66             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
67                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
68                     "/selfSignedController", PathType.CLASSPATH) ;
69             connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
70         } else {
71             connConfig = new ConnectionConfigurationImpl(startupAddress, 0, null, SWITCH_IDLE_TIMEOUT);
72         }
73         mockPlugin = new MockPlugin();
74         
75         switchConnectionProvider = new SwitchConnectionProviderImpl();
76         switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
77         switchConnectionProvider.setConfiguration(connConfig);
78         switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
79         TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
80         port = tcpHandler.getPort();
81     }
82
83     /**
84      * @throws Exception
85      */
86     @After
87     public void tearDown() throws Exception {
88         switchConnectionProvider.close();
89         LOGGER.debug("\n ending test -------------------------------");
90
91     }
92
93     /**
94      * Library integration and communication test with handshake
95      * @throws Exception 
96      */
97     @Test
98     public void testHandshake() throws Exception {
99         setUp(false);
100         int amountOfCLients = 1;
101         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
102         ScenarioHandler handler = new ScenarioHandler(scenario);
103         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler, false);
104         SimpleClient firstClient = clients.get(0);
105         firstClient.getScenarioDone().get();
106         Thread.sleep(1000);
107         
108         LOGGER.debug("testHandshake() Finished") ;
109     }
110
111     /**
112      * Library integration and communication test with handshake
113      * @throws Exception 
114      */
115     @Test
116     public void testTlsHandshake() throws Exception {
117         setUp(true);
118         int amountOfCLients = 1;
119         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
120         ScenarioHandler handler = new ScenarioHandler(scenario);
121         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler, true);
122         SimpleClient firstClient = clients.get(0);
123         firstClient.getScenarioDone().get();
124         Thread.sleep(1000);
125         
126         LOGGER.debug("testTlsHandshake() Finished") ;
127     }
128
129     /**
130      * Library integration and communication test with handshake + echo exchange
131      * @throws Exception 
132      */
133     @Test
134     public void testHandshakeAndEcho() throws Exception {
135         setUp(false);
136         int amountOfCLients = 1;
137         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
138         scenario.add(0, new SleepEvent(1000));
139         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
140         scenario.add(0, new SleepEvent(1000));
141         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
142         ScenarioHandler handler = new ScenarioHandler(scenario);
143         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler, false);
144         SimpleClient firstClient = clients.get(0);
145         firstClient.getScenarioDone().get();
146
147         LOGGER.debug("testHandshakeAndEcho() Finished") ;
148     }
149
150     /**
151      * Library integration and communication test with handshake + echo exchange
152      * @throws Exception 
153      */
154     @Test
155     public void testTlsHandshakeAndEcho() throws Exception {
156         setUp(true);
157         int amountOfCLients = 1;
158         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
159         scenario.add(0, new SleepEvent(1000));
160         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
161         scenario.add(0, new SleepEvent(1000));
162         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
163         ScenarioHandler handler = new ScenarioHandler(scenario);
164         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler, true);
165         SimpleClient firstClient = clients.get(0);
166         firstClient.getScenarioDone().get();
167
168         LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
169     }
170
171     /**
172      * Library integration and communication test (with virtual machine)        
173      * @throws Exception        
174      */     
175     //@Test         
176     public void testCommunicationWithVM() throws Exception {        
177         mockPlugin.getFinishedFuture().get();       
178     }
179
180     /**
181      * @param amountOfCLients 
182      * @param secured true if encrypted connection should be used
183      * @return new clients up and running
184      * @throws ExecutionException if some client could not start
185      */
186     private List<SimpleClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler,
187             boolean secured) throws ExecutionException {
188         List<SimpleClient> clientsHorde = new ArrayList<>();
189         for (int i = 0; i < amountOfCLients; i++) {
190             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
191             SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), port);
192             sc.setSecuredClient(secured);
193             sc.setScenarioHandler(scenarioHandler);
194             clientsHorde.add(sc);
195             sc.start();
196         }
197         for (SimpleClient sc : clientsHorde) {
198             try {
199                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
200             } catch (Exception e) {
201                 LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
202                 throw new ExecutionException(e);
203             }
204         }
205         return clientsHorde;
206     }
207
208 }