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.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.infrautils.diagstatus.DiagStatusService;
23 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
24 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
25 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
26 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
27 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
31 import org.slf4j.LoggerFactory;
34 * Unit tests for SwitchConnectionProviderImpl.
36 * @author michal.polkorab
38 public class SwitchConnectionProviderImplTest {
40 @Mock SwitchConnectionHandler handler;
41 @Mock DiagStatusService diagStatusService;
43 private static final int SWITCH_IDLE_TIMEOUT = 2000;
44 private static final int WAIT_TIMEOUT = 2000;
45 private static final int CHANNEL_OUTBOUND_QUEUE_SIZE = 1024;
46 private TlsConfiguration tlsConfiguration;
47 private SwitchConnectionProviderImpl provider;
48 private ConnectionConfigurationImpl config;
51 * Creates new {@link SwitchConnectionProvider} instance for each test.
52 * @param protocol communication protocol
54 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
55 MockitoAnnotations.initMocks(this);
57 if (protocol != null) {
58 createConfig(protocol);
60 provider = new SwitchConnectionProviderImpl(config, diagStatusService);
63 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
64 InetAddress startupAddress = InetAddress.getLocalHost();
66 tlsConfiguration = null;
67 if (protocol.equals(TransportProtocol.TLS)) {
68 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
69 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
70 "/selfSignedController", PathType.CLASSPATH,
71 Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
73 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
74 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
75 config.setTransferProtocol(protocol);
79 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
82 public void testStartup1() throws UnknownHostException {
84 final ListenableFuture<Boolean> future = provider.startup();
86 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
87 } catch (InterruptedException | ExecutionException | TimeoutException e) {
88 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
93 * Tests provider startup - without configuration.
96 public void testStartup2() throws UnknownHostException {
98 provider.setSwitchConnectionHandler(handler);
99 final ListenableFuture<Boolean> future = provider.startup();
101 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
102 } catch (InterruptedException | ExecutionException | TimeoutException e) {
103 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
108 * Tests provider startup - without {@link SwitchConnectionHandler}.
111 public void testStartup3() throws UnknownHostException {
112 startUp(TransportProtocol.TCP);
113 final ListenableFuture<Boolean> future = provider.startup();
115 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
116 } catch (InterruptedException | ExecutionException | TimeoutException e) {
117 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
118 + " SwitchConnectionHandler is not set", e.getMessage());
123 * Tests correct provider startup - over TCP.
126 public void testStartup4() throws UnknownHostException {
127 startUp(TransportProtocol.TCP);
128 provider.setSwitchConnectionHandler(handler);
130 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
131 } catch (InterruptedException | ExecutionException | TimeoutException e) {
137 * Tests correct provider startup - over TLS.
140 public void testStartup5() throws UnknownHostException {
141 startUp(TransportProtocol.TLS);
142 provider.setSwitchConnectionHandler(handler);
144 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
145 } catch (InterruptedException | ExecutionException | TimeoutException e) {
151 * Tests correct provider startup - over UDP.
154 public void testStartup6() throws UnknownHostException {
155 startUp(TransportProtocol.UDP);
156 provider.setSwitchConnectionHandler(handler);
158 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
159 } catch (InterruptedException | ExecutionException | TimeoutException e) {
165 * Tests correct provider shutdown.
168 public void testShutdown() throws UnknownHostException {
169 startUp(TransportProtocol.TCP);
170 provider.setSwitchConnectionHandler(handler);
172 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
173 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
174 } catch (InterruptedException | ExecutionException | TimeoutException e) {
175 LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);