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;
58 private final static int ECHO_REPLY_TIMEOUT = 500;
61 * before each test method
65 connectionManagerImpl = new ConnectionManagerImpl(ECHO_REPLY_TIMEOUT);
66 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
67 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
68 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
69 Mockito.when(connection.isAlive()).thenReturn(true);
70 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
71 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
75 * after each test method
76 * @throws InterruptedException
79 public void tearDown() throws InterruptedException {
84 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
85 * invoking onConnectionReady first, scenario:
87 * <li>send hello to device (rpc with void output)</li>
88 * <li>receive hello from device (notification)</li>
89 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
90 * <li>wait for rpc to finish with getFeatureOutput</li>
92 * @throws InterruptedException
95 public void testOnSwitchConnected1() throws Exception {
96 connectionManagerImpl.onSwitchConnected(connection);
97 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
98 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
100 // prepare void reply (hello rpc output)
101 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
102 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
103 // prepare getFeature reply (getFeture rpc output)
104 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
105 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
109 connectionReadyListenerAC.getValue().onConnectionReady();
111 // deliver hello send output (void)
113 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
114 voidResponseFx.set(helloResponse);
117 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
118 ofpListenerAC.getValue().onHelloMessage(hello);
120 // deliver getFeature output
122 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
123 .setDatapathId(BigInteger.TEN)
124 .setVersion(OFConstants.OFP_VERSION_1_3)
126 .setTables((short) 15)
128 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
129 featureResponseFx.set(rpcFeaturesOutput);
131 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
135 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
136 * invoking onHelloMessage, scenario:
138 * <li>receive hello from device (notification)</li>
139 * <li>send hello to device (rpc with void output)</li>
140 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
141 * <li>wait for rpc to finish with getFeatureOutput</li>
143 * @throws InterruptedException
146 public void testOnSwitchConnected2() throws Exception {
147 connectionManagerImpl.onSwitchConnected(connection);
148 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
149 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
151 // prepare void reply (hello rpc output)
152 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
153 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
154 // prepare getFeature reply (getFeture rpc output)
155 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
156 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
159 // fire handshake - send hello reply
160 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
161 ofpListenerAC.getValue().onHelloMessage(hello);
163 // notify about connection ready
164 connectionReadyListenerAC.getValue().onConnectionReady();
166 // deliver hello send output (void)
168 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
169 voidResponseFx.set(helloResponse);
171 // deliver getFeature output
173 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
174 .setDatapathId(BigInteger.TEN)
175 .setVersion(OFConstants.OFP_VERSION_1_3)
177 .setTables((short) 15)
179 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
180 featureResponseFx.set(rpcFeaturesOutput);
182 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));