51848fe36e154523d8168abd336304572c95a5b7
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / integration / IntegrationTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.integration;\r
3 \r
4 import java.net.InetAddress;\r
5 import java.net.UnknownHostException;\r
6 import java.util.ArrayList;\r
7 import java.util.List;\r
8 import java.util.concurrent.ExecutionException;\r
9 import java.util.concurrent.TimeUnit;\r
10 \r
11 import org.junit.Before;\r
12 import org.junit.Test;\r
13 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;\r
14 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration.FEATURE_SUPPORT;\r
15 import org.opendaylight.openflowjava.protocol.impl.clients.SimpleClient;\r
16 import org.opendaylight.openflowjava.protocol.impl.connection.SwitchConnectionProviderImpl;\r
17 import org.slf4j.Logger;\r
18 import org.slf4j.LoggerFactory;\r
19 \r
20 /**\r
21  * @author michal.polkorab\r
22  *\r
23  */\r
24 public class IntegrationTest {\r
25 \r
26     /** Name of file in which OpenFLow protocol messages are stored in binary format */\r
27     public static final String OF_BINARY_MESSAGE_INPUT_TXT = "OFBinaryMessageInput.txt";\r
28     private static final int DEFAULT_PORT = 6633;\r
29     private static final FEATURE_SUPPORT DEFAULT_TLS_SUPPORT = FEATURE_SUPPORT.NOT_SUPPORTED;\r
30     \r
31     protected static final Logger LOGGER = LoggerFactory\r
32             .getLogger(IntegrationTest.class);\r
33 \r
34     private static final long CONNECTION_TIMEOUT = 2000;\r
35 \r
36     private InetAddress startupAddress;\r
37     private MockPlugin mockPlugin;\r
38 \r
39     /**\r
40      * @throws UnknownHostException\r
41      */\r
42     @Before\r
43     public void setUp() throws UnknownHostException {\r
44         startupAddress = InetAddress.getLocalHost();\r
45     }\r
46     \r
47     /**\r
48      * Library integration and communication test\r
49      * @throws Exception \r
50      */\r
51     @Test\r
52     public void testCommunication() throws Exception {\r
53         mockPlugin = new MockPlugin();\r
54         SwitchConnectionProviderImpl scpimpl = new SwitchConnectionProviderImpl();\r
55         scpimpl.setSwitchConnectionHandler(mockPlugin);\r
56         List<ConnectionConfiguration> configs = new ArrayList<>();\r
57         configs.add(new TestingConnConfigImpl(startupAddress, DEFAULT_PORT, DEFAULT_TLS_SUPPORT));\r
58         scpimpl.configure(configs);\r
59         scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);\r
60 \r
61         int amountOfCLients = 1;\r
62         List<SimpleClient> clients = createAndStartClient(amountOfCLients);\r
63         SimpleClient firstClient = clients.get(0);\r
64         firstClient.getAutomatedPartDone().get();\r
65         mockPlugin.getFinishedFuture().get();\r
66     }\r
67 \r
68     /**\r
69      * Library integration and communication test (with virtual machine)\r
70      * @throws Exception\r
71      */\r
72     //@Test\r
73     public void testCommunicationWithVM() throws Exception {\r
74         mockPlugin = new MockPlugin();\r
75         SwitchConnectionProviderImpl scpimpl = new SwitchConnectionProviderImpl();\r
76         scpimpl.setSwitchConnectionHandler(mockPlugin);\r
77         List<ConnectionConfiguration> configs = new ArrayList<>();\r
78         configs.add(new TestingConnConfigImpl(startupAddress, DEFAULT_PORT, DEFAULT_TLS_SUPPORT));\r
79         scpimpl.configure(configs);\r
80         scpimpl.startup().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);\r
81         mockPlugin.getFinishedFuture().get();\r
82     }\r
83     \r
84     /**\r
85      * @param amountOfCLients \r
86      * @param dataLimit TODO\r
87      * @return new clients up and running\r
88      * @throws InterruptedException\r
89      * @throws ExecutionException\r
90      */\r
91     private List<SimpleClient> createAndStartClient(int amountOfCLients)\r
92             throws InterruptedException, ExecutionException {\r
93         List<SimpleClient> clientsHorde = new ArrayList<>();\r
94         for (int i = 0; i < amountOfCLients; i++) {\r
95             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());\r
96             SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), DEFAULT_PORT,\r
97                     getClass().getResourceAsStream(OF_BINARY_MESSAGE_INPUT_TXT));\r
98             sc.setSecuredClient(false);\r
99             clientsHorde.add(sc);\r
100             sc.start();\r
101         }\r
102         for (SimpleClient sc : clientsHorde) {\r
103             try {\r
104                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);\r
105             } catch (Exception e) {\r
106                 LOGGER.error(e.getMessage(), e);\r
107                 throw new ExecutionException(e);\r
108             }\r
109         }\r
110         return clientsHorde;\r
111     }\r
112     \r
113     /**\r
114      * @param clients\r
115      * @throws InterruptedException \r
116      */\r
117     private static void disconnectClients(List<SimpleClient> clients) throws InterruptedException {\r
118         List<io.netty.util.concurrent.Future<?>> disconnectFutureBag = new ArrayList<>();\r
119         for (SimpleClient simpleClient : clients) {\r
120             disconnectFutureBag.add(simpleClient.disconnect());\r
121         }\r
122         for (io.netty.util.concurrent.Future<?> toBeDisconnected : disconnectFutureBag) {\r
123             toBeDisconnected.sync();\r
124         }\r
125     }\r
126 \r
127 }\r