2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.openflowplugin.impl.connection;
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.GetFeaturesInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
41 @RunWith(MockitoJUnitRunner.class)
42 public class ConnectionManagerImplTest {
44 /** timeout of final step [ms] */
45 private static final int FINAL_STEP_TIMEOUT = 500;
46 private ConnectionManagerImpl connectionManagerImpl;
48 private ConnectionAdapter connection;
50 private DeviceConnectedHandler deviceConnectedHandler;
52 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
54 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
57 * before each test method
61 connectionManagerImpl = new ConnectionManagerImpl();
62 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
63 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
64 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
65 Mockito.when(connection.isAlive()).thenReturn(true);
69 * after each test method
70 * @throws InterruptedException
73 public void tearDown() throws InterruptedException {
78 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
79 * invoking onConnectionReady first, scenario:
81 * <li>send hello to device (rpc with void output)</li>
82 * <li>receive hello from device (notification)</li>
83 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
84 * <li>wait for rpc to finish with getFeatureOutput</li>
86 * @throws InterruptedException
89 public void testOnSwitchConnected1() throws InterruptedException {
90 connectionManagerImpl.onSwitchConnected(connection);
91 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
92 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
94 // prepare void reply (hello rpc output)
95 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
96 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
97 // prepare getFeature reply (getFeture rpc output)
98 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
99 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
103 connectionReadyListenerAC.getValue().onConnectionReady();
105 // deliver hello send output (void)
107 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
108 voidResponseFx.set(helloResponse);
111 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
112 ofpListenerAC.getValue().onHelloMessage(hello);
114 // deliver getFeature output
116 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
117 .setDatapathId(BigInteger.TEN)
118 .setVersion(OFConstants.OFP_VERSION_1_3)
120 .setTables((short) 15)
122 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
123 featureResponseFx.set(rpcFeaturesOutput);
125 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
129 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
130 * invoking onHelloMessage, scenario:
132 * <li>receive hello from device (notification)</li>
133 * <li>send hello to device (rpc with void output)</li>
134 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
135 * <li>wait for rpc to finish with getFeatureOutput</li>
137 * @throws InterruptedException
140 public void testOnSwitchConnected2() throws InterruptedException {
141 connectionManagerImpl.onSwitchConnected(connection);
142 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
143 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
145 // prepare void reply (hello rpc output)
146 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
147 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
148 // prepare getFeature reply (getFeture rpc output)
149 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
150 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
153 // fire handshake - send hello reply
154 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
155 ofpListenerAC.getValue().onHelloMessage(hello);
157 // notify about connection ready
158 connectionReadyListenerAC.getValue().onConnectionReady();
160 // deliver hello send output (void)
162 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
163 voidResponseFx.set(helloResponse);
165 // deliver getFeature output
167 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
168 .setDatapathId(BigInteger.TEN)
169 .setVersion(OFConstants.OFP_VERSION_1_3)
171 .setTables((short) 15)
173 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
174 featureResponseFx.set(rpcFeaturesOutput);
176 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));