2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.it.integration;
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;
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;
38 * @author michal.polkorab
39 * @author timotej.kubas
41 public class IntegrationTest {
43 private static final Logger LOGGER = LoggerFactory
44 .getLogger(IntegrationTest.class);
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;
56 * @param secured true if an encrypted connection should be used
59 public void setUp(boolean secured) throws Exception {
60 LOGGER.debug("\n starting test -------------------------------");
62 String currentDir = System.getProperty("user.dir");
63 LOGGER.debug("Current dir using System:" +currentDir);
64 startupAddress = InetAddress.getLocalHost();
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);
71 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, null, SWITCH_IDLE_TIMEOUT);
73 mockPlugin = new MockPlugin();
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();
87 public void tearDown() throws Exception {
88 switchConnectionProvider.close();
89 LOGGER.debug("\n ending test -------------------------------");
94 * Library integration and communication test with handshake
98 public void testHandshake() throws Exception {
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();
108 LOGGER.debug("testHandshake() Finished") ;
112 * Library integration and communication test with handshake
116 public void testTlsHandshake() throws Exception {
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();
126 LOGGER.debug("testTlsHandshake() Finished") ;
130 * Library integration and communication test with handshake + echo exchange
134 public void testHandshakeAndEcho() throws Exception {
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();
147 LOGGER.debug("testHandshakeAndEcho() Finished") ;
151 * Library integration and communication test with handshake + echo exchange
155 public void testTlsHandshakeAndEcho() throws Exception {
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();
168 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
172 * Library integration and communication test (with virtual machine)
176 public void testCommunicationWithVM() throws Exception {
177 mockPlugin.getFinishedFuture().get();
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
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);
197 for (SimpleClient sc : clientsHorde) {
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);