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.OpenflowDiagStatusProvider;
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 OpenflowDiagStatusProvider openflowPluginDiagStatusProvider;
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
55 public void startUp(final TransportProtocol protocol) throws UnknownHostException {
56 MockitoAnnotations.initMocks(this);
58 if (protocol != null) {
59 createConfig(protocol);
61 provider = new SwitchConnectionProviderImpl(config, openflowPluginDiagStatusProvider);
64 private void createConfig(final TransportProtocol protocol) throws UnknownHostException {
65 InetAddress startupAddress = InetAddress.getLocalHost();
67 tlsConfiguration = null;
68 if (protocol.equals(TransportProtocol.TLS)) {
69 tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,
70 "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,
71 "/selfSignedController", PathType.CLASSPATH,
72 Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256")) ;
74 config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT, true,
75 false, CHANNEL_OUTBOUND_QUEUE_SIZE);
76 config.setTransferProtocol(protocol);
80 * Tests provider startup - without configuration and {@link SwitchConnectionHandler}.
83 public void testStartup1() throws UnknownHostException {
85 final ListenableFuture<Boolean> future = provider.startup();
87 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
88 } catch (InterruptedException | ExecutionException | TimeoutException e) {
89 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
94 * Tests provider startup - without configuration.
97 public void testStartup2() throws UnknownHostException {
99 provider.setSwitchConnectionHandler(handler);
100 final ListenableFuture<Boolean> future = provider.startup();
102 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
103 } catch (InterruptedException | ExecutionException | TimeoutException e) {
104 Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());
109 * Tests provider startup - without {@link SwitchConnectionHandler}.
112 public void testStartup3() throws UnknownHostException {
113 startUp(TransportProtocol.TCP);
114 final ListenableFuture<Boolean> future = provider.startup();
116 future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);
117 } catch (InterruptedException | ExecutionException | TimeoutException e) {
118 Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"
119 + " SwitchConnectionHandler is not set", e.getMessage());
124 * Tests correct provider startup - over TCP.
127 public void testStartup4() throws UnknownHostException {
128 startUp(TransportProtocol.TCP);
129 provider.setSwitchConnectionHandler(handler);
131 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
132 } catch (InterruptedException | ExecutionException | TimeoutException e) {
138 * Tests correct provider startup - over TLS.
141 public void testStartup5() throws UnknownHostException {
142 startUp(TransportProtocol.TLS);
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() throws UnknownHostException {
156 startUp(TransportProtocol.UDP);
157 provider.setSwitchConnectionHandler(handler);
159 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
160 } catch (InterruptedException | ExecutionException | TimeoutException e) {
166 * Tests correct provider shutdown.
169 public void testShutdown() throws UnknownHostException {
170 startUp(TransportProtocol.TCP);
171 provider.setSwitchConnectionHandler(handler);
173 Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
174 Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));
175 } catch (InterruptedException | ExecutionException | TimeoutException e) {
176 LoggerFactory.getLogger(SwitchConnectionProviderImplTest.class).error("Unexpected error", e);