2a7a89768c7cfc9ce4bcc937c6cd2da3b610976c
[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 TestingConnConfigImpl 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 TestingConnConfigImpl(startupAddress, 0, DEFAULT_TLS_SUPPORT, SWITCH_IDLE_TIMEOUT);
64         scpimpl.setConfiguration(configs);
65         scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
66         TcpHandler server = (TcpHandler) scpimpl.getServerFacade();
67         port = server.getPort();
68     }
69
70     /**
71      * @throws Exception
72      */
73     @After
74     public void tearDown() throws Exception {
75         Thread.sleep(500);
76     }
77
78     /**
79      * Library integration and communication test with handshake
80      * @throws Exception 
81      */
82     @Test
83     public void testHandshake() throws Exception {
84         int amountOfCLients = 1;
85         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
86         ScenarioHandler handler = new ScenarioHandler(scenario);
87         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
88         SimpleClient firstClient = clients.get(0);
89         firstClient.getScenarioDone().get();
90         mockPlugin.shutdown();
91         mockPlugin.getFinishedFuture().get();
92     }
93
94     /**
95      * Library integration and communication test with handshake + echo exchange
96      * @throws Exception 
97      */
98     @Test
99     public void testHandshakeAndEcho() throws Exception {
100         int amountOfCLients = 1;
101         Stack<ClientEvent> scenario = ScenarioFactory.createHandshakeScenario();
102         scenario.add(0, new SleepEvent(100));
103         scenario.add(0, new SendEvent(ByteBufUtils.hexStringToBytes("04 02 00 08 00 00 00 04")));
104         scenario.add(0, new SleepEvent(100));
105         scenario.add(0, new WaitForMessageEvent(ByteBufUtils.hexStringToBytes("04 03 00 08 00 00 00 04")));
106         ScenarioHandler handler = new ScenarioHandler(scenario);
107         List<SimpleClient> clients = createAndStartClient(amountOfCLients, handler);
108         SimpleClient firstClient = clients.get(0);
109         firstClient.getScenarioDone().get();
110         mockPlugin.shutdown();
111         mockPlugin.getFinishedFuture().get();
112     }
113
114     /**
115      * Library integration and communication test (with virtual machine)
116      * @throws Exception
117      */
118     //@Test
119     public void testCommunicationWithVM() throws Exception {
120         mockPlugin.getFinishedFuture().get();
121     }
122     
123     /**
124      * @param amountOfCLients 
125      * @return new clients up and running
126      * @throws ExecutionException if some client could not start
127      */
128     private List<SimpleClient> createAndStartClient(int amountOfCLients, ScenarioHandler scenarioHandler)
129             throws ExecutionException {
130         List<SimpleClient> clientsHorde = new ArrayList<>();
131         for (int i = 0; i < amountOfCLients; i++) {
132             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());
133             SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), port);
134             sc.setSecuredClient(false);
135             sc.setScenarioHandler(scenarioHandler);
136             clientsHorde.add(sc);
137             sc.start();
138         }
139         for (SimpleClient sc : clientsHorde) {
140             try {
141                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
142             } catch (Exception e) {
143                 LOGGER.error(e.getMessage(), e);
144                 throw new ExecutionException(e);
145             }
146         }
147         return clientsHorde;
148     }
149
150 }