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 java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.List;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.infrautils.diagstatus.DiagStatusService;
30 import org.opendaylight.infrautils.diagstatus.ServiceRegistration;
31 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
32 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
33 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
34 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
35 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
41 * Unit tests for SwitchConnectionProviderImpl.
43 * @author michal.polkorab
45 @RunWith(MockitoJUnitRunner.class)
46 public class SwitchConnectionProviderImplTest {
48 @Mock SwitchConnectionHandler handler;
49 @Mock DiagStatusService diagStatus;
51 private static final int SWITCH_IDLE_TIMEOUT = 2000;
52 private static final int WAIT_TIMEOUT = 2000;
53 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
54 private TlsConfiguration tlsConfiguration;
55 private SwitchConnectionProviderImpl provider;
56 private ConnectionConfigurationImpl config;
59 public void before() {
60 doReturn(mock(ServiceRegistration.class)).when(diagStatus).register(any());
64 * Creates new {@link SwitchConnectionProvider} instance for each test.
65 * @param protocol communication protocol
67 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
69 if (protocol != null) {
70 createConfig(protocol);
72 provider = new SwitchConnectionProviderImpl(diagStatus, config);
75 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
76 InetAddress startupAddress = InetAddress.getLocalHost();
78 tlsConfiguration = null;
79 if (protocol.equals(TransportProtocol.TLS)) {
80 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
81 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
82 "/selfSignedController", PathType.CLASSPATH,
83 List.of("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256"));
85 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
86 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
87 config.setTransferProtocol(protocol);
91 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
94 public void testStartup1() throws UnknownHostException {
96 final var future = provider.startup();
98 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
100 assertThat(cause, instanceOf(IllegalStateException.class));
101 assertEquals("Connection not configured", cause.getMessage());
105 * Tests provider startup - without configuration.
108 public void testStartup2() throws UnknownHostException {
110 provider.setSwitchConnectionHandler(handler);
111 final var future = provider.startup();
113 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
115 assertThat(cause, instanceOf(IllegalStateException.class));
116 assertEquals("Connection not configured", cause.getMessage());
120 * Tests provider startup - without {@link SwitchConnectionHandler}.
123 public void testStartup3() throws UnknownHostException {
124 startUp(TransportProtocol.TCP);
125 final var future = provider.startup();
127 final var cause = assertThrows(ExecutionException.class, () -> future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS))
129 assertThat(cause, instanceOf(IllegalStateException.class));
130 assertEquals("SwitchConnectionHandler is not set", cause.getMessage());
134 * Tests correct provider startup - over TCP.
137 public void testStartup4() throws Exception {
138 startUp(TransportProtocol.TCP);
139 provider.setSwitchConnectionHandler(handler);
141 provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
145 * Tests correct provider startup - over TLS.
148 public void testStartup5() throws Exception {
149 startUp(TransportProtocol.TLS);
150 provider.setSwitchConnectionHandler(handler);
152 provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
156 * Tests correct provider startup - over UDP.
159 public void testStartup6() throws Exception {
160 startUp(TransportProtocol.UDP);
161 provider.setSwitchConnectionHandler(handler);
163 provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
167 * Tests correct provider shutdown.
170 public void testShutdown() throws Exception {
171 startUp(TransportProtocol.TCP);
172 provider.setSwitchConnectionHandler(handler);
174 provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
175 assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));