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 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;
41 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
43 @RunWith(MockitoJUnitRunner.class)
44 public class ConnectionManagerImplTest {
46 /** timeout of final step [ms] */
47 private static final int FINAL_STEP_TIMEOUT = 500;
48 private ConnectionManagerImpl connectionManagerImpl;
50 private ConnectionAdapter connection;
52 private DeviceConnectedHandler deviceConnectedHandler;
54 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
56 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
59 * before each test method
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);
71 * after each test method
72 * @throws InterruptedException
75 public void tearDown() throws InterruptedException {
80 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
81 * invoking onConnectionReady first, scenario:
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>
88 * @throws InterruptedException
91 public void testOnSwitchConnected1() throws InterruptedException {
92 connectionManagerImpl.onSwitchConnected(connection);
93 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
94 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
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);
105 connectionReadyListenerAC.getValue().onConnectionReady();
107 // deliver hello send output (void)
109 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
110 voidResponseFx.set(helloResponse);
113 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
114 ofpListenerAC.getValue().onHelloMessage(hello);
116 // deliver getFeature output
118 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
119 .setDatapathId(BigInteger.TEN)
120 .setVersion(OFConstants.OFP_VERSION_1_3)
122 .setTables((short) 15)
124 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
125 featureResponseFx.set(rpcFeaturesOutput);
127 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
131 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
132 * invoking onHelloMessage, scenario:
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>
139 * @throws InterruptedException
142 public void testOnSwitchConnected2() throws InterruptedException {
143 connectionManagerImpl.onSwitchConnected(connection);
144 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
145 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
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);
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);
159 // notify about connection ready
160 connectionReadyListenerAC.getValue().onConnectionReady();
162 // deliver hello send output (void)
164 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
165 voidResponseFx.set(helloResponse);
167 // deliver getFeature output
169 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
170 .setDatapathId(BigInteger.TEN)
171 .setVersion(OFConstants.OFP_VERSION_1_3)
173 .setTables((short) 15)
175 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
176 featureResponseFx.set(rpcFeaturesOutput);
178 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
182 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#setOpenflowProtocolListener(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener)}.
186 public void testSetOpenflowProtocolListener() {
187 fail("Not yet implemented");