Merge "Remove redundant type specifiers"
[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.openflowjava.protocol.api.connection.OpenflowDiagStatusProvider;
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 OpenflowDiagStatusProvider openflowPluginDiagStatusProvider;
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
55     public void startUp(final TransportProtocol protocol) throws UnknownHostException {
56         MockitoAnnotations.initMocks(this);
57         config = null;
58         if (protocol != null) {
59             createConfig(protocol);
60         }
61         provider = new SwitchConnectionProviderImpl(config, openflowPluginDiagStatusProvider);
62     }
63
64     private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
65         InetAddress startupAddress = InetAddress.getLocalHost();
66
67         tlsConfiguration = null;
68         if (protocol.equals(TransportProtocol.TLS)) {
69             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
70                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
71                     "/selfSignedController", PathType.CLASSPATH,
72                     Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
73         }
74         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
75                 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
76         config.setTransferProtocol(protocol);
77     }
78
79     /**
80      * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
81      */
82     @Test
83     public void testStartup1() throws UnknownHostException {
84         startUp(null);
85         final ListenableFuture<Boolean> future = provider.startup();
86         try {
87             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
88         } catch (InterruptedException | ExecutionException | TimeoutException e) {
89             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
90         }
91     }
92
93     /**
94      * Tests provider startup - without configuration.
95      */
96     @Test
97     public void testStartup2() throws UnknownHostException {
98         startUp(null);
99         provider.setSwitchConnectionHandler(handler);
100         final ListenableFuture<Boolean> future = provider.startup();
101         try {
102             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
103         } catch (InterruptedException | ExecutionException | TimeoutException e) {
104             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
105         }
106     }
107
108     /**
109      * Tests provider startup - without {@link SwitchConnectionHandler}.
110      */
111     @Test
112     public void testStartup3() throws UnknownHostException {
113         startUp(TransportProtocol.TCP);
114         final ListenableFuture<Boolean> future = provider.startup();
115         try {
116             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
117         } catch (InterruptedException | ExecutionException | TimeoutException e) {
118             Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
119                     + " SwitchConnectionHandler is not set", e.getMessage());
120         }
121     }
122
123     /**
124      * Tests correct provider startup - over TCP.
125      */
126     @Test
127     public void testStartup4() throws UnknownHostException {
128         startUp(TransportProtocol.TCP);
129         provider.setSwitchConnectionHandler(handler);
130         try {
131             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
132         } catch (InterruptedException | ExecutionException | TimeoutException e) {
133             Assert.fail();
134         }
135     }
136
137     /**
138      * Tests correct provider startup - over TLS.
139      */
140     @Test
141     public void testStartup5() throws UnknownHostException {
142         startUp(TransportProtocol.TLS);
143         provider.setSwitchConnectionHandler(handler);
144         try {
145             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
146         } catch (InterruptedException | ExecutionException | TimeoutException e) {
147             Assert.fail();
148         }
149     }
150
151     /**
152      * Tests correct provider startup - over UDP.
153      */
154     @Test
155     public void testStartup6() throws UnknownHostException {
156         startUp(TransportProtocol.UDP);
157         provider.setSwitchConnectionHandler(handler);
158         try {
159             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
160         } catch (InterruptedException | ExecutionException | TimeoutException e) {
161             Assert.fail();
162         }
163     }
164
165     /**
166      * Tests correct provider shutdown.
167      */
168     @Test
169     public void testShutdown() throws UnknownHostException {
170         startUp(TransportProtocol.TCP);
171         provider.setSwitchConnectionHandler(handler);
172         try {
173             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
174             Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
175         } catch (InterruptedException | ExecutionException | TimeoutException e) {
176             LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);
177         }
178     }
179
180 }