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.BarrierOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
50 @RunWith(MockitoJUnitRunner.class)
51 public class ConnectionManagerImplTest {
53 // timeout of final step [ms]
54 private static final int FINAL_STEP_TIMEOUT = 500;
55 private ConnectionManagerImpl connectionManagerImpl;
57 private ConnectionAdapter connection;
59 private DeviceConnectedHandler deviceConnectedHandler;
61 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
63 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
65 private static final long ECHO_REPLY_TIMEOUT = 500;
66 private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
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 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
77 .build(), threadPool);
79 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
80 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
81 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
82 Mockito.when(connection.isAlive()).thenReturn(true);
83 Mockito.when(connection.barrier(ArgumentMatchers.any()))
84 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
88 public void tearDown() throws InterruptedException {
94 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
95 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
96 * invoking onConnectionReady first, scenario:
98 * <li>send hello to device (rpc with void output)</li>
99 * <li>receive hello from device (notification)</li>
100 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
101 * <li>wait for rpc to finish with getFeatureOutput</li>
104 * @throws InterruptedException - interrupted exception
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<HelloOutput>> voidResponseFx = SettableFuture.create();
114 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
115 // prepare getFeature reply (getFeture rpc output)
116 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
117 SettableFuture.create();
118 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
122 connectionReadyListenerAC.getValue().onConnectionReady();
124 // deliver hello send output (void)
126 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
127 voidResponseFx.set(helloResponse);
130 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
131 ofpListenerAC.getValue().onHelloMessage(hello);
133 // deliver getFeature output
135 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
136 .setDatapathId(BigInteger.TEN)
137 .setVersion(OFConstants.OFP_VERSION_1_3)
139 .setTables((short) 15)
141 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
142 featureResponseFx.set(rpcFeaturesOutput);
144 Mockito.verify(deviceConnectedHandler,
145 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
150 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
151 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
152 * invoking onHelloMessage, scenario:
154 * <li>receive hello from device (notification)</li>
155 * <li>send hello to device (rpc with void output)</li>
156 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
157 * <li>wait for rpc to finish with getFeatureOutput</li>
160 * @throws InterruptedException - interrupted exception
163 public void testOnSwitchConnected2() throws Exception {
164 connectionManagerImpl.onSwitchConnected(connection);
165 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
166 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
168 // prepare void reply (hello rpc output)
169 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
170 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
171 // prepare getFeature reply (getFeture rpc output)
172 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
173 SettableFuture.create();
174 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
177 // fire handshake - send hello reply
178 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
179 ofpListenerAC.getValue().onHelloMessage(hello);
181 // notify about connection ready
182 connectionReadyListenerAC.getValue().onConnectionReady();
184 // deliver hello send output (void)
186 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
187 voidResponseFx.set(helloResponse);
189 // deliver getFeature output
191 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
192 .setDatapathId(BigInteger.TEN)
193 .setVersion(OFConstants.OFP_VERSION_1_3)
195 .setTables((short) 15)
197 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
198 featureResponseFx.set(rpcFeaturesOutput);
200 Mockito.verify(deviceConnectedHandler,
201 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));