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.infrautils.diagstatus.DiagStatusService;
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(Mockito.mock(DiagStatusService.class), connConfig);
103 switchConnectionProvider.setSwitchConnectionHandler(mockPlugin);
104 switchConnectionProvider.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
105 if (protocol.equals(TransportProtocol.TCP) || protocol.equals(TransportProtocol.TLS)) {
106 final TcpHandler tcpHandler = (TcpHandler) switchConnectionProvider.getServerFacade();
107 port = tcpHandler.getPort();
109 final UdpHandler udpHandler = (UdpHandler) switchConnectionProvider.getServerFacade();
110 port = udpHandler.getPort();
115 public void tearDown() {
116 switchConnectionProvider.close();
117 LOGGER.debug("\n ending test -------------------------------");
121 * Library integration and communication test with handshake.
124 public void testHandshake() throws Exception {
125 setUp(TransportProtocol.TCP);
126 final int amountOfCLients = 1;
127 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
128 final ScenarioHandler handler = new ScenarioHandler(scenario);
129 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
130 TransportProtocol.TCP, ClientType.SIMPLE);
131 final OFClient firstClient = clients.get(0);
132 firstClient.getScenarioDone().get();
135 LOGGER.debug("testHandshake() Finished") ;
139 * Library integration and secured communication test with handshake.
142 public void testTlsHandshake() throws Exception {
143 setUp(TransportProtocol.TLS);
144 final int amountOfCLients = 1;
145 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
146 final ScenarioHandler handler = new ScenarioHandler(scenario);
147 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
148 TransportProtocol.TLS, ClientType.SIMPLE);
149 final OFClient firstClient = clients.get(0);
150 firstClient.getScenarioDone().get();
153 LOGGER.debug("testTlsHandshake() Finished") ;
157 * Library integration and communication test with handshake + echo exchange.
160 public void testHandshakeAndEcho() throws Exception {
161 setUp(TransportProtocol.TCP);
162 final int amountOfCLients = 1;
163 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
164 scenario.addFirst(new SleepEvent(1000));
165 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
166 scenario.addFirst(new SleepEvent(1000));
167 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
168 final ScenarioHandler handler = new ScenarioHandler(scenario);
169 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
170 TransportProtocol.TCP, ClientType.SIMPLE);
171 final OFClient firstClient = clients.get(0);
172 firstClient.getScenarioDone().get();
174 LOGGER.debug("testHandshakeAndEcho() Finished") ;
178 * Library integration and secured communication test with handshake + echo exchange.
181 public void testTlsHandshakeAndEcho() throws Exception {
182 setUp(TransportProtocol.TLS);
183 final int amountOfCLients = 1;
184 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
185 scenario.addFirst(new SleepEvent(1000));
186 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
187 scenario.addFirst(new SleepEvent(1000));
188 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
189 final ScenarioHandler handler = new ScenarioHandler(scenario);
190 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
191 TransportProtocol.TLS, ClientType.SIMPLE);
192 final OFClient firstClient = clients.get(0);
193 firstClient.getScenarioDone().get();
195 LOGGER.debug("testTlsHandshakeAndEcho() Finished") ;
199 * Library udp integration and communication test with handshake + echo exchange.
202 public void testUdpHandshakeAndEcho() throws Exception {
203 setUp(TransportProtocol.UDP);
204 final int amountOfCLients = 1;
205 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
206 scenario.addFirst(new SleepEvent(1000));
207 scenario.addFirst(new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
208 scenario.addFirst(new SleepEvent(1000));
209 scenario.addFirst(new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
210 final ScenarioHandler handler = new ScenarioHandler(scenario);
211 final List<OFClient> clients = createAndStartClient(amountOfCLients, handler,
212 TransportProtocol.UDP, ClientType.SIMPLE);
213 final OFClient firstClient = clients.get(0);
214 firstClient.getScenarioDone().get();
216 LOGGER.debug("testUdpHandshakeAndEcho() Finished") ;
220 * Library integration and communication test (with virtual machine).
223 public void testCommunicationWithVM() throws Exception {
224 mockPlugin.getFinishedFuture().get();
228 * Creates and start a client.
230 * @param amountOfCLients number of clients
231 * @param protocol true if encrypted connection should be used
232 * @return new clients up and running
233 * @throws ExecutionException if some client could not start
235 private List<OFClient> createAndStartClient(final int amountOfCLients, final ScenarioHandler scenarioHandler,
236 final TransportProtocol protocol, final ClientType clientType)
237 throws ExecutionException, InterruptedException, TimeoutException {
238 final List<OFClient> clientsHorde = new ArrayList<>();
239 for (int i = 0; i < amountOfCLients; i++) {
240 LOGGER.debug("startup address in createclient: {}", startupAddress.getHostAddress());
242 if (clientType == ClientType.SIMPLE) {
243 if (protocol.equals(TransportProtocol.TCP)) {
244 sc = new SimpleClient(startupAddress.getHostAddress(), port);
245 sc.setSecuredClient(false);
246 } else if (protocol.equals(TransportProtocol.TLS)) {
247 sc = new SimpleClient(startupAddress.getHostAddress(), port);
248 sc.setSecuredClient(true);
250 sc = new UdpSimpleClient(startupAddress.getHostAddress(), port);
252 } else if (clientType == ClientType.LISTENING) {
253 sc = new ListeningSimpleClient(0);
254 sc.setScenarioHandler(scenarioHandler);
255 sc.setSecuredClient(false);
257 LOGGER.error("Unknown type of client.");
258 throw new IllegalStateException("Unknown type of client.");
261 sc.setScenarioHandler(scenarioHandler);
262 clientsHorde.add(sc);
264 thread = new Thread(sc);
267 for (final OFClient sc : clientsHorde) {
268 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
274 public void testInitiateConnection() throws Exception {
275 setUp(TransportProtocol.TCP);
277 final Deque<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
278 final ScenarioHandler handler = new ScenarioHandler(scenario);
279 final List<OFClient> clients = createAndStartClient(1, handler, TransportProtocol.TCP, ClientType.LISTENING);
280 final OFClient ofClient = clients.get(0);
281 ofClient.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
282 final int listeningClientPort = ((ListeningSimpleClient) ofClient).getPort();
283 mockPlugin.initiateConnection(switchConnectionProvider, "localhost", listeningClientPort);
284 ofClient.getScenarioDone().get();
285 LOGGER.debug("testInitiateConnection() Finished") ;