1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.integration;
\r
4 import java.net.InetAddress;
\r
5 import java.util.ArrayList;
\r
6 import java.util.List;
\r
7 import java.util.Stack;
\r
8 import java.util.concurrent.ExecutionException;
\r
9 import java.util.concurrent.TimeUnit;
\r
11 import org.junit.After;
\r
12 import org.junit.Before;
\r
13 import org.junit.Test;
\r
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
\r
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
\r
16 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
\r
17 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
\r
18 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
\r
19 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
\r
20 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
\r
21 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
\r
22 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
\r
23 import org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl;
\r
24 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
\r
25 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
\r
26 import org.slf4j.Logger;
\r
27 import org.slf4j.LoggerFactory;
\r
30 * @author michal.polkorab
\r
33 public class IntegrationTest {
\r
35 /** Name of file in which OpenFLow protocol messages are stored in binary format */
\r
36 private static int port;
\r
37 private static final FEATURE_SUPPORT DEFAULT_TLS_SUPPORT = FEATURE_SUPPORT.NOT_SUPPORTED;
\r
38 private static final int SWITCH_IDLE_TIMEOUT = 2000;
\r
40 protected static final Logger LOGGER = LoggerFactory
\r
41 .getLogger(IntegrationTest.class);
\r
43 private static final long CONNECTION_TIMEOUT = 2000;
\r
44 private InetAddress startupAddress;
\r
45 private MockPlugin mockPlugin;
\r
46 private SwitchConnectionProviderImpl scpimpl;
\r
47 private List<ConnectionConfiguration> configs;
\r
53 public void setUp() throws Exception {
\r
54 LOGGER.debug("\n\nstarting test -------------------------------");
\r
55 startupAddress = InetAddress.getLocalHost();
\r
56 mockPlugin = new MockPlugin();
\r
57 scpimpl = new SwitchConnectionProviderImpl();
\r
58 scpimpl.setSwitchConnectionHandler(mockPlugin);
\r
59 configs = new ArrayList<>();
\r
60 configs.add(new TestingConnConfigImpl(startupAddress, 0, DEFAULT_TLS_SUPPORT, SWITCH_IDLE_TIMEOUT));
\r
61 scpimpl.configure(configs);
\r
62 scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
\r
63 TcpHandler server = (TcpHandler) scpimpl.getServerLot().iterator().next();
\r
64 port = server.getPort();
\r
68 public void tearDown() throws Exception {
\r
73 * Library integration and communication test
\r
74 * @throws Exception
\r
77 public void testHandshake() throws Exception {
\r
78 int amountOfCLients = 1;
\r
79 Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
\r
80 ScenarioHandler handler = new ScenarioHandler(scenario);
\r
81 List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
\r
82 SimpleClient firstClient = clients.get(0);
\r
83 firstClient.getScenarioDone().get();
\r
84 mockPlugin.shutdown();
\r
85 mockPlugin.getFinishedFuture().get();
\r
89 * Library integration and communication test
\r
90 * @throws Exception
\r
93 public void testHandshakeAndEcho() throws Exception {
\r
94 int amountOfCLients = 1;
\r
95 Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
\r
96 scenario.add(0, new SleepEvent(1500));
\r
97 scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
\r
98 scenario.add(0, new SleepEvent(1500));
\r
99 scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
\r
100 ScenarioHandler handler = new ScenarioHandler(scenario);
\r
101 List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
\r
102 SimpleClient firstClient = clients.get(0);
\r
103 firstClient.getScenarioDone().get();
\r
104 mockPlugin.shutdown();
\r
105 mockPlugin.getFinishedFuture().get();
\r
109 * Library integration and communication test (with virtual machine)
\r
110 * @throws Exception
\r
113 public void testCommunicationWithVM() throws Exception {
\r
114 mockPlugin.getFinishedFuture().get();
\r
118 * @param amountOfCLients
\r
119 * @return new clients up and running
\r
120 * @throws InterruptedException
\r
121 * @throws ExecutionException
\r
123 private List<SimpleClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler)
\r
124 throws InterruptedException, ExecutionException {
\r
125 List<SimpleClient> clientsHorde = new ArrayList<>();
\r
126 for (int i = 0; i < amountOfCLients; i++) {
\r
127 LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
\r
128 SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), port);
\r
129 sc.setSecuredClient(false);
\r
130 sc.setScenarioHandler(scenarioHandler);
\r
131 clientsHorde.add(sc);
\r
134 for (SimpleClient sc : clientsHorde) {
\r
136 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
\r
137 } catch (Exception e) {
\r
138 LOGGER.error(e.getMessage(), e);
\r
139 throw new ExecutionException(e);
\r
142 return clientsHorde;
\r