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;
32 * @author michal.polkorab
35 public class SwitchConnectionProviderImplTest {
37 @Mock SwitchConnectionHandler handler;
39 private static final int SWITCH_IDLE_TIMEOUT = 2000;
40 private static final int WAIT_TIMEOUT = 2000;
41 private InetAddress startupAddress;
42 private TlsConfiguration tlsConfiguration;
43 private SwitchConnectionProviderImpl provider;
44 private ConnectionConfigurationImpl config;
47 * Creates new {@link SwitchConnectionProvider} instance for each test
48 * @param protocol communication protocol
50 public void startUp(final TransportProtocol protocol) {
51 MockitoAnnotations.initMocks(this);
53 if (protocol != null) {
54 createConfig(protocol);
56 provider = new SwitchConnectionProviderImpl();
59 private void createConfig(final TransportProtocol protocol) {
61 startupAddress = InetAddress.getLocalHost();
62 } catch (final UnknownHostException e) {
65 tlsConfiguration = null;
66 if (protocol.equals(TransportProtocol.TLS)) {
67 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
68 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
69 "/selfSignedController", PathType.CLASSPATH,
70 Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
72 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true);
73 config.setTransferProtocol(protocol);
77 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}
80 public void testStartup1() {
81 provider = new SwitchConnectionProviderImpl();
82 final ListenableFuture<Boolean> future = provider.startup();
84 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
85 } catch (InterruptedException | ExecutionException | TimeoutException e) {
86 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
91 * Tests provider startup - without configuration
94 public void testStartup2() {
96 provider.setSwitchConnectionHandler(handler);
97 final ListenableFuture<Boolean> future = provider.startup();
99 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
100 } catch (InterruptedException | ExecutionException | TimeoutException e) {
101 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
106 * Tests provider startup - without {@link SwitchConnectionHandler}
109 public void testStartup3() {
110 startUp(TransportProtocol.TCP);
111 provider.setConfiguration(config);
112 final ListenableFuture<Boolean> future = provider.startup();
114 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
115 } catch (InterruptedException | ExecutionException | TimeoutException e) {
116 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
117 + " SwitchConnectionHandler is not set", e.getMessage());
122 * Tests correct provider startup - over TCP
125 public void testStartup4() {
126 startUp(TransportProtocol.TCP);
127 provider.setConfiguration(config);
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() {
141 startUp(TransportProtocol.TLS);
142 provider.setConfiguration(config);
143 provider.setSwitchConnectionHandler(handler);
145 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
146 } catch (InterruptedException | ExecutionException | TimeoutException e) {
152 * Tests correct provider startup - over UDP
155 public void testStartup6() {
156 startUp(TransportProtocol.UDP);
157 provider.setConfiguration(config);
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() {
171 startUp(TransportProtocol.TCP);
172 provider.setConfiguration(config);
173 provider.setSwitchConnectionHandler(handler);
175 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
176 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
177 } catch (InterruptedException | ExecutionException | TimeoutException e) {