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
9 package org.opendaylight.openflowjava.protocol.impl.core.connection;
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;
35 * Unit tests for SwitchConnectionProviderImpl.
37 * @author michal.polkorab
39 @RunWith(MockitoJUnitRunner.class)
40 public class SwitchConnectionProviderImplTest {
42 @Mock SwitchConnectionHandler handler;
43 @Mock OpenflowDiagStatusProvider openflowPluginDiagStatusProvider;
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;
53 * Creates new {@link SwitchConnectionProvider} instance for each test.
54 * @param protocol communication protocol
57 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
59 if (protocol != null) {
60 createConfig(protocol);
62 provider = new SwitchConnectionProviderImpl(config, openflowPluginDiagStatusProvider);
65 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
66 InetAddress startupAddress = InetAddress.getLocalHost();
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"));
75 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
76 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
77 config.setTransferProtocol(protocol);
81 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
84 public void testStartup1() throws UnknownHostException {
86 final ListenableFuture<Boolean> future = provider.startup();
88 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
89 } catch (InterruptedException | ExecutionException | TimeoutException e) {
90 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
95 * Tests provider startup - without configuration.
98 public void testStartup2() throws UnknownHostException {
100 provider.setSwitchConnectionHandler(handler);
101 final ListenableFuture<Boolean> future = provider.startup();
103 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
104 } catch (InterruptedException | ExecutionException | TimeoutException e) {
105 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
110 * Tests provider startup - without {@link SwitchConnectionHandler}.
113 public void testStartup3() throws UnknownHostException {
114 startUp(TransportProtocol.TCP);
115 final ListenableFuture<Boolean> future = provider.startup();
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());
125 * Tests correct provider startup - over TCP.
128 public void testStartup4() throws UnknownHostException {
129 startUp(TransportProtocol.TCP);
130 provider.setSwitchConnectionHandler(handler);
132 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
133 } catch (InterruptedException | ExecutionException | TimeoutException e) {
139 * Tests correct provider startup - over TLS.
142 public void testStartup5() throws UnknownHostException {
143 startUp(TransportProtocol.TLS);
144 provider.setSwitchConnectionHandler(handler);
146 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
147 } catch (InterruptedException | ExecutionException | TimeoutException e) {
153 * Tests correct provider startup - over UDP.
156 public void testStartup6() throws UnknownHostException {
157 startUp(TransportProtocol.UDP);
158 provider.setSwitchConnectionHandler(handler);
160 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
161 } catch (InterruptedException | ExecutionException | TimeoutException e) {
167 * Tests correct provider shutdown.
170 public void testShutdown() throws UnknownHostException {
171 startUp(TransportProtocol.TCP);
172 provider.setSwitchConnectionHandler(handler);
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);