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
8 package org.opendaylight.openflowjava.protocol.it.integration;
10 import java.net.InetAddress;
11 import java.util.ArrayList;
12 import java.util.Deque;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15 import java.util.concurrent.ExecutorService;
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.junit.runner.RunWith;
21 import org.mockito.ArgumentMatchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
26 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
27 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
28 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
29 import org.opendaylight.openflowjava.protocol.impl.clients.ListeningSimpleClient;
30 import org.opendaylight.openflowjava.protocol.impl.clients.OFClient;
31 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
32 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
33 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
34 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
35 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
36 import org.opendaylight.openflowjava.protocol.impl.clients.UdpSimpleClient;
37 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
38 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
39 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
40 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
41 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionConfigurationImpl;
42 import org.opendaylight.openflowjava.util.ByteBufUtils;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * End-to-end integration test.
52 * @author michal.polkorab
53 * @author timotej.kubas
55 @RunWith(MockitoJUnitRunner.class)
56 public class IntegrationTest {
58 private static final Logger LOGGER = LoggerFactory
59 .getLogger(IntegrationTest.class);
61 private static int port;
62 private TlsConfiguration tlsConfiguration;
63 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
64 private static final int SWITCH_IDLE_TIMEOUT = 2000;
65 private static final long CONNECTION_TIMEOUT = 2000;
66 private InetAddress startupAddress;
67 private MockPlugin mockPlugin;
68 private SwitchConnectionProviderImpl switchConnectionProvider;
69 private ConnectionConfigurationImpl connConfig;
71 private ExecutorService executorService;
73 private Thread thread;
75 private enum ClientType {
80 public void setUp(final TransportProtocol protocol) throws Exception {
81 LOGGER.debug("\n starting test -------------------------------");
82 Mockito.doAnswer(invocation -> {
83 ((Runnable)invocation.getArguments()[0]).run();
85 }).when(executorService).execute(ArgumentMatchers.any());
87 final String currentDir = System.getProperty("user.dir");
88 LOGGER.debug("Current dir using System: {}", currentDir);
89 startupAddress = InetAddress.getLocalHost();
90 tlsConfiguration = null;
91 if (protocol.equals(TransportProtocol.TLS)) {
92 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
93 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
94 "/selfSignedController", PathType.CLASSPATH,
97 connConfig = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration,
98 SWITCH_IDLE_TIMEOUT, true, false, CHANNEL_OUTBOUND_QUEUE_SIZE);
99 connConfig.setTransferProtocol(protocol);
100 mockPlugin = new MockPlugin(executorService);
102 switchConnectionProvider = new SwitchConnectionProviderImpl(connConfig,
103 Mockito.mock(OpenflowDiagStatusProvider.class));
104 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
105 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
106 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
107 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
108 port = tcpHandler.getPort();
110 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
111 port = udpHandler.getPort();
116 public void tearDown() {
117 switchConnectionProvider.close();
118 LOGGER.debug("\n ending test -------------------------------");
122 * Library integration and communication test with handshake.
125 public void testHandshake() throws Exception {
126 setUp(TransportProtocol.TCP);
127 final int amountOfCLients = 1;
128 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
129 final ScenarioHandler handler = new ScenarioHandler(scenario);
130 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
131 TransportProtocol.TCP, ClientType.SIMPLE);
132 final OFClient firstClient = clients.get(0);
133 firstClient.getScenarioDone().get();
136 LOGGER.debug("testHandshake() Finished") ;
140 * Library integration and secured communication test with handshake.
143 public void testTlsHandshake() throws Exception {
144 setUp(TransportProtocol.TLS);
145 final int amountOfCLients = 1;
146 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
147 final ScenarioHandler handler = new ScenarioHandler(scenario);
148 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
149 TransportProtocol.TLS, ClientType.SIMPLE);
150 final OFClient firstClient = clients.get(0);
151 firstClient.getScenarioDone().get();
154 LOGGER.debug("testTlsHandshake() Finished") ;
158 * Library integration and communication test with handshake + echo exchange.
161 public void testHandshakeAndEcho() throws Exception {
162 setUp(TransportProtocol.TCP);
163 final int amountOfCLients = 1;
164 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
165 scenario.addFirst(new SleepEvent(1000));
166 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
167 scenario.addFirst(new SleepEvent(1000));
168 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
169 final ScenarioHandler handler = new ScenarioHandler(scenario);
170 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
171 TransportProtocol.TCP, ClientType.SIMPLE);
172 final OFClient firstClient = clients.get(0);
173 firstClient.getScenarioDone().get();
175 LOGGER.debug("testHandshakeAndEcho() Finished") ;
179 * Library integration and secured communication test with handshake + echo exchange.
182 public void testTlsHandshakeAndEcho() throws Exception {
183 setUp(TransportProtocol.TLS);
184 final int amountOfCLients = 1;
185 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
186 scenario.addFirst(new SleepEvent(1000));
187 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
188 scenario.addFirst(new SleepEvent(1000));
189 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
190 final ScenarioHandler handler = new ScenarioHandler(scenario);
191 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
192 TransportProtocol.TLS, ClientType.SIMPLE);
193 final OFClient firstClient = clients.get(0);
194 firstClient.getScenarioDone().get();
196 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
200 * Library udp integration and communication test with handshake + echo exchange.
203 public void testUdpHandshakeAndEcho() throws Exception {
204 setUp(TransportProtocol.UDP);
205 final int amountOfCLients = 1;
206 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
207 scenario.addFirst(new SleepEvent(1000));
208 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
209 scenario.addFirst(new SleepEvent(1000));
210 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
211 final ScenarioHandler handler = new ScenarioHandler(scenario);
212 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
213 TransportProtocol.UDP, ClientType.SIMPLE);
214 final OFClient firstClient = clients.get(0);
215 firstClient.getScenarioDone().get();
217 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
221 * Library integration and communication test (with virtual machine).
224 public void testCommunicationWithVM() throws Exception {
225 mockPlugin.getFinishedFuture().get();
229 * Creates and start a client.
231 * @param amountOfCLients number of clients
232 * @param protocol true if encrypted connection should be used
233 * @return new clients up and running
234 * @throws ExecutionException if some client could not start
236 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
237 final TransportProtocol protocol, final ClientType clientType)
238 throws ExecutionException, InterruptedException, TimeoutException {
239 final List<OFClient> clientsHorde = new ArrayList<>();
240 for (int i = 0; i < amountOfCLients; i++) {
241 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
243 if (clientType == ClientType.SIMPLE) {
244 if (protocol.equals(TransportProtocol.TCP)) {
245 sc = new SimpleClient(startupAddress.getHostAddress(), port);
246 sc.setSecuredClient(false);
247 } else if (protocol.equals(TransportProtocol.TLS)) {
248 sc = new SimpleClient(startupAddress.getHostAddress(), port);
249 sc.setSecuredClient(true);
251 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
253 } else if (clientType == ClientType.LISTENING) {
254 sc = new ListeningSimpleClient(0);
255 sc.setScenarioHandler(scenarioHandler);
256 sc.setSecuredClient(false);
258 LOGGER.error("Unknown type of client.");
259 throw new IllegalStateException("Unknown type of client.");
262 sc.setScenarioHandler(scenarioHandler);
263 clientsHorde.add(sc);
265 thread = new Thread(sc);
268 for (final OFClient sc : clientsHorde) {
269 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
275 public void testInitiateConnection() throws Exception {
276 setUp(TransportProtocol.TCP);
278 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
279 final ScenarioHandler handler = new ScenarioHandler(scenario);
280 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
281 final OFClient ofClient = clients.get(0);
282 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
283 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
284 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
285 ofClient.getScenarioDone().get();
286 LOGGER.debug("testInitiateConnection() Finished") ;