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.InetAddress;
13 import java.net.InetSocketAddress;
14 import org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
44 @RunWith(MockitoJUnitRunner.class)
45 public class ConnectionManagerImplTest {
47 /** timeout of final step [ms] */
48 private static final int FINAL_STEP_TIMEOUT = 500;
49 private ConnectionManagerImpl connectionManagerImpl;
51 private ConnectionAdapter connection;
53 private DeviceConnectedHandler deviceConnectedHandler;
55 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
57 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
59 private final static int ECHO_REPLY_TIMEOUT = 500;
62 * before each test method
66 connectionManagerImpl = new ConnectionManagerImpl(ECHO_REPLY_TIMEOUT);
67 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
68 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
69 final InetAddress inetAddress = deviceAddress.getAddress();
70 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
71 Mockito.when(connection.isAlive()).thenReturn(true);
72 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
73 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
74 // Mockito.when(connection.getRemoteAddress().getAddress()).thenReturn(inetAddress);
78 * after each test method
79 * @throws InterruptedException
82 public void tearDown() throws InterruptedException {
87 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
88 * invoking onConnectionReady first, scenario:
90 * <li>send hello to device (rpc with void output)</li>
91 * <li>receive hello from device (notification)</li>
92 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
93 * <li>wait for rpc to finish with getFeatureOutput</li>
95 * @throws InterruptedException
98 public void testOnSwitchConnected1() throws Exception {
99 connectionManagerImpl.onSwitchConnected(connection);
100 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
101 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
103 // prepare void reply (hello rpc output)
104 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
105 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
106 // prepare getFeature reply (getFeture rpc output)
107 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
108 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
112 connectionReadyListenerAC.getValue().onConnectionReady();
114 // deliver hello send output (void)
116 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
117 voidResponseFx.set(helloResponse);
120 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
121 ofpListenerAC.getValue().onHelloMessage(hello);
123 // deliver getFeature output
125 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
126 .setDatapathId(BigInteger.TEN)
127 .setVersion(OFConstants.OFP_VERSION_1_3)
129 .setTables((short) 15)
131 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
132 featureResponseFx.set(rpcFeaturesOutput);
134 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
138 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
139 * invoking onHelloMessage, scenario:
141 * <li>receive hello from device (notification)</li>
142 * <li>send hello to device (rpc with void output)</li>
143 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
144 * <li>wait for rpc to finish with getFeatureOutput</li>
146 * @throws InterruptedException
149 public void testOnSwitchConnected2() throws Exception {
150 connectionManagerImpl.onSwitchConnected(connection);
151 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
152 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
154 // prepare void reply (hello rpc output)
155 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
156 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
157 // prepare getFeature reply (getFeture rpc output)
158 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
159 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
162 // fire handshake - send hello reply
163 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
164 ofpListenerAC.getValue().onHelloMessage(hello);
166 // notify about connection ready
167 connectionReadyListenerAC.getValue().onConnectionReady();
169 // deliver hello send output (void)
171 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
172 voidResponseFx.set(helloResponse);
174 // deliver getFeature output
176 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
177 .setDatapathId(BigInteger.TEN)
178 .setVersion(OFConstants.OFP_VERSION_1_3)
180 .setTables((short) 15)
182 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
183 featureResponseFx.set(rpcFeaturesOutput);
185 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));