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