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