2627d61fbe760870b475a3f9cd74cc43dea0a641
[openflowjava.git] / openflow-protocol-it / src / test / java / org / opendaylight / openflowjava / protocol / impl / integration / IntegrationTest.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.integration;
10
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;
17
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
22 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;
23 import org.opendaylight.openflowjava.protocol.impl.clients.ClientEvent;
24 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioFactory;
25 import org.opendaylight.openflowjava.protocol.impl.clients.ScenarioHandler;
26 import org.opendaylight.openflowjava.protocol.impl.clients.SendEvent;
27 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;
28 import org.opendaylight.openflowjava.protocol.impl.clients.SleepEvent;
29 import org.opendaylight.openflowjava.protocol.impl.clients.WaitForMessageEvent;
30 import org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl;
31 import org.opendaylight.openflowjava.protocol.impl.core.TcpHandler;
32 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * @author michal.polkorab
38  * @author timotej.kubas
39  */
40 public class IntegrationTest {
41
42     private static final Logger LOGGER = LoggerFactory
43             .getLogger(IntegrationTest.class);
44     private static int port;
45     private static final FEATURE_SUPPORT DEFAULT_TLS_SUPPORT = FEATURE_SUPPORT.NOT_SUPPORTED;
46     private static final int SWITCH_IDLE_TIMEOUT = 2000;
47     private static final long CONNECTION_TIMEOUT = 2000;
48     private InetAddress startupAddress;
49     private MockPlugin mockPlugin;
50     private SwitchConnectionProviderImpl scpimpl;
51     private List<ConnectionConfiguration> configs;
52
53     /**
54      * @throws Exception
55      */
56     @Before
57     public void setUp() throws Exception {
58         LOGGER.debug("\n\nstarting test -------------------------------");
59         startupAddress = InetAddress.getLocalHost();
60         mockPlugin = new MockPlugin();
61         scpimpl = new SwitchConnectionProviderImpl();
62         scpimpl.setSwitchConnectionHandler(mockPlugin);
63         configs = new ArrayList<>();
64         configs.add(new TestingConnConfigImpl(startupAddress, 0, DEFAULT_TLS_SUPPORT, SWITCH_IDLE_TIMEOUT));
65         scpimpl.configure(configs);
66         scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
67         TcpHandler server = (TcpHandler) scpimpl.getServerLot().iterator().next();
68         port = server.getPort();
69     }
70
71     /**
72      * @throws Exception
73      */
74     @After
75     public void tearDown() throws Exception {
76         Thread.sleep(500);
77     }
78
79     /**
80      * Library integration and communication test with handshake
81      * @throws Exception 
82      */
83     @Test
84     public void testHandshake() throws Exception {
85         int amountOfCLients = 1;
86         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
87         ScenarioHandler handler = new ScenarioHandler(scenario);
88         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
89         SimpleClient firstClient = clients.get(0);
90         firstClient.getScenarioDone().get();
91         mockPlugin.shutdown();
92         mockPlugin.getFinishedFuture().get();
93     }
94
95     /**
96      * Library integration and communication test with handshake + echo exchange
97      * @throws Exception 
98      */
99     @Test
100     public void testHandshakeAndEcho() throws Exception {
101         int amountOfCLients = 1;
102         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
103         scenario.add(0, new SleepEvent(100));
104         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
105         scenario.add(0, new SleepEvent(100));
106         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
107         ScenarioHandler handler = new ScenarioHandler(scenario);
108         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
109         SimpleClient firstClient = clients.get(0);
110         firstClient.getScenarioDone().get();
111         mockPlugin.shutdown();
112         mockPlugin.getFinishedFuture().get();
113     }
114
115     /**
116      * Library integration and communication test (with virtual machine)
117      * @throws Exception
118      */
119     //@Test
120     public void testCommunicationWithVM() throws Exception {
121         mockPlugin.getFinishedFuture().get();
122     }
123     
124     /**
125      * @param amountOfCLients 
126      * @return new clients up and running
127      * @throws ExecutionException if some client could not start
128      */
129     private List<SimpleClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler)
130             throws ExecutionException {
131         List<SimpleClient> clientsHorde = new ArrayList<>();
132         for (int i = 0; i < amountOfCLients; i++) {
133             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
134             SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), port);
135             sc.setSecuredClient(false);
136             sc.setScenarioHandler(scenarioHandler);
137             clientsHorde.add(sc);
138             sc.start();
139         }
140         for (SimpleClient sc : clientsHorde) {
141             try {
142                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
143             } catch (Exception e) {
144                 LOGGER.error(e.getMessage(), e);
145                 throw new ExecutionException(e);
146             }
147         }
148         return clientsHorde;
149     }
150
151 }