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.openflowjava.protocol.api.connection.SwitchConnectionHandler;
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
24 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
25 import org.opendaylight.openflowjava.protocol.impl.core.SwitchConnectionProviderImpl;
26 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;
30 import org.slf4j.LoggerFactory;
33 * Unit tests for SwitchConnectionProviderImpl.
35 * @author michal.polkorab
37 public class SwitchConnectionProviderImplTest {
39 @Mock SwitchConnectionHandler handler;
41 private static final int SWITCH_IDLE_TIMEOUT = 2000;
42 private static final int WAIT_TIMEOUT = 2000;
43 private TlsConfiguration tlsConfiguration;
44 private SwitchConnectionProviderImpl provider;
45 private ConnectionConfigurationImpl config;
48 * Creates new {@link SwitchConnectionProvider} instance for each test.
49 * @param protocol communication protocol
51 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
52 MockitoAnnotations.initMocks(this);
54 if (protocol != null) {
55 createConfig(protocol);
57 provider = new SwitchConnectionProviderImpl(config);
60 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
61 InetAddress startupAddress = InetAddress.getLocalHost();
63 tlsConfiguration = null;
64 if (protocol.equals(TransportProtocol.TLS)) {
65 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
66 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
67 "/selfSignedController", PathType.CLASSPATH,
68 Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
70 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true, false);
71 config.setTransferProtocol(protocol);
75 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
78 public void testStartup1() {
79 provider = new SwitchConnectionProviderImpl(config);
80 final ListenableFuture<Boolean> future = provider.startup();
82 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
83 } catch (InterruptedException | ExecutionException | TimeoutException e) {
84 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
89 * Tests provider startup - without configuration.
92 public void testStartup2() throws UnknownHostException {
94 provider.setSwitchConnectionHandler(handler);
95 final ListenableFuture<Boolean> future = provider.startup();
97 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
98 } catch (InterruptedException | ExecutionException | TimeoutException e) {
99 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
104 * Tests provider startup - without {@link SwitchConnectionHandler}.
107 public void testStartup3() throws UnknownHostException {
108 startUp(TransportProtocol.TCP);
109 final ListenableFuture<Boolean> future = provider.startup();
111 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
112 } catch (InterruptedException | ExecutionException | TimeoutException e) {
113 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
114 + " SwitchConnectionHandler is not set", e.getMessage());
119 * Tests correct provider startup - over TCP.
122 public void testStartup4() throws UnknownHostException {
123 startUp(TransportProtocol.TCP);
124 provider.setSwitchConnectionHandler(handler);
126 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
127 } catch (InterruptedException | ExecutionException | TimeoutException e) {
133 * Tests correct provider startup - over TLS.
136 public void testStartup5() throws UnknownHostException {
137 startUp(TransportProtocol.TLS);
138 provider.setSwitchConnectionHandler(handler);
140 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
141 } catch (InterruptedException | ExecutionException | TimeoutException e) {
147 * Tests correct provider startup - over UDP.
150 public void testStartup6() throws UnknownHostException {
151 startUp(TransportProtocol.UDP);
152 provider.setSwitchConnectionHandler(handler);
154 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
155 } catch (InterruptedException | ExecutionException | TimeoutException e) {
161 * Tests correct provider shutdown.
164 public void testShutdown() throws UnknownHostException {
165 startUp(TransportProtocol.TCP);
166 provider.setSwitchConnectionHandler(handler);
168 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
169 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
170 } catch (InterruptedException | ExecutionException | TimeoutException e) {
171 LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);