5416a2a05e887ee60b1a7e14631c6c22f82119b7
[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, 24);\r
63         SimpleClient firstClient = clients.get(0);\r
64         firstClient.getAutomatedPartDone().get();\r
65         firstClient.getDataReceived().get();\r
66         disconnectClients(clients);\r
67     }\r
68     \r
69     /**\r
70      * @param amountOfCLients \r
71      * @param dataLimit TODO\r
72      * @return new clients up and running\r
73      * @throws InterruptedException\r
74      * @throws ExecutionException\r
75      */\r
76     private List<SimpleClient> createAndStartClient(int amountOfCLients, int dataLimit)\r
77             throws InterruptedException, ExecutionException {\r
78         List<SimpleClient> clientsHorde = new ArrayList<>();\r
79         for (int i = 0; i < amountOfCLients; i++) {\r
80             LOGGER.debug("startup address in createclient: " + startupAddress.getHostAddress());\r
81             SimpleClient sc = new SimpleClient(startupAddress.getHostAddress(), DEFAULT_PORT,\r
82                     getClass().getResourceAsStream(OF_BINARY_MESSAGE_INPUT_TXT));\r
83             sc.setSecuredClient(false);\r
84             clientsHorde.add(sc);\r
85             sc.setDataLimit(dataLimit);\r
86             sc.start();\r
87         }\r
88         for (SimpleClient sc : clientsHorde) {\r
89             try {\r
90                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);\r
91             } catch (Exception e) {\r
92                 LOGGER.error(e.getMessage(), e);\r
93                 throw new ExecutionException(e);\r
94             }\r
95         }\r
96         return clientsHorde;\r
97     }\r
98     \r
99     /**\r
100      * @param clients\r
101      * @throws InterruptedException \r
102      */\r
103     private static void disconnectClients(List<SimpleClient> clients) throws InterruptedException {\r
104         List<io.netty.util.concurrent.Future<?>> disconnectFutureBag = new ArrayList<>();\r
105         for (SimpleClient simpleClient : clients) {\r
106             disconnectFutureBag.add(simpleClient.disconnect());\r
107         }\r
108         for (io.netty.util.concurrent.Future<?> toBeDisconnected : disconnectFutureBag) {\r
109             toBeDisconnected.sync();\r
110         }\r
111     }\r
112 \r
113 }\r