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