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.Deque;
14 import java.util.List;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.After;
18 import org.junit.Test;
19 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
20 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
21 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
22 import org.opendaylight.openflowjava.protocol.impl.clients.ListeningSimpleClient;
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;
62 private enum ClientType {SIMPLE, LISTENING}
64 * @param protocol communication protocol to be used during test
67 public void setUp(final TransportProtocol protocol) throws Exception {
68 LOGGER.debug("\n starting test -------------------------------");
70 final String currentDir = System.getProperty("user.dir");
71 LOGGER.debug("Current dir using System: {}", currentDir);
72 startupAddress = InetAddress.getLocalHost();
73 tlsConfiguration = null;
74 if (protocol.equals(TransportProtocol.TLS)) {
75 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
76 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
77 "/selfSignedController", PathType.CLASSPATH) ;
79 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
80 connConfig.setTransferProtocol(protocol);
81 mockPlugin = new MockPlugin();
83 switchConnectionProvider = new SwitchConnectionProviderImpl();
84 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
85 switchConnectionProvider.setConfiguration(connConfig);
86 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
87 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
88 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
89 port = tcpHandler.getPort();
91 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
92 port = udpHandler.getPort();
100 public void tearDown() throws Exception {
101 switchConnectionProvider.close();
102 LOGGER.debug("\n ending test -------------------------------");
106 * Library integration and communication test with handshake
110 public void testHandshake() throws Exception {
111 setUp(TransportProtocol.TCP);
112 final int amountOfCLients = 1;
113 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
114 final ScenarioHandler handler = new ScenarioHandler(scenario);
115 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
116 final OFClient firstClient = clients.get(0);
117 firstClient.getScenarioDone().get();
120 LOGGER.debug("testHandshake() Finished") ;
124 * Library integration and secured communication test with handshake
128 public void testTlsHandshake() throws Exception {
129 setUp(TransportProtocol.TLS);
130 final int amountOfCLients = 1;
131 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
132 final ScenarioHandler handler = new ScenarioHandler(scenario);
133 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
134 final OFClient firstClient = clients.get(0);
135 firstClient.getScenarioDone().get();
138 LOGGER.debug("testTlsHandshake() Finished") ;
142 * Library integration and communication test with handshake + echo exchange
146 public void testHandshakeAndEcho() throws Exception {
147 setUp(TransportProtocol.TCP);
148 final int amountOfCLients = 1;
149 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
150 scenario.addFirst(new SleepEvent(1000));
151 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
152 scenario.addFirst(new SleepEvent(1000));
153 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
154 final ScenarioHandler handler = new ScenarioHandler(scenario);
155 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
156 final OFClient firstClient = clients.get(0);
157 firstClient.getScenarioDone().get();
159 LOGGER.debug("testHandshakeAndEcho() Finished") ;
163 * Library integration and secured communication test with handshake + echo exchange
167 public void testTlsHandshakeAndEcho() throws Exception {
168 setUp(TransportProtocol.TLS);
169 final int amountOfCLients = 1;
170 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
171 scenario.addFirst(new SleepEvent(1000));
172 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
173 scenario.addFirst(new SleepEvent(1000));
174 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
175 final ScenarioHandler handler = new ScenarioHandler(scenario);
176 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
177 final OFClient firstClient = clients.get(0);
178 firstClient.getScenarioDone().get();
180 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
184 * Library udp integration and communication test with handshake + echo exchange
188 public void testUdpHandshakeAndEcho() throws Exception {
189 setUp(TransportProtocol.UDP);
190 final int amountOfCLients = 1;
191 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
192 scenario.addFirst(new SleepEvent(1000));
193 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
194 scenario.addFirst(new SleepEvent(1000));
195 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
196 final ScenarioHandler handler = new ScenarioHandler(scenario);
197 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP, ClientType.SIMPLE);
198 final OFClient firstClient = clients.get(0);
199 firstClient.getScenarioDone().get();
201 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
205 * Library integration and communication test (with virtual machine)
209 public void testCommunicationWithVM() throws Exception {
210 mockPlugin.getFinishedFuture().get();
214 * @param amountOfCLients
215 * @param protocol true if encrypted connection should be used
216 * @return new clients up and running
217 * @throws ExecutionException if some client could not start
219 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
220 final TransportProtocol protocol, final ClientType clientType) throws ExecutionException {
221 final List<OFClient> clientsHorde = new ArrayList<>();
222 for (int i = 0; i < amountOfCLients; i++) {
223 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
225 if (clientType == ClientType.SIMPLE) {
226 if (protocol.equals(TransportProtocol.TCP)) {
227 sc = new SimpleClient(startupAddress.getHostAddress(), port);
228 sc.setSecuredClient(false);
229 } else if (protocol.equals(TransportProtocol.TLS)) {
230 sc = new SimpleClient(startupAddress.getHostAddress(), port);
231 sc.setSecuredClient(true);
233 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
235 } else if (clientType == ClientType.LISTENING) {
236 sc = new ListeningSimpleClient(0);
237 sc.setScenarioHandler(scenarioHandler);
238 sc.setSecuredClient(false);
240 LOGGER.error("Unknown type of client.");
241 throw new IllegalStateException("Unknown type of client.");
244 sc.setScenarioHandler(scenarioHandler);
245 clientsHorde.add(sc);
250 for (final OFClient sc : clientsHorde) {
252 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
253 } catch (final Exception e) {
254 LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
255 throw new ExecutionException(e);
265 public void testInitiateConnection() throws Exception {
266 setUp(TransportProtocol.TCP);
268 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
269 final ScenarioHandler handler = new ScenarioHandler(scenario);
270 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
271 final OFClient ofClient = clients.get(0);
272 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
273 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
274 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
275 ofClient.getScenarioDone().get();
276 LOGGER.debug("testInitiateConnection() Finished") ;