ConnectionManager proposal
[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.junit.Assert.fail;
11 import com.google.common.util.concurrent.SettableFuture;
12 import java.math.BigInteger;
13 import java.net.InetSocketAddress;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Ignore;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
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     }
69
70     /**
71      * after each test method
72      * @throws InterruptedException
73      */
74     @After
75     public void tearDown() throws InterruptedException {
76         Thread.sleep(200L);
77     }
78
79     /**
80      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
81      * invoking onConnectionReady first, scenario:
82      * <ol>
83      *  <li>send hello to device (rpc with void output)</li>
84      *  <li>receive hello from device (notification)</li>
85      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
86      *  <li>wait for rpc to finish with getFeatureOutput</li>
87      * </ol>
88      * @throws InterruptedException
89      */
90     @Test
91     public void testOnSwitchConnected1() throws InterruptedException {
92         connectionManagerImpl.onSwitchConnected(connection);
93         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
94         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
95
96         // prepare void reply (hello rpc output)
97         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
98         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
99         // prepare getFeature reply (getFeture rpc output)
100         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
101         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
102
103
104         // fire handshake
105         connectionReadyListenerAC.getValue().onConnectionReady();
106
107         // deliver hello send output (void)
108         Thread.sleep(100L);
109         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
110         voidResponseFx.set(helloResponse);
111
112         // send hello reply
113         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
114         ofpListenerAC.getValue().onHelloMessage(hello);
115
116         // deliver getFeature output
117         Thread.sleep(100L);
118         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
119         .setDatapathId(BigInteger.TEN)
120         .setVersion(OFConstants.OFP_VERSION_1_3)
121         .setXid(2L)
122         .setTables((short) 15)
123         .build();
124         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
125         featureResponseFx.set(rpcFeaturesOutput);
126
127         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
128     }
129
130     /**
131      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
132      * invoking onHelloMessage, scenario:
133      * <ol>
134      *  <li>receive hello from device (notification)</li>
135      *  <li>send hello to device (rpc with void output)</li>
136      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
137      *  <li>wait for rpc to finish with getFeatureOutput</li>
138      * </ol>
139      * @throws InterruptedException
140      */
141     @Test
142     public void testOnSwitchConnected2() throws InterruptedException {
143         connectionManagerImpl.onSwitchConnected(connection);
144         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
145         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
146
147         // prepare void reply (hello rpc output)
148         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
149         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
150         // prepare getFeature reply (getFeture rpc output)
151         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
152         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
153
154
155         // fire handshake - send hello reply
156         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
157         ofpListenerAC.getValue().onHelloMessage(hello);
158
159         // notify about connection ready
160         connectionReadyListenerAC.getValue().onConnectionReady();
161
162         // deliver hello send output (void)
163         Thread.sleep(100L);
164         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
165         voidResponseFx.set(helloResponse);
166
167         // deliver getFeature output
168         Thread.sleep(100L);
169         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
170         .setDatapathId(BigInteger.TEN)
171         .setVersion(OFConstants.OFP_VERSION_1_3)
172         .setXid(2L)
173         .setTables((short) 15)
174         .build();
175         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
176         featureResponseFx.set(rpcFeaturesOutput);
177
178         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
179     }
180
181     /**
182      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#setOpenflowProtocolListener(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener)}.
183      */
184     @Test
185     @Ignore
186     public void testSetOpenflowProtocolListener() {
187         fail("Not yet implemented");
188     }
189
190 }