OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / UdpHandlerTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.io.IOException;
12 import java.net.InetAddress;
13 import java.util.concurrent.ExecutionException;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.TimeoutException;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.openflowjava.protocol.impl.core.UdpChannelInitializer;
22 import org.opendaylight.openflowjava.protocol.impl.core.UdpHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Unit tests for UdpHandler.
28  *
29  * @author madamjak
30  */
31 public class UdpHandlerTest {
32
33     private static final Logger LOG = LoggerFactory.getLogger(UdpHandlerTest.class);
34
35     @Mock
36     private UdpChannelInitializer udpChannelInitializerMock;
37     private UdpHandler udpHandler;
38
39     /**
40      * Mock init.
41      */
42     @Before
43     public void startUp() {
44         MockitoAnnotations.initMocks(this);
45     }
46
47     /**
48      * Test to create UdpHandler with empty address and zero port.
49      */
50     @Test
51     public void testWithEmptyAddress() throws Exception {
52         udpHandler = new UdpHandler(null, 0);
53         udpHandler.setChannelInitializer(udpChannelInitializerMock);
54         Assert.assertTrue("Wrong - start server", startupServer(false));
55         try {
56             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500, TimeUnit.MILLISECONDS));
57         } catch (TimeoutException e) {
58             Assert.fail("Wrong - getIsOnlineFuture timed out");
59         }
60         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
61         shutdownServer();
62     }
63
64     /**
65      * Test to create UdpHandler with empty address and zero port on Epoll native transport.
66      */
67     @Test
68     public void testWithEmptyAddressOnEpoll() throws Exception {
69         udpHandler = new UdpHandler(null, 0);
70         udpHandler.setChannelInitializer(udpChannelInitializerMock);
71         Assert.assertTrue("Wrong - start server", startupServer(true));
72         try {
73             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
74         } catch (TimeoutException e) {
75             Assert.fail("Wrong - getIsOnlineFuture timed out");
76         }
77         Assert.assertFalse("Wrong - port has been set to zero", udpHandler.getPort() == 0);
78         shutdownServer();
79     }
80
81     /**
82      * Test to create UdpHandler with fill address and given port.
83      */
84     @Test
85     public void testWithAddressAndPort() throws Exception {
86         int port = 9874;
87         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
88         udpHandler.setChannelInitializer(udpChannelInitializerMock);
89         Assert.assertTrue("Wrong - start server", startupServer(false));
90         try {
91             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
92         } catch (TimeoutException e) {
93             Assert.fail("Wrong - getIsOnlineFuture timed out");
94         }
95         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
96         shutdownServer();
97     }
98
99     /**
100      * Test to create UdpHandler with fill address and given port on Epoll native transport.
101      */
102     @Test
103     public void testWithAddressAndPortOnEpoll() throws Exception {
104         int port = 9874;
105         udpHandler = new UdpHandler(InetAddress.getLocalHost(), port);
106         udpHandler.setChannelInitializer(udpChannelInitializerMock);
107         Assert.assertTrue("Wrong - start server", startupServer(true));
108         try {
109             Assert.assertTrue(udpHandler.getIsOnlineFuture().get(1500,TimeUnit.MILLISECONDS));
110         } catch (TimeoutException e) {
111             Assert.fail("Wrong - getIsOnlineFuture timed out");
112         }
113         Assert.assertEquals("Wrong - bad port number has been set", port, udpHandler.getPort());
114         shutdownServer();
115     }
116
117     private Boolean startupServer(final boolean isEpollEnabled)
118             throws InterruptedException, IOException, ExecutionException {
119         ListenableFuture<Boolean> online = udpHandler.getIsOnlineFuture();
120         /**
121          * Test EPoll based native transport if isEpollEnabled is true.
122          * Else use Nio based transport.
123          */
124         udpHandler.initiateEventLoopGroups(null, isEpollEnabled);
125         new Thread(udpHandler).start();
126
127         try {
128             online.get(10, TimeUnit.SECONDS);
129         } catch (TimeoutException e) {
130             LOG.warn("Timeout while waiting for UDP handler to start", e);
131         }
132
133         return online.isDone();
134     }
135
136     private void shutdownServer() throws InterruptedException, ExecutionException, TimeoutException {
137         ListenableFuture<Boolean> shutdownRet = udpHandler.shutdown() ;
138         final Boolean shutdownSucceeded = shutdownRet.get(10, TimeUnit.SECONDS);
139         Assert.assertTrue("Wrong - shutdown failed", shutdownSucceeded);
140     }
141 }