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