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