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 java.util.concurrent.SynchronousQueue;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.After;
16 import org.junit.Before;
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.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
48 @RunWith(MockitoJUnitRunner.class)
49 public class ConnectionManagerImplTest {
51 /** timeout of final step [ms] */
52 private static final int FINAL_STEP_TIMEOUT = 500;
53 private ConnectionManagerImpl connectionManagerImpl;
55 private ConnectionAdapter connection;
57 private DeviceConnectedHandler deviceConnectedHandler;
59 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
61 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
63 private final static long ECHO_REPLY_TIMEOUT = 500;
66 * before each test method
70 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
71 60L, TimeUnit.SECONDS,
72 new SynchronousQueue<>(), "ofppool");
74 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
75 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
76 .build(), threadPool);
78 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
79 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
80 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
81 Mockito.when(connection.isAlive()).thenReturn(true);
82 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
83 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
87 * after each test method
88 * @throws InterruptedException
91 public void tearDown() throws InterruptedException {
96 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
97 * invoking onConnectionReady first, scenario:
99 * <li>send hello to device (rpc with void output)</li>
100 * <li>receive hello from device (notification)</li>
101 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
102 * <li>wait for rpc to finish with getFeatureOutput</li>
104 * @throws InterruptedException
107 public void testOnSwitchConnected1() throws Exception {
108 connectionManagerImpl.onSwitchConnected(connection);
109 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
110 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
112 // prepare void reply (hello rpc output)
113 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
114 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
115 // prepare getFeature reply (getFeture rpc output)
116 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
117 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
121 connectionReadyListenerAC.getValue().onConnectionReady();
123 // deliver hello send output (void)
125 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
126 voidResponseFx.set(helloResponse);
129 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
130 ofpListenerAC.getValue().onHelloMessage(hello);
132 // deliver getFeature output
134 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
135 .setDatapathId(BigInteger.TEN)
136 .setVersion(OFConstants.OFP_VERSION_1_3)
138 .setTables((short) 15)
140 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
141 featureResponseFx.set(rpcFeaturesOutput);
143 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
147 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
148 * invoking onHelloMessage, scenario:
150 * <li>receive hello from device (notification)</li>
151 * <li>send hello to device (rpc with void output)</li>
152 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
153 * <li>wait for rpc to finish with getFeatureOutput</li>
155 * @throws InterruptedException
158 public void testOnSwitchConnected2() throws Exception {
159 connectionManagerImpl.onSwitchConnected(connection);
160 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
161 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
163 // prepare void reply (hello rpc output)
164 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
165 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
166 // prepare getFeature reply (getFeture rpc output)
167 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
168 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
171 // fire handshake - send hello reply
172 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
173 ofpListenerAC.getValue().onHelloMessage(hello);
175 // notify about connection ready
176 connectionReadyListenerAC.getValue().onConnectionReady();
178 // deliver hello send output (void)
180 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
181 voidResponseFx.set(helloResponse);
183 // deliver getFeature output
185 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
186 .setDatapathId(BigInteger.TEN)
187 .setVersion(OFConstants.OFP_VERSION_1_3)
189 .setTables((short) 15)
191 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
192 featureResponseFx.set(rpcFeaturesOutput);
194 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));