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.impl.integration;
11 import java.net.InetAddress;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Stack;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
22 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
23 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
24 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
25 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
26 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
28 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
29 import org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl;
30 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
31 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * @author michal.polkorab
37 * @author timotej.kubas
39 public class IntegrationTest {
41 private static final Logger LOGGER = LoggerFactory
42 .getLogger(IntegrationTest.class);
43 private static int port;
44 private static final FEATURE_SUPPORT DEFAULT_TLS_SUPPORT = FEATURE_SUPPORT.NOT_SUPPORTED;
45 private static final int SWITCH_IDLE_TIMEOUT = 2000;
46 private static final long CONNECTION_TIMEOUT = 2000;
47 private InetAddress startupAddress;
48 private MockPlugin mockPlugin;
49 private SwitchConnectionProviderImpl scpimpl;
50 private TestingConnConfigImpl configs;
56 public void setUp() throws Exception {
57 LOGGER.debug("\n\nstarting test -------------------------------");
58 startupAddress = InetAddress.getLocalHost();
59 mockPlugin = new MockPlugin();
60 scpimpl = new SwitchConnectionProviderImpl();
61 scpimpl.setSwitchConnectionHandler(mockPlugin);
62 configs = new TestingConnConfigImpl(startupAddress, 0, DEFAULT_TLS_SUPPORT, SWITCH_IDLE_TIMEOUT);
63 scpimpl.setConfiguration(configs);
64 scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
65 TcpHandler server = (TcpHandler) scpimpl.getServerFacade();
66 port = server.getPort();
73 public void tearDown() throws Exception {
78 * Library integration and communication test with handshake
82 public void testHandshake() throws Exception {
83 int amountOfCLients = 1;
84 Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
85 ScenarioHandler handler = new ScenarioHandler(scenario);
86 List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
87 SimpleClient firstClient = clients.get(0);
88 firstClient.getScenarioDone().get();
89 mockPlugin.shutdown();
90 mockPlugin.getFinishedFuture().get();
94 * Library integration and communication test with handshake + echo exchange
98 public void testHandshakeAndEcho() throws Exception {
99 int amountOfCLients = 1;
100 Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
101 scenario.add(0, new SleepEvent(100));
102 scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
103 scenario.add(0, new SleepEvent(100));
104 scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
105 ScenarioHandler handler = new ScenarioHandler(scenario);
106 List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
107 SimpleClient firstClient = clients.get(0);
108 firstClient.getScenarioDone().get();
109 mockPlugin.shutdown();
110 mockPlugin.getFinishedFuture().get();
114 * Library integration and communication test (with virtual machine)
118 public void testCommunicationWithVM() throws Exception {
119 mockPlugin.getFinishedFuture().get();
123 * @param amountOfCLients
124 * @return new clients up and running
125 * @throws ExecutionException if some client could not start
127 private List<SimpleClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler)
128 throws ExecutionException {
129 List<SimpleClient> clientsHorde = new ArrayList<>();
130 for (int i = 0; i < amountOfCLients; i++) {
131 LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
132 SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), port);
133 sc.setSecuredClient(false);
134 sc.setScenarioHandler(scenarioHandler);
135 clientsHorde.add(sc);
138 for (SimpleClient sc : clientsHorde) {
140 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
141 } catch (Exception e) {
142 LOGGER.error(e.getMessage(), e);
143 throw new ExecutionException(e);