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.infrautils.diagstatus.DiagStatusService;
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, Mockito.mock(DiagStatusService.class));
92 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
93 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
94 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
95 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
96 port = tcpHandler.getPort();
98 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
99 port = udpHandler.getPort();
104 public void tearDown() {
105 switchConnectionProvider.close();
106 LOGGER.debug("\n ending test -------------------------------");
110 * Library integration and communication test with handshake.
113 public void testHandshake() throws Exception {
114 setUp(TransportProtocol.TCP);
115 final int amountOfCLients = 1;
116 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
117 final ScenarioHandler handler = new ScenarioHandler(scenario);
118 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
119 TransportProtocol.TCP, ClientType.SIMPLE);
120 final OFClient firstClient = clients.get(0);
121 firstClient.getScenarioDone().get();
124 LOGGER.debug("testHandshake() Finished") ;
128 * Library integration and secured communication test with handshake.
131 public void testTlsHandshake() throws Exception {
132 setUp(TransportProtocol.TLS);
133 final int amountOfCLients = 1;
134 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
135 final ScenarioHandler handler = new ScenarioHandler(scenario);
136 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
137 TransportProtocol.TLS, ClientType.SIMPLE);
138 final OFClient firstClient = clients.get(0);
139 firstClient.getScenarioDone().get();
142 LOGGER.debug("testTlsHandshake() Finished") ;
146 * Library integration and communication test with handshake + echo exchange.
149 public void testHandshakeAndEcho() throws Exception {
150 setUp(TransportProtocol.TCP);
151 final int amountOfCLients = 1;
152 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
153 scenario.addFirst(new SleepEvent(1000));
154 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
155 scenario.addFirst(new SleepEvent(1000));
156 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
157 final ScenarioHandler handler = new ScenarioHandler(scenario);
158 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
159 TransportProtocol.TCP, ClientType.SIMPLE);
160 final OFClient firstClient = clients.get(0);
161 firstClient.getScenarioDone().get();
163 LOGGER.debug("testHandshakeAndEcho() Finished") ;
167 * Library integration and secured communication test with handshake + echo exchange.
170 public void testTlsHandshakeAndEcho() throws Exception {
171 setUp(TransportProtocol.TLS);
172 final int amountOfCLients = 1;
173 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
174 scenario.addFirst(new SleepEvent(1000));
175 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
176 scenario.addFirst(new SleepEvent(1000));
177 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
178 final ScenarioHandler handler = new ScenarioHandler(scenario);
179 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
180 TransportProtocol.TLS, ClientType.SIMPLE);
181 final OFClient firstClient = clients.get(0);
182 firstClient.getScenarioDone().get();
184 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
188 * Library udp integration and communication test with handshake + echo exchange.
191 public void testUdpHandshakeAndEcho() throws Exception {
192 setUp(TransportProtocol.UDP);
193 final int amountOfCLients = 1;
194 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
195 scenario.addFirst(new SleepEvent(1000));
196 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
197 scenario.addFirst(new SleepEvent(1000));
198 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
199 final ScenarioHandler handler = new ScenarioHandler(scenario);
200 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
201 TransportProtocol.UDP, ClientType.SIMPLE);
202 final OFClient firstClient = clients.get(0);
203 firstClient.getScenarioDone().get();
205 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
209 * Library integration and communication test (with virtual machine).
212 public void testCommunicationWithVM() throws Exception {
213 mockPlugin.getFinishedFuture().get();
217 * Creates and start a client.
219 * @param amountOfCLients number of clients
220 * @param protocol true if encrypted connection should be used
221 * @return new clients up and running
222 * @throws ExecutionException if some client could not start
224 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
225 final TransportProtocol protocol, final ClientType clientType)
226 throws ExecutionException, InterruptedException, TimeoutException {
227 final List<OFClient> clientsHorde = new ArrayList<>();
228 for (int i = 0; i < amountOfCLients; i++) {
229 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
231 if (clientType == ClientType.SIMPLE) {
232 if (protocol.equals(TransportProtocol.TCP)) {
233 sc = new SimpleClient(startupAddress.getHostAddress(), port);
234 sc.setSecuredClient(false);
235 } else if (protocol.equals(TransportProtocol.TLS)) {
236 sc = new SimpleClient(startupAddress.getHostAddress(), port);
237 sc.setSecuredClient(true);
239 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
241 } else if (clientType == ClientType.LISTENING) {
242 sc = new ListeningSimpleClient(0);
243 sc.setScenarioHandler(scenarioHandler);
244 sc.setSecuredClient(false);
246 LOGGER.error("Unknown type of client.");
247 throw new IllegalStateException("Unknown type of client.");
250 sc.setScenarioHandler(scenarioHandler);
251 clientsHorde.add(sc);
253 thread = new Thread(sc);
256 for (final OFClient sc : clientsHorde) {
257 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
263 public void testInitiateConnection() throws Exception {
264 setUp(TransportProtocol.TCP);
266 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
267 final ScenarioHandler handler = new ScenarioHandler(scenario);
268 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
269 final OFClient ofClient = clients.get(0);
270 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
271 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
272 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
273 ofClient.getScenarioDone().get();
274 LOGGER.debug("testInitiateConnection() Finished") ;