Remove redundant type specifiers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.connection;
9
10 import static org.mockito.ArgumentMatchers.any;
11
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.math.BigInteger;
14 import java.net.InetSocketAddress;
15 import java.util.concurrent.SynchronousQueue;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.ArgumentMatchers;
23 import org.mockito.Captor;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
29 import org.opendaylight.openflowplugin.api.OFConstants;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
32 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46
47 /**
48  * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
49  */
50 @RunWith(MockitoJUnitRunner.class)
51 public class ConnectionManagerImplTest {
52
53     // timeout of final step [ms]
54     private static final int FINAL_STEP_TIMEOUT = 500;
55     private ConnectionManagerImpl connectionManagerImpl;
56     @Mock
57     private ConnectionAdapter connection;
58     @Mock
59     private DeviceConnectedHandler deviceConnectedHandler;
60     @Captor
61     private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
62     @Captor
63     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
64
65     private static final long ECHO_REPLY_TIMEOUT = 500;
66     private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
67
68     @Before
69     public void setUp() {
70         final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
71                 60L, TimeUnit.SECONDS,
72                 new SynchronousQueue<>(), "ofppool");
73
74         connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
75                 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
76                 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
77                 .build(), threadPool);
78
79         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
80         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
81         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
82         Mockito.when(connection.isAlive()).thenReturn(true);
83         Mockito.when(connection.barrier(ArgumentMatchers.any()))
84                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
85     }
86
87     @After
88     public void tearDown() throws InterruptedException {
89         Thread.sleep(200L);
90     }
91
92     /**
93      * Test method for
94      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
95      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
96      * invoking onConnectionReady first, scenario:
97      * <ol>
98      * <li>send hello to device (rpc with void output)</li>
99      * <li>receive hello from device (notification)</li>
100      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
101      * <li>wait for rpc to finish with getFeatureOutput</li>
102      * </ol>
103      *
104      * @throws InterruptedException - interrupted exception
105      */
106     @Test
107     public void testOnSwitchConnected1() throws Exception {
108         connectionManagerImpl.onSwitchConnected(connection);
109         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
110         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
111
112         // prepare void reply (hello rpc output)
113         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
114         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
115         // prepare getFeature reply (getFeture rpc output)
116         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
117                 SettableFuture.create();
118         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
119
120
121         // fire handshake
122         connectionReadyListenerAC.getValue().onConnectionReady();
123
124         // deliver hello send output (void)
125         Thread.sleep(100L);
126         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
127         voidResponseFx.set(helloResponse);
128
129         // send hello reply
130         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
131         ofpListenerAC.getValue().onHelloMessage(hello);
132
133         // deliver getFeature output
134         Thread.sleep(100L);
135         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
136                 .setDatapathId(BigInteger.TEN)
137                 .setVersion(OFConstants.OFP_VERSION_1_3)
138                 .setXid(2L)
139                 .setTables((short) 15)
140                 .build();
141         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
142         featureResponseFx.set(rpcFeaturesOutput);
143
144         Mockito.verify(deviceConnectedHandler,
145                 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
146     }
147
148     /**
149      * Test method for
150      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
151      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
152      * invoking onHelloMessage, scenario:
153      * <ol>
154      * <li>receive hello from device (notification)</li>
155      * <li>send hello to device (rpc with void output)</li>
156      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
157      * <li>wait for rpc to finish with getFeatureOutput</li>
158      * </ol>
159      *
160      * @throws InterruptedException - interrupted exception
161      */
162     @Test
163     public void testOnSwitchConnected2() throws Exception {
164         connectionManagerImpl.onSwitchConnected(connection);
165         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
166         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
167
168         // prepare void reply (hello rpc output)
169         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
170         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
171         // prepare getFeature reply (getFeture rpc output)
172         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
173                 SettableFuture.create();
174         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
175
176
177         // fire handshake - send hello reply
178         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
179         ofpListenerAC.getValue().onHelloMessage(hello);
180
181         // notify about connection ready
182         connectionReadyListenerAC.getValue().onConnectionReady();
183
184         // deliver hello send output (void)
185         Thread.sleep(100L);
186         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
187         voidResponseFx.set(helloResponse);
188
189         // deliver getFeature output
190         Thread.sleep(100L);
191         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
192                 .setDatapathId(BigInteger.TEN)
193                 .setVersion(OFConstants.OFP_VERSION_1_3)
194                 .setXid(2L)
195                 .setTables((short) 15)
196                 .build();
197         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
198         featureResponseFx.set(rpcFeaturesOutput);
199
200         Mockito.verify(deviceConnectedHandler,
201                 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));
202     }
203 }