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.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;
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);
68 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
69 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
73 * after each test method
74 * @throws InterruptedException
77 public void tearDown() throws InterruptedException {
82 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
83 * invoking onConnectionReady first, scenario:
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>
90 * @throws InterruptedException
93 public void testOnSwitchConnected1() throws InterruptedException {
94 connectionManagerImpl.onSwitchConnected(connection);
95 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
96 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
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);
107 connectionReadyListenerAC.getValue().onConnectionReady();
109 // deliver hello send output (void)
111 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
112 voidResponseFx.set(helloResponse);
115 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
116 ofpListenerAC.getValue().onHelloMessage(hello);
118 // deliver getFeature output
120 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
121 .setDatapathId(BigInteger.TEN)
122 .setVersion(OFConstants.OFP_VERSION_1_3)
124 .setTables((short) 15)
126 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
127 featureResponseFx.set(rpcFeaturesOutput);
129 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
133 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
134 * invoking onHelloMessage, scenario:
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>
141 * @throws InterruptedException
144 public void testOnSwitchConnected2() throws InterruptedException {
145 connectionManagerImpl.onSwitchConnected(connection);
146 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
147 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
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);
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);
161 // notify about connection ready
162 connectionReadyListenerAC.getValue().onConnectionReady();
164 // deliver hello send output (void)
166 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
167 voidResponseFx.set(helloResponse);
169 // deliver getFeature output
171 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
172 .setDatapathId(BigInteger.TEN)
173 .setVersion(OFConstants.OFP_VERSION_1_3)
175 .setTables((short) 15)
177 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
178 featureResponseFx.set(rpcFeaturesOutput);
180 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));