add lib-plugin interaction implementations on library side
[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         new Thread(tcphandler).start();
49         tcphandler.getIsOnlineFuture().get();
50         port = tcphandler.getPort();
51         address = tcphandler.getAddress();
52     }
53     
54     /**
55      * stop {@link TcpHandler}
56      * @throws ExecutionException 
57      * @throws InterruptedException 
58      */
59     @After
60     public void tearDown() throws InterruptedException, ExecutionException {
61         tcphandler.shutdown().get();
62     }
63
64     /**
65      * Test of connections in {@link TcpHandler} - accepting connection of 1
66      * client
67      * @throws InterruptedException 
68      * @throws ExecutionException 
69      */
70     @Test
71     public void testConnectOneClient() throws InterruptedException, ExecutionException {
72         int amountOfCLients = 1;
73         createAndStartClient(amountOfCLients);
74         int actualConnections = tcphandler.getNumberOfConnections();
75         Assert.assertEquals(amountOfCLients, actualConnections);
76         PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
77         for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
78             Channel channel =  iterator.next();
79             try {
80                 channel.close().sync();
81             } catch (InterruptedException e) {
82                 LOGGER.error(e.getMessage(), e);
83             }
84         }
85         actualConnections = tcphandler.getNumberOfConnections();
86         Assert.assertEquals(0, actualConnections);
87     }
88     
89     /**
90      * Test of connections in {@link TcpHandler} - accepting connection of 10
91      * clients
92      * @throws InterruptedException 
93      * @throws ExecutionException 
94      */
95     @Test
96     public void testConnectTenClients() throws InterruptedException, ExecutionException {
97         int amountOfCLients = 10;
98         createAndStartClient(amountOfCLients);
99         int actualConnections = tcphandler.getNumberOfConnections();
100         Assert.assertEquals(amountOfCLients, actualConnections);
101         PublishingChannelInitializer channelInitializer = tcphandler.getChannelInitializer();
102         for (Iterator<Channel> iterator = channelInitializer.getConnectionIterator(); iterator.hasNext();) {
103             Channel channel =  iterator.next();
104             try {
105                 channel.close().sync();
106             } catch (InterruptedException e) {
107                 LOGGER.error(e.getMessage(), e);
108             }
109         }
110         actualConnections = tcphandler.getNumberOfConnections();
111         Assert.assertEquals(0, actualConnections);
112     }
113     
114     /**
115      * Test of disconnecting in {@link TcpHandler} - shutting down connection of 10
116      * clients
117      * @throws InterruptedException 
118      * @throws ExecutionException 
119      */
120     @Test
121     public void testDisconnectTenClients() throws InterruptedException, ExecutionException {
122         int amountOfCLients = 10;
123         List<SimpleClient> clients = createAndStartClient(amountOfCLients);
124         int actualConnections = tcphandler.getNumberOfConnections();
125         Assert.assertEquals(amountOfCLients, actualConnections);
126         
127         disconnectClients(clients);
128
129         actualConnections = tcphandler.getNumberOfConnections();
130         Assert.assertEquals(0, actualConnections);
131     }
132
133     /**
134      * @param amountOfCLients 
135      * @return new clients up and running
136      * @throws InterruptedException
137      * @throws ExecutionException
138      */
139     private List<SimpleClient> createAndStartClient(int amountOfCLients)
140             throws InterruptedException, ExecutionException {
141         List<SimpleClient> clientsHorde = new ArrayList<>();
142         for (int i = 0; i < amountOfCLients; i++) {
143             SimpleClient sc = new SimpleClient(address, port, getClass().getResourceAsStream(
144                     OF_BINARY_MESSAGE_INPUT_TXT));
145             sc.setSecuredClient(true);
146             clientsHorde.add(sc);
147             sc.start();
148         }
149         for (SimpleClient sc : clientsHorde) {
150             try {
151                 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
152             } catch (Exception e) {
153                 LOGGER.error(e.getMessage(), e);
154                 throw new ExecutionException(e);
155             }
156         }
157         return clientsHorde;
158     }
159     
160     /**
161      * Test of disconnecting in {@link TcpHandler} - shutting down connection of 1
162      * client
163      * @throws InterruptedException 
164      * @throws ExecutionException 
165      */
166     @Test
167     public void testDisconnectOneClient() throws InterruptedException, ExecutionException {
168         int amountOfCLients = 1;
169         List<SimpleClient> clients = createAndStartClient(amountOfCLients);
170         int actualConnections = tcphandler.getNumberOfConnections();
171         Assert.assertEquals(amountOfCLients, actualConnections);
172         disconnectClients(clients);
173         actualConnections = tcphandler.getNumberOfConnections();
174         Assert.assertEquals(0, actualConnections);
175     }
176
177     /**
178      * @param clients
179      * @throws InterruptedException 
180      */
181     private static void disconnectClients(List<SimpleClient> clients) throws InterruptedException {
182         List<Future<?>> disconnectFutureBag = new ArrayList<>();
183         for (SimpleClient simpleClient : clients) {
184             disconnectFutureBag.add(simpleClient.disconnect());
185         }
186         for (Future<?> toBeDisconnected : disconnectFutureBag) {
187             toBeDisconnected.sync();
188         }
189     }
190 }