5c33b8d2988f4f4b0c82b53bffabd134c0650a5d
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / connection / SwitchConnectionProviderImplTest.java
1 /*\r
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.openflowjava.protocol.impl.connection;\r
10 \r
11 import java.net.InetAddress;\r
12 import java.net.UnknownHostException;\r
13 import java.util.concurrent.ExecutionException;\r
14 import java.util.concurrent.TimeUnit;\r
15 import java.util.concurrent.TimeoutException;\r
16 \r
17 import org.junit.Assert;\r
18 import org.junit.Test;\r
19 import org.mockito.Mock;\r
20 import org.mockito.MockitoAnnotations;\r
21 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;\r
22 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;\r
23 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;\r
24 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.TransportProtocol;\r
28 \r
29 import com.google.common.util.concurrent.ListenableFuture;\r
30 \r
31 /**\r
32  * @author michal.polkorab\r
33  *\r
34  */\r
35 public class SwitchConnectionProviderImplTest {\r
36 \r
37     @Mock SwitchConnectionHandler handler;\r
38 \r
39     private static final int SWITCH_IDLE_TIMEOUT = 2000;\r
40     private static final int WAIT_TIMEOUT = 2000;\r
41     private InetAddress startupAddress;\r
42     private TlsConfiguration tlsConfiguration;\r
43     private SwitchConnectionProviderImpl provider;\r
44     private ConnectionConfigurationImpl config;\r
45 \r
46     /**\r
47      * Creates new {@link SwitchConnectionProvider} instance for each test\r
48      * @param protocol communication protocol\r
49      */\r
50     public void startUp(TransportProtocol protocol) {\r
51         MockitoAnnotations.initMocks(this);\r
52         config = null;\r
53         if (protocol != null) {\r
54             createConfig(protocol);\r
55         }\r
56         provider = new SwitchConnectionProviderImpl();\r
57     }\r
58 \r
59     private void createConfig(TransportProtocol protocol) {\r
60         try {\r
61             startupAddress = InetAddress.getLocalHost();\r
62         } catch (UnknownHostException e) {\r
63             e.printStackTrace();\r
64         }\r
65         tlsConfiguration = null;\r
66         if (protocol.equals(TransportProtocol.TLS)) {\r
67             tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS,\r
68                     "/selfSignedSwitch", PathType.CLASSPATH, KeystoreType.JKS,\r
69                     "/selfSignedController", PathType.CLASSPATH) ;\r
70         }\r
71         config = new ConnectionConfigurationImpl(startupAddress, 0, tlsConfiguration, SWITCH_IDLE_TIMEOUT);\r
72         config.setTransferProtocol(protocol);\r
73     }\r
74 \r
75     /**\r
76      * Tests provider startup - without configuration and {@link SwitchConnectionHandler}\r
77      */\r
78     @Test\r
79     public void testStartup1() {\r
80         provider = new SwitchConnectionProviderImpl();\r
81         ListenableFuture<Boolean> future = provider.startup();\r
82         try {\r
83             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
84         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
85             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());\r
86         }\r
87     }\r
88 \r
89     /**\r
90      * Tests provider startup - without configuration\r
91      */\r
92     @Test\r
93     public void testStartup2() {\r
94         startUp(null);\r
95         provider.setSwitchConnectionHandler(handler);\r
96         ListenableFuture<Boolean> future = provider.startup();\r
97         try {\r
98             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
99         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
100             Assert.assertEquals("Wrong state", "java.lang.NullPointerException", e.getMessage());\r
101         }\r
102     }\r
103 \r
104     /**\r
105      * Tests provider startup - without {@link SwitchConnectionHandler}\r
106      */\r
107     @Test\r
108     public void testStartup3() {\r
109         startUp(TransportProtocol.TCP);\r
110         provider.setConfiguration(config);\r
111         ListenableFuture<Boolean> future = provider.startup();\r
112         try {\r
113             future.get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS);\r
114         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
115             Assert.assertEquals("Wrong state", "java.lang.IllegalStateException:"\r
116                     + " SwitchConnectionHandler is not set", e.getMessage());\r
117         }\r
118     }\r
119 \r
120     /**\r
121      * Tests correct provider startup - over TCP\r
122      */\r
123     @Test\r
124     public void testStartup4() {\r
125         startUp(TransportProtocol.TCP);\r
126         provider.setConfiguration(config);\r
127         provider.setSwitchConnectionHandler(handler);\r
128         try {\r
129             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
130         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
131             Assert.fail();\r
132         }\r
133     }\r
134 \r
135     /**\r
136      * Tests correct provider startup - over TLS\r
137      */\r
138     @Test\r
139     public void testStartup5() {\r
140         startUp(TransportProtocol.TLS);\r
141         provider.setConfiguration(config);\r
142         provider.setSwitchConnectionHandler(handler);\r
143         try {\r
144             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
145         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
146             Assert.fail();\r
147         }\r
148     }\r
149 \r
150     /**\r
151      * Tests correct provider startup - over UDP\r
152      */\r
153     @Test\r
154     public void testStartup6() {\r
155         startUp(TransportProtocol.UDP);\r
156         provider.setConfiguration(config);\r
157         provider.setSwitchConnectionHandler(handler);\r
158         try {\r
159             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
160         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
161             Assert.fail();\r
162         }\r
163     }\r
164 \r
165     /**\r
166      * Tests correct provider shutdown\r
167      */\r
168     @Test\r
169     public void testShutdown() {\r
170         startUp(TransportProtocol.TCP);\r
171         provider.setConfiguration(config);\r
172         provider.setSwitchConnectionHandler(handler);\r
173         try {\r
174             Assert.assertTrue("Failed to start", provider.startup().get(WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
175             Assert.assertTrue("Failed to stop", provider.shutdown().get(5 * WAIT_TIMEOUT, TimeUnit.MILLISECONDS));\r
176         } catch (InterruptedException | ExecutionException | TimeoutException e) {\r
177             e.printStackTrace();\r
178         }\r
179     }\r
180 }