report diagstatus from UdpHandler/TcpHandler on Netty thread terminate
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / connection / SwitchConnectionProviderImplTest.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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
10
11 import com.google.common.collect.Lists;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.TimeUnit;
17 import java.util.concurrent.TimeoutException;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
23 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
24 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
25 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
26 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
27 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Unit tests for SwitchConnectionProviderImpl.
35  *
36  * @author michal.polkorab
37  */
38 public class SwitchConnectionProviderImplTest {
39
40     @Mock SwitchConnectionHandler handler;
41     @Mock DiagStatusService diagStatusService;
42
43     private static final int SWITCH_IDLE_TIMEOUT = 2000;
44     private static final int WAIT_TIMEOUT = 2000;
45     private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
46     private TlsConfiguration tlsConfiguration;
47     private SwitchConnectionProviderImpl provider;
48     private ConnectionConfigurationImpl config;
49
50     /**
51      * Creates new {@link SwitchConnectionProvider} instance for each test.
52      * @param protocol communication protocol
53      */
54     public void startUp(final TransportProtocol protocol) throws UnknownHostException {
55         MockitoAnnotations.initMocks(this);
56         config = null;
57         if (protocol != null) {
58             createConfig(protocol);
59         }
60         provider = new SwitchConnectionProviderImpl(config, diagStatusService);
61     }
62
63     private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
64         InetAddress startupAddress = InetAddress.getLocalHost();
65
66         tlsConfiguration = null;
67         if (protocol.equals(TransportProtocol.TLS)) {
68             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
69                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
70                     "/selfSignedController", PathType.CLASSPATH,
71                     Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
72         }
73         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
74                 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
75         config.setTransferProtocol(protocol);
76     }
77
78     /**
79      * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
80      */
81     @Test
82     public void testStartup1() throws UnknownHostException {
83         startUp(null);
84         final ListenableFuture<Boolean> future = provider.startup();
85         try {
86             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
87         } catch (InterruptedException | ExecutionException | TimeoutException e) {
88             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
89         }
90     }
91
92     /**
93      * Tests provider startup - without configuration.
94      */
95     @Test
96     public void testStartup2() throws UnknownHostException {
97         startUp(null);
98         provider.setSwitchConnectionHandler(handler);
99         final ListenableFuture<Boolean> future = provider.startup();
100         try {
101             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
102         } catch (InterruptedException | ExecutionException | TimeoutException e) {
103             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
104         }
105     }
106
107     /**
108      * Tests provider startup - without {@link SwitchConnectionHandler}.
109      */
110     @Test
111     public void testStartup3() throws UnknownHostException {
112         startUp(TransportProtocol.TCP);
113         final ListenableFuture<Boolean> future = provider.startup();
114         try {
115             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
116         } catch (InterruptedException | ExecutionException | TimeoutException e) {
117             Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
118                     + " SwitchConnectionHandler is not set", e.getMessage());
119         }
120     }
121
122     /**
123      * Tests correct provider startup - over TCP.
124      */
125     @Test
126     public void testStartup4() throws UnknownHostException {
127         startUp(TransportProtocol.TCP);
128         provider.setSwitchConnectionHandler(handler);
129         try {
130             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
131         } catch (InterruptedException | ExecutionException | TimeoutException e) {
132             Assert.fail();
133         }
134     }
135
136     /**
137      * Tests correct provider startup - over TLS.
138      */
139     @Test
140     public void testStartup5() throws UnknownHostException {
141         startUp(TransportProtocol.TLS);
142         provider.setSwitchConnectionHandler(handler);
143         try {
144             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
145         } catch (InterruptedException | ExecutionException | TimeoutException e) {
146             Assert.fail();
147         }
148     }
149
150     /**
151      * Tests correct provider startup - over UDP.
152      */
153     @Test
154     public void testStartup6() throws UnknownHostException {
155         startUp(TransportProtocol.UDP);
156         provider.setSwitchConnectionHandler(handler);
157         try {
158             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
159         } catch (InterruptedException | ExecutionException | TimeoutException e) {
160             Assert.fail();
161         }
162     }
163
164     /**
165      * Tests correct provider shutdown.
166      */
167     @Test
168     public void testShutdown() throws UnknownHostException {
169         startUp(TransportProtocol.TCP);
170         provider.setSwitchConnectionHandler(handler);
171         try {
172             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
173             Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
174         } catch (InterruptedException | ExecutionException | TimeoutException e) {
175             LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);
176         }
177     }
178
179 }