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