Extensibility support (deserialization part)
[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.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;
34
35 /**
36  * @author michal.polkorab
37  * @author timotej.kubas
38  */
39 public class IntegrationTest {
40
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;
51
52     /**
53      * @throws Exception
54      */
55     @Before
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();
67     }
68
69     /**
70      * @throws Exception
71      */
72     @After
73     public void tearDown() throws Exception {
74         Thread.sleep(500);
75     }
76
77     /**
78      * Library integration and communication test with handshake
79      * @throws Exception 
80      */
81     @Test
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();
91     }
92
93     /**
94      * Library integration and communication test with handshake + echo exchange
95      * @throws Exception 
96      */
97     @Test
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();
111     }
112
113     /**
114      * Library integration and communication test (with virtual machine)
115      * @throws Exception
116      */
117     //@Test
118     public void testCommunicationWithVM() throws Exception {
119         mockPlugin.getFinishedFuture().get();
120     }
121     
122     /**
123      * @param amountOfCLients 
124      * @return new clients up and running
125      * @throws ExecutionException if some client could not start
126      */
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);
136             sc.start();
137         }
138         for (SimpleClient sc : clientsHorde) {
139             try {
140                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
141             } catch (Exception e) {
142                 LOGGER.error(e.getMessage(), e);
143                 throw new ExecutionException(e);
144             }
145         }
146         return clientsHorde;
147     }
148
149 }