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 java.util.concurrent.TimeoutException;
18 import org.junit.After;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
22 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
24 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
25 import org.opendaylight.openflowjava.protocol.impl.clients.ListeningSimpleClient;
26 import org.opendaylight.openflowjava.protocol.impl.clients.OFClient;
27 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
28 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
29 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
30 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
31 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
32 import org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClient;
33 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
34 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
35 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
36 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
37 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionConfigurationImpl;
38 import org.opendaylight.openflowjava.util.ByteBufUtils;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * End-to-end integration test.
48 * @author michal.polkorab
49 * @author timotej.kubas
51 public class IntegrationTest {
53 private static final Logger LOGGER = LoggerFactory
54 .getLogger(IntegrationTest.class);
56 private static int port;
57 private TlsConfiguration tlsConfiguration;
58 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
59 private static final int SWITCH_IDLE_TIMEOUT = 2000;
60 private static final long CONNECTION_TIMEOUT = 2000;
61 private InetAddress startupAddress;
62 private MockPlugin mockPlugin;
63 private SwitchConnectionProviderImpl switchConnectionProvider;
64 private ConnectionConfigurationImpl connConfig;
66 private Thread thread;
68 private enum ClientType {
73 public void setUp(final TransportProtocol protocol) throws Exception {
74 LOGGER.debug("\n starting test -------------------------------");
76 final String currentDir = System.getProperty("user.dir");
77 LOGGER.debug("Current dir using System: {}", currentDir);
78 startupAddress = InetAddress.getLocalHost();
79 tlsConfiguration = null;
80 if (protocol.equals(TransportProtocol.TLS)) {
81 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
82 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
83 "/selfSignedController", PathType.CLASSPATH,
84 new ArrayList<String>());
86 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration,
87 SWITCH_IDLE_TIMEOUT, true, false, CHANNEL_OUTBOUND_QUEUE_SIZE);
88 connConfig.setTransferProtocol(protocol);
89 mockPlugin = new MockPlugin();
91 switchConnectionProvider = new SwitchConnectionProviderImpl(connConfig,
92 Mockito.mock(OpenflowDiagStatusProvider.class));
93 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
94 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
95 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
96 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
97 port = tcpHandler.getPort();
99 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
100 port = udpHandler.getPort();
105 public void tearDown() throws Exception {
106 switchConnectionProvider.close();
107 LOGGER.debug("\n ending test -------------------------------");
111 * Library integration and communication test with handshake.
114 public void testHandshake() throws Exception {
115 setUp(TransportProtocol.TCP);
116 final int amountOfCLients = 1;
117 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
118 final ScenarioHandler handler = new ScenarioHandler(scenario);
119 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
120 TransportProtocol.TCP, ClientType.SIMPLE);
121 final OFClient firstClient = clients.get(0);
122 firstClient.getScenarioDone().get();
125 LOGGER.debug("testHandshake() Finished") ;
129 * Library integration and secured communication test with handshake.
132 public void testTlsHandshake() throws Exception {
133 setUp(TransportProtocol.TLS);
134 final int amountOfCLients = 1;
135 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
136 final ScenarioHandler handler = new ScenarioHandler(scenario);
137 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
138 TransportProtocol.TLS, ClientType.SIMPLE);
139 final OFClient firstClient = clients.get(0);
140 firstClient.getScenarioDone().get();
143 LOGGER.debug("testTlsHandshake() Finished") ;
147 * Library integration and communication test with handshake + echo exchange.
150 public void testHandshakeAndEcho() throws Exception {
151 setUp(TransportProtocol.TCP);
152 final int amountOfCLients = 1;
153 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
154 scenario.addFirst(new SleepEvent(1000));
155 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
156 scenario.addFirst(new SleepEvent(1000));
157 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
158 final ScenarioHandler handler = new ScenarioHandler(scenario);
159 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
160 TransportProtocol.TCP, ClientType.SIMPLE);
161 final OFClient firstClient = clients.get(0);
162 firstClient.getScenarioDone().get();
164 LOGGER.debug("testHandshakeAndEcho() Finished") ;
168 * Library integration and secured communication test with handshake + echo exchange.
171 public void testTlsHandshakeAndEcho() throws Exception {
172 setUp(TransportProtocol.TLS);
173 final int amountOfCLients = 1;
174 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
175 scenario.addFirst(new SleepEvent(1000));
176 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
177 scenario.addFirst(new SleepEvent(1000));
178 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
179 final ScenarioHandler handler = new ScenarioHandler(scenario);
180 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
181 TransportProtocol.TLS, ClientType.SIMPLE);
182 final OFClient firstClient = clients.get(0);
183 firstClient.getScenarioDone().get();
185 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
189 * Library udp integration and communication test with handshake + echo exchange.
192 public void testUdpHandshakeAndEcho() throws Exception {
193 setUp(TransportProtocol.UDP);
194 final int amountOfCLients = 1;
195 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
196 scenario.addFirst(new SleepEvent(1000));
197 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
198 scenario.addFirst(new SleepEvent(1000));
199 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
200 final ScenarioHandler handler = new ScenarioHandler(scenario);
201 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
202 TransportProtocol.UDP, ClientType.SIMPLE);
203 final OFClient firstClient = clients.get(0);
204 firstClient.getScenarioDone().get();
206 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
210 * Library integration and communication test (with virtual machine).
213 public void testCommunicationWithVM() throws Exception {
214 mockPlugin.getFinishedFuture().get();
218 * Creates and start a client.
220 * @param amountOfCLients number of clients
221 * @param protocol true if encrypted connection should be used
222 * @return new clients up and running
223 * @throws ExecutionException if some client could not start
225 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
226 final TransportProtocol protocol, final ClientType clientType)
227 throws ExecutionException, InterruptedException, TimeoutException {
228 final List<OFClient> clientsHorde = new ArrayList<>();
229 for (int i = 0; i < amountOfCLients; i++) {
230 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
232 if (clientType == ClientType.SIMPLE) {
233 if (protocol.equals(TransportProtocol.TCP)) {
234 sc = new SimpleClient(startupAddress.getHostAddress(), port);
235 sc.setSecuredClient(false);
236 } else if (protocol.equals(TransportProtocol.TLS)) {
237 sc = new SimpleClient(startupAddress.getHostAddress(), port);
238 sc.setSecuredClient(true);
240 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
242 } else if (clientType == ClientType.LISTENING) {
243 sc = new ListeningSimpleClient(0);
244 sc.setScenarioHandler(scenarioHandler);
245 sc.setSecuredClient(false);
247 LOGGER.error("Unknown type of client.");
248 throw new IllegalStateException("Unknown type of client.");
251 sc.setScenarioHandler(scenarioHandler);
252 clientsHorde.add(sc);
254 thread = new Thread(sc);
257 for (final OFClient sc : clientsHorde) {
258 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
264 public void testInitiateConnection() throws Exception {
265 setUp(TransportProtocol.TCP);
267 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
268 final ScenarioHandler handler = new ScenarioHandler(scenario);
269 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
270 final OFClient ofClient = clients.get(0);
271 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
272 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
273 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
274 ofClient.getScenarioDone().get();
275 LOGGER.debug("testInitiateConnection() Finished") ;