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.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;
43 * @author michal.polkorab
44 * @author timotej.kubas
46 public class IntegrationTest {
48 private static final Logger LOGGER = LoggerFactory
49 .getLogger(IntegrationTest.class);
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;
61 * @param protocol communication protocol to be used during test
64 public void setUp(TransportProtocol protocol) throws Exception {
65 LOGGER.debug("\n starting test -------------------------------");
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) ;
76 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);
77 connConfig.setTransferProtocol(protocol);
78 mockPlugin = new MockPlugin();
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();
88 UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
89 port = udpHandler.getPort();
97 public void tearDown() throws Exception {
98 switchConnectionProvider.close();
99 LOGGER.debug("\n ending test -------------------------------");
103 * Library integration and communication test with handshake
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();
117 LOGGER.debug("testHandshake() Finished") ;
121 * Library integration and secured communication test with handshake
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();
135 LOGGER.debug("testTlsHandshake() Finished") ;
139 * Library integration and communication test with handshake + echo exchange
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();
156 LOGGER.debug("testHandshakeAndEcho() Finished") ;
160 * Library integration and secured communication test with handshake + echo exchange
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();
177 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
181 * Library udp integration and communication test with handshake + echo exchange
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();
198 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
202 * Library integration and communication test (with virtual machine)
206 public void testCommunicationWithVM() throws Exception {
207 mockPlugin.getFinishedFuture().get();
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
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());
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);
229 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
231 sc.setScenarioHandler(scenarioHandler);
232 clientsHorde.add(sc);
235 for (OFClient sc : clientsHorde) {
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);