Merge changes from topic 'BUG-3774'
[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     /**
59      * before each test method
60      */
61     @Before
62     public void setUp() {
63         connectionManagerImpl = new ConnectionManagerImpl();
64         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
65         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
66         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
67         Mockito.when(connection.isAlive()).thenReturn(true);
68         Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
69                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
70     }
71
72     /**
73      * after each test method
74      * @throws InterruptedException
75      */
76     @After
77     public void tearDown() throws InterruptedException {
78         Thread.sleep(200L);
79     }
80
81     /**
82      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
83      * invoking onConnectionReady first, scenario:
84      * <ol>
85      *  <li>send hello to device (rpc with void output)</li>
86      *  <li>receive hello from device (notification)</li>
87      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
88      *  <li>wait for rpc to finish with getFeatureOutput</li>
89      * </ol>
90      * @throws InterruptedException
91      */
92     @Test
93     public void testOnSwitchConnected1() throws InterruptedException {
94         connectionManagerImpl.onSwitchConnected(connection);
95         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
96         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
97
98         // prepare void reply (hello rpc output)
99         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
100         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
101         // prepare getFeature reply (getFeture rpc output)
102         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
103         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
104
105
106         // fire handshake
107         connectionReadyListenerAC.getValue().onConnectionReady();
108
109         // deliver hello send output (void)
110         Thread.sleep(100L);
111         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
112         voidResponseFx.set(helloResponse);
113
114         // send hello reply
115         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
116         ofpListenerAC.getValue().onHelloMessage(hello);
117
118         // deliver getFeature output
119         Thread.sleep(100L);
120         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
121         .setDatapathId(BigInteger.TEN)
122         .setVersion(OFConstants.OFP_VERSION_1_3)
123         .setXid(2L)
124         .setTables((short) 15)
125         .build();
126         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
127         featureResponseFx.set(rpcFeaturesOutput);
128
129         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
130     }
131
132     /**
133      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
134      * invoking onHelloMessage, scenario:
135      * <ol>
136      *  <li>receive hello from device (notification)</li>
137      *  <li>send hello to device (rpc with void output)</li>
138      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
139      *  <li>wait for rpc to finish with getFeatureOutput</li>
140      * </ol>
141      * @throws InterruptedException
142      */
143     @Test
144     public void testOnSwitchConnected2() throws InterruptedException {
145         connectionManagerImpl.onSwitchConnected(connection);
146         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
147         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
148
149         // prepare void reply (hello rpc output)
150         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
151         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
152         // prepare getFeature reply (getFeture rpc output)
153         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
154         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
155
156
157         // fire handshake - send hello reply
158         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
159         ofpListenerAC.getValue().onHelloMessage(hello);
160
161         // notify about connection ready
162         connectionReadyListenerAC.getValue().onConnectionReady();
163
164         // deliver hello send output (void)
165         Thread.sleep(100L);
166         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
167         voidResponseFx.set(helloResponse);
168
169         // deliver getFeature output
170         Thread.sleep(100L);
171         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
172         .setDatapathId(BigInteger.TEN)
173         .setVersion(OFConstants.OFP_VERSION_1_3)
174         .setXid(2L)
175         .setTables((short) 15)
176         .build();
177         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
178         featureResponseFx.set(rpcFeaturesOutput);
179
180         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
181     }
182 }