1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
4 import io.netty.channel.Channel;
5 import io.netty.util.concurrent.Future;
7 import java.util.ArrayList;
8 import java.util.Iterator;
10 import java.util.concurrent.ExecutionException;
11 import java.util.concurrent.TimeUnit;
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;
25 * @author michal.polkorab
27 public class TcpHandlerTest {
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";
32 protected static final Logger LOGGER = LoggerFactory
33 .getLogger(TcpHandlerTest.class);
35 private static final long CONNECTION_TIMEOUT = 2000;
38 protected String address;
39 protected TcpHandler tcphandler;
42 * @throws InterruptedException
43 * @throws ExecutionException
46 public void setUp() throws InterruptedException, ExecutionException {
47 tcphandler = new TcpHandler(0);
49 tcphandler.getIsOnlineFuture().get();
50 port = tcphandler.getPort();
51 address = tcphandler.getAddress();
55 * stop {@link TcpHandler}
58 public void tearDown() {
59 tcphandler.shutdown();
63 * Test of connections in {@link TcpHandler} - accepting connection of 1
65 * @throws InterruptedException
66 * @throws ExecutionException
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();
78 channel.close().sync();
79 } catch (InterruptedException e) {
80 LOGGER.error(e.getMessage(), e);
83 actualConnections = tcphandler.getNumberOfConnections();
84 Assert.assertEquals(0, actualConnections);
88 * Test of connections in {@link TcpHandler} - accepting connection of 10
90 * @throws InterruptedException
91 * @throws ExecutionException
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();
103 channel.close().sync();
104 } catch (InterruptedException e) {
105 LOGGER.error(e.getMessage(), e);
108 actualConnections = tcphandler.getNumberOfConnections();
109 Assert.assertEquals(0, actualConnections);
113 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 10
115 * @throws InterruptedException
116 * @throws ExecutionException
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);
125 disconnectClients(clients);
127 actualConnections = tcphandler.getNumberOfConnections();
128 Assert.assertEquals(0, actualConnections);
132 * @param amountOfCLients
133 * @return new clients up and running
134 * @throws InterruptedException
135 * @throws ExecutionException
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);
147 for (SimpleClient sc : clientsHorde) {
149 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
150 } catch (Exception e) {
151 LOGGER.error(e.getMessage(), e);
152 throw new ExecutionException(e);
159 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 1
161 * @throws InterruptedException
162 * @throws ExecutionException
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);
177 * @throws InterruptedException
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());
184 for (Future<?> toBeDisconnected : disconnectFutureBag) {
185 toBeDisconnected.sync();