Merge "add basic lib - plugin communication"
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / openflow / lib / TCPHandlerTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.openflow.lib;
3
4 import io.netty.channel.Channel;
5 import io.netty.util.concurrent.Future;
6
7 import java.util.ArrayList;
8 import java.util.Iterator;
9 import java.util.List;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit;
12
13 import org.junit.After;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.openflow.lib.clients.SimpleClient;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * 
23  * @author michal.polkorab
24  */
25 public class TCPHandlerTest {
26
27     /** Name of file in which OpenFLow protocol messages are stored in binary format */
28     private static final String OF_BINARY_MESSAGE_INPUT_TXT = "OFBinaryMessageInput.txt";
29
30     protected static final Logger LOGGER = LoggerFactory
31             .getLogger(TCPHandlerTest.class);
32
33     private static final long CONNECTION_TIMEOUT = 2000;
34
35     protected int port;
36     protected String address;
37     protected TcpHandler tcphandler;
38
39     /**
40      * @throws InterruptedException
41      * @throws ExecutionException
42      */
43     @Before
44     public void setUp() throws InterruptedException, ExecutionException {
45         tcphandler = new TcpHandler(0);
46         tcphandler.start();
47         tcphandler.getIsOnlineFuture().get();
48         port = tcphandler.getPort();
49         address = tcphandler.getAddress();
50     }
51     
52     /**
53      * stop {@link TcpHandler}
54      */
55     @After
56     public void tearDown() {
57         tcphandler.shutdown();
58     }
59
60     /**
61      * Test of connections in {@link TcpHandler} - accepting connection of 1
62      * client
63      * @throws InterruptedException 
64      * @throws ExecutionException 
65      */
66     @Test
67     public void testConnectOneClient() throws InterruptedException, ExecutionException {
68         int amountOfCLients = 1;
69         createAndStartClient(amountOfCLients);
70         int actualConnections = tcphandler.getNumberOfConnections();
71         Assert.assertEquals(amountOfCLients, actualConnections);
72         PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
73         for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
74             Channel channel =  iterator.next();
75             try {
76                 channel.close().sync();
77             } catch (InterruptedException e) {
78                 LOGGER.error(e.getMessage(), e);
79             }
80         }
81         actualConnections = tcphandler.getNumberOfConnections();
82         Assert.assertEquals(0, actualConnections);
83     }
84     
85     /**
86      * Test of connections in {@link TcpHandler} - accepting connection of 10
87      * clients
88      * @throws InterruptedException 
89      * @throws ExecutionException 
90      */
91     @Test
92     public void testConnectTenClients() throws InterruptedException, ExecutionException {
93         int amountOfCLients = 10;
94         createAndStartClient(amountOfCLients);
95         int actualConnections = tcphandler.getNumberOfConnections();
96         Assert.assertEquals(amountOfCLients, actualConnections);
97         PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
98         for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
99             Channel channel =  iterator.next();
100             try {
101                 channel.close().sync();
102             } catch (InterruptedException e) {
103                 LOGGER.error(e.getMessage(), e);
104             }
105         }
106         actualConnections = tcphandler.getNumberOfConnections();
107         Assert.assertEquals(0, actualConnections);
108     }
109     
110     /**
111      * Test of disconnecting in {@link TcpHandler} - shutting down connection of 10
112      * clients
113      * @throws InterruptedException 
114      * @throws ExecutionException 
115      */
116     @Test
117     public void testDisconnectTenClients() throws InterruptedException, ExecutionException {
118         int amountOfCLients = 10;
119         List<SimpleClient> clients = createAndStartClient(amountOfCLients);
120         int actualConnections = tcphandler.getNumberOfConnections();
121         Assert.assertEquals(amountOfCLients, actualConnections);
122         
123         disconnectClients(clients);
124
125         actualConnections = tcphandler.getNumberOfConnections();
126         Assert.assertEquals(0, actualConnections);
127     }
128
129     /**
130      * @param amountOfCLients 
131      * @return new clients up and running
132      * @throws InterruptedException
133      * @throws ExecutionException
134      */
135     private List<SimpleClient> createAndStartClient(int amountOfCLients)
136             throws InterruptedException, ExecutionException {
137         List<SimpleClient> clientsHorde = new ArrayList<>();
138         for (int i = 0; i < amountOfCLients; i++) {
139             SimpleClient sc = new SimpleClient(address, port, getClass().getResourceAsStream(
140                     OF_BINARY_MESSAGE_INPUT_TXT));
141             sc.setSecuredClient(true);
142             clientsHorde.add(sc);
143             sc.start();
144         }
145         for (SimpleClient sc : clientsHorde) {
146             try {
147                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
148             } catch (Exception e) {
149                 LOGGER.error(e.getMessage(), e);
150                 throw new ExecutionException(e);
151             }
152         }
153         return clientsHorde;
154     }
155     
156     /**
157      * Test of disconnecting in {@link TcpHandler} - shutting down connection of 1
158      * client
159      * @throws InterruptedException 
160      * @throws ExecutionException 
161      */
162     @Test
163     public void testDisconnectOneClient() throws InterruptedException, ExecutionException {
164         int amountOfCLients = 1;
165         List<SimpleClient> clients = createAndStartClient(amountOfCLients);
166         int actualConnections = tcphandler.getNumberOfConnections();
167         Assert.assertEquals(amountOfCLients, actualConnections);
168         disconnectClients(clients);
169         actualConnections = tcphandler.getNumberOfConnections();
170         Assert.assertEquals(0, actualConnections);
171     }
172
173     /**
174      * @param clients
175      * @throws InterruptedException 
176      */
177     private static void disconnectClients(List<SimpleClient> clients) throws InterruptedException {
178         List<Future<?>> disconnectFutureBag = new ArrayList<>();
179         for (SimpleClient simpleClient : clients) {
180             disconnectFutureBag.add(simpleClient.disconnect());
181         }
182         for (Future<?> toBeDisconnected : disconnectFutureBag) {
183             toBeDisconnected.sync();
184         }
185     }
186 }