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.mockito.ArgumentMatchers.any;
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.math.BigInteger;
14 import java.net.InetSocketAddress;
15 import java.util.concurrent.SynchronousQueue;
16 import java.util.concurrent.TimeUnit;
17 import org.junit.After;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.ArgumentCaptor;
22 import org.mockito.ArgumentMatchers;
23 import org.mockito.Captor;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
28 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
29 import org.opendaylight.openflowplugin.api.OFConstants;
30 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
31 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
32 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49 * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
51 @RunWith(MockitoJUnitRunner.class)
52 public class ConnectionManagerImplTest {
54 // timeout of final step [ms]
55 private static final int FINAL_STEP_TIMEOUT = 500;
56 private ConnectionManagerImpl connectionManagerImpl;
58 private ConnectionAdapter connection;
60 private DeviceConnectedHandler deviceConnectedHandler;
62 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
64 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
66 private static final long ECHO_REPLY_TIMEOUT = 500;
67 private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
71 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
72 60L, TimeUnit.SECONDS,
73 new SynchronousQueue<>(), "ofppool");
75 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
76 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
77 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
78 .build(), threadPool);
80 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
81 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
82 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
83 Mockito.when(connection.isAlive()).thenReturn(true);
84 Mockito.when(connection.barrier(ArgumentMatchers.<BarrierInput>any()))
85 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
89 public void tearDown() throws InterruptedException {
95 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
96 * 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>
105 * @throws InterruptedException - interrupted exception
108 public void testOnSwitchConnected1() throws Exception {
109 connectionManagerImpl.onSwitchConnected(connection);
110 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
111 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
113 // prepare void reply (hello rpc output)
114 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
115 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
116 // prepare getFeature reply (getFeture rpc output)
117 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
118 SettableFuture.<RpcResult<GetFeaturesOutput>>create();
119 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
123 connectionReadyListenerAC.getValue().onConnectionReady();
125 // deliver hello send output (void)
127 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
128 voidResponseFx.set(helloResponse);
131 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
132 ofpListenerAC.getValue().onHelloMessage(hello);
134 // deliver getFeature output
136 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
137 .setDatapathId(BigInteger.TEN)
138 .setVersion(OFConstants.OFP_VERSION_1_3)
140 .setTables((short) 15)
142 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
143 featureResponseFx.set(rpcFeaturesOutput);
145 Mockito.verify(deviceConnectedHandler,
146 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
151 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
152 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
153 * invoking onHelloMessage, scenario:
155 * <li>receive hello from device (notification)</li>
156 * <li>send hello to device (rpc with void output)</li>
157 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
158 * <li>wait for rpc to finish with getFeatureOutput</li>
161 * @throws InterruptedException - interrupted exception
164 public void testOnSwitchConnected2() throws Exception {
165 connectionManagerImpl.onSwitchConnected(connection);
166 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
167 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
169 // prepare void reply (hello rpc output)
170 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
171 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
172 // prepare getFeature reply (getFeture rpc output)
173 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
174 SettableFuture.<RpcResult<GetFeaturesOutput>>create();
175 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
178 // fire handshake - send hello reply
179 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
180 ofpListenerAC.getValue().onHelloMessage(hello);
182 // notify about connection ready
183 connectionReadyListenerAC.getValue().onConnectionReady();
185 // deliver hello send output (void)
187 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
188 voidResponseFx.set(helloResponse);
190 // deliver getFeature output
192 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
193 .setDatapathId(BigInteger.TEN)
194 .setVersion(OFConstants.OFP_VERSION_1_3)
196 .setTables((short) 15)
198 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
199 featureResponseFx.set(rpcFeaturesOutput);
201 Mockito.verify(deviceConnectedHandler,
202 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));