2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.core.connection;
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;
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;
42 * Unit tests for SwitchConnectionProviderImpl.
44 * @author michal.polkorab
46 @RunWith(MockitoJUnitRunner.class)
47 public class SwitchConnectionProviderImplTest {
49 @Mock SwitchConnectionHandler handler;
50 @Mock DiagStatusService diagStatus;
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;
60 public void before() {
61 doReturn(mock(ServiceRegistration.class)).when(diagStatus).register(any());
65 * Creates new {@link SwitchConnectionProvider} instance for each test.
66 * @param protocol communication protocol
68 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
70 if (protocol != null) {
71 createConfig(protocol);
73 provider = new SwitchConnectionProviderImpl(diagStatus, config);
76 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
77 InetAddress startupAddress = InetAddress.getLocalHost();
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"));
86 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
87 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
88 config.setTransferProtocol(protocol);
92 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
95 public void testStartup1() throws UnknownHostException {
97 final ListenableFuture<Boolean> future = provider.startup();
99 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
101 assertThat(cause, instanceOf(IllegalStateException.class));
102 assertEquals("Connection not configured", cause.getMessage());
106 * Tests provider startup - without configuration.
109 public void testStartup2() throws UnknownHostException {
111 provider.setSwitchConnectionHandler(handler);
112 final ListenableFuture<Boolean> future = provider.startup();
114 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
116 assertThat(cause, instanceOf(IllegalStateException.class));
117 assertEquals("Connection not configured", cause.getMessage());
121 * Tests provider startup - without {@link SwitchConnectionHandler}.
124 public void testStartup3() throws UnknownHostException {
125 startUp(TransportProtocol.TCP);
126 final ListenableFuture<Boolean> future = provider.startup();
128 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
130 assertThat(cause, instanceOf(IllegalStateException.class));
131 assertEquals("SwitchConnectionHandler is not set", cause.getMessage());
135 * Tests correct provider startup - over TCP.
138 public void testStartup4() throws Exception {
139 startUp(TransportProtocol.TCP);
140 provider.setSwitchConnectionHandler(handler);
142 assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
146 * Tests correct provider startup - over TLS.
149 public void testStartup5() throws Exception {
150 startUp(TransportProtocol.TLS);
151 provider.setSwitchConnectionHandler(handler);
153 assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
157 * Tests correct provider startup - over UDP.
160 public void testStartup6() throws Exception {
161 startUp(TransportProtocol.UDP);
162 provider.setSwitchConnectionHandler(handler);
164 assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
168 * Tests correct provider shutdown.
171 public void testShutdown() throws Exception {
172 startUp(TransportProtocol.TCP);
173 provider.setSwitchConnectionHandler(handler);
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));