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