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,
78 new ArrayList<String>());
80 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
81 connConfig.setTransferProtocol(protocol);
82 mockPlugin = new MockPlugin();
84 switchConnectionProvider = new SwitchConnectionProviderImpl();
85 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
86 switchConnectionProvider.setConfiguration(connConfig);
87 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
88 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
89 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
90 port = tcpHandler.getPort();
92 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
93 port = udpHandler.getPort();
101 public void tearDown() throws Exception {
102 switchConnectionProvider.close();
103 LOGGER.debug("\n ending test -------------------------------");
107 * Library integration and communication test with handshake
111 public void testHandshake() throws Exception {
112 setUp(TransportProtocol.TCP);
113 final int amountOfCLients = 1;
114 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
115 final ScenarioHandler handler = new ScenarioHandler(scenario);
116 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
117 final OFClient firstClient = clients.get(0);
118 firstClient.getScenarioDone().get();
121 LOGGER.debug("testHandshake() Finished") ;
125 * Library integration and secured communication test with handshake
129 public void testTlsHandshake() throws Exception {
130 setUp(TransportProtocol.TLS);
131 final int amountOfCLients = 1;
132 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
133 final ScenarioHandler handler = new ScenarioHandler(scenario);
134 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
135 final OFClient firstClient = clients.get(0);
136 firstClient.getScenarioDone().get();
139 LOGGER.debug("testTlsHandshake() Finished") ;
143 * Library integration and communication test with handshake + echo exchange
147 public void testHandshakeAndEcho() throws Exception {
148 setUp(TransportProtocol.TCP);
149 final int amountOfCLients = 1;
150 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
151 scenario.addFirst(new SleepEvent(1000));
152 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
153 scenario.addFirst(new SleepEvent(1000));
154 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
155 final ScenarioHandler handler = new ScenarioHandler(scenario);
156 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TCP, ClientType.SIMPLE);
157 final OFClient firstClient = clients.get(0);
158 firstClient.getScenarioDone().get();
160 LOGGER.debug("testHandshakeAndEcho() Finished") ;
164 * Library integration and secured communication test with handshake + echo exchange
168 public void testTlsHandshakeAndEcho() throws Exception {
169 setUp(TransportProtocol.TLS);
170 final int amountOfCLients = 1;
171 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
172 scenario.addFirst(new SleepEvent(1000));
173 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
174 scenario.addFirst(new SleepEvent(1000));
175 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
176 final ScenarioHandler handler = new ScenarioHandler(scenario);
177 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.TLS, ClientType.SIMPLE);
178 final OFClient firstClient = clients.get(0);
179 firstClient.getScenarioDone().get();
181 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
185 * Library udp integration and communication test with handshake + echo exchange
189 public void testUdpHandshakeAndEcho() throws Exception {
190 setUp(TransportProtocol.UDP);
191 final int amountOfCLients = 1;
192 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
193 scenario.addFirst(new SleepEvent(1000));
194 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
195 scenario.addFirst(new SleepEvent(1000));
196 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
197 final ScenarioHandler handler = new ScenarioHandler(scenario);
198 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler, TransportProtocol.UDP, ClientType.SIMPLE);
199 final OFClient firstClient = clients.get(0);
200 firstClient.getScenarioDone().get();
202 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
206 * Library integration and communication test (with virtual machine)
210 public void testCommunicationWithVM() throws Exception {
211 mockPlugin.getFinishedFuture().get();
215 * @param amountOfCLients
216 * @param protocol true if encrypted connection should be used
217 * @return new clients up and running
218 * @throws ExecutionException if some client could not start
220 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
221 final TransportProtocol protocol, final ClientType clientType) throws ExecutionException {
222 final List<OFClient> clientsHorde = new ArrayList<>();
223 for (int i = 0; i < amountOfCLients; i++) {
224 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
226 if (clientType == ClientType.SIMPLE) {
227 if (protocol.equals(TransportProtocol.TCP)) {
228 sc = new SimpleClient(startupAddress.getHostAddress(), port);
229 sc.setSecuredClient(false);
230 } else if (protocol.equals(TransportProtocol.TLS)) {
231 sc = new SimpleClient(startupAddress.getHostAddress(), port);
232 sc.setSecuredClient(true);
234 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
236 } else if (clientType == ClientType.LISTENING) {
237 sc = new ListeningSimpleClient(0);
238 sc.setScenarioHandler(scenarioHandler);
239 sc.setSecuredClient(false);
241 LOGGER.error("Unknown type of client.");
242 throw new IllegalStateException("Unknown type of client.");
245 sc.setScenarioHandler(scenarioHandler);
246 clientsHorde.add(sc);
251 for (final OFClient sc : clientsHorde) {
253 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
254 } catch (final Exception e) {
255 LOGGER.error("createAndStartClient: Something borked ... ", e.getMessage(), e);
256 throw new ExecutionException(e);
266 public void testInitiateConnection() throws Exception {
267 setUp(TransportProtocol.TCP);
269 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
270 final ScenarioHandler handler = new ScenarioHandler(scenario);
271 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
272 final OFClient ofClient = clients.get(0);
273 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
274 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
275 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
276 ofClient.getScenarioDone().get();
277 LOGGER.debug("testInitiateConnection() Finished") ;