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);
48 new Thread(tcphandler).start();
49 tcphandler.getIsOnlineFuture().get();
50 port = tcphandler.getPort();
51 address = tcphandler.getAddress();
55 * stop {@link TcpHandler}
56 * @throws ExecutionException
57 * @throws InterruptedException
60 public void tearDown() throws InterruptedException, ExecutionException {
61 tcphandler.shutdown().get();
65 * Test of connections in {@link TcpHandler} - accepting connection of 1
67 * @throws InterruptedException
68 * @throws ExecutionException
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();
80 channel.close().sync();
81 } catch (InterruptedException e) {
82 LOGGER.error(e.getMessage(), e);
85 actualConnections = tcphandler.getNumberOfConnections();
86 Assert.assertEquals(0, actualConnections);
90 * Test of connections in {@link TcpHandler} - accepting connection of 10
92 * @throws InterruptedException
93 * @throws ExecutionException
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();
105 channel.close().sync();
106 } catch (InterruptedException e) {
107 LOGGER.error(e.getMessage(), e);
110 actualConnections = tcphandler.getNumberOfConnections();
111 Assert.assertEquals(0, actualConnections);
115 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 10
117 * @throws InterruptedException
118 * @throws ExecutionException
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);
127 disconnectClients(clients);
129 actualConnections = tcphandler.getNumberOfConnections();
130 Assert.assertEquals(0, actualConnections);
134 * @param amountOfCLients
135 * @return new clients up and running
136 * @throws InterruptedException
137 * @throws ExecutionException
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);
149 for (SimpleClient sc : clientsHorde) {
151 sc.getIsOnlineFuture().get(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS);
152 } catch (Exception e) {
153 LOGGER.error(e.getMessage(), e);
154 throw new ExecutionException(e);
161 * Test of disconnecting in {@link TcpHandler} - shutting down connection of 1
163 * @throws InterruptedException
164 * @throws ExecutionException
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);
179 * @throws InterruptedException
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());
186 for (Future<?> toBeDisconnected : disconnectFutureBag) {
187 toBeDisconnected.sync();