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.time.LocalDateTime;
16 import java.util.concurrent.SynchronousQueue;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.ArgumentMatchers;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
30 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
31 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
35 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51 * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
53 @RunWith(MockitoJUnitRunner.class)
54 public class ConnectionManagerImplTest {
56 // timeout of final step [ms]
57 private static final int FINAL_STEP_TIMEOUT = 500;
58 private ConnectionManagerImpl connectionManagerImpl;
60 private ConnectionAdapter connection;
62 private DeviceConnectedHandler deviceConnectedHandler;
64 private NotificationPublishService notificationPublishService;
66 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
68 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
70 DataBroker dataBroker;
72 private static final long ECHO_REPLY_TIMEOUT = 500;
73 private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
74 private static final int DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = 60;
75 private static final boolean ENABLE_CUSTOM_TRUST_MANAGER = false;
79 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
80 60L, TimeUnit.SECONDS,
81 new SynchronousQueue<>(), "ofppool");
83 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
84 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
85 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
86 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
87 .setEnableCustomTrustManager(ENABLE_CUSTOM_TRUST_MANAGER)
88 .build(), threadPool, dataBroker, notificationPublishService);
90 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
91 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
92 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
93 Mockito.when(connection.isAlive()).thenReturn(true);
94 Mockito.when(connection.barrier(ArgumentMatchers.any()))
95 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
99 public void tearDown() throws InterruptedException {
105 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
106 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
107 * invoking onConnectionReady first, scenario:
109 * <li>send hello to device (rpc with void output)</li>
110 * <li>receive hello from device (notification)</li>
111 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
112 * <li>wait for rpc to finish with getFeatureOutput</li>
115 * @throws InterruptedException - interrupted exception
118 public void testOnSwitchConnected1() throws Exception {
119 connectionManagerImpl.onSwitchConnected(connection);
120 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
121 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
123 // prepare void reply (hello rpc output)
124 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
125 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
126 // prepare getFeature reply (getFeture rpc output)
127 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
128 SettableFuture.create();
129 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
133 connectionReadyListenerAC.getValue().onConnectionReady();
135 // deliver hello send output (void)
137 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
138 voidResponseFx.set(helloResponse);
140 //set dpn last connected time to be before dpn hold time seconds from now
141 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
142 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
145 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
146 ofpListenerAC.getValue().onHelloMessage(hello);
148 // deliver getFeature output
150 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
151 .setDatapathId(BigInteger.TEN)
152 .setVersion(OFConstants.OFP_VERSION_1_3)
154 .setTables((short) 15)
156 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
157 featureResponseFx.set(rpcFeaturesOutput);
159 Mockito.verify(deviceConnectedHandler,
160 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
165 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
166 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
167 * invoking onHelloMessage, scenario:
169 * <li>receive hello from device (notification)</li>
170 * <li>send hello to device (rpc with void output)</li>
171 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
172 * <li>wait for rpc to finish with getFeatureOutput</li>
175 * @throws InterruptedException - interrupted exception
178 public void testOnSwitchConnected2() throws Exception {
179 connectionManagerImpl.onSwitchConnected(connection);
180 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
181 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
183 // prepare void reply (hello rpc output)
184 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
185 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
186 // prepare getFeature reply (getFeture rpc output)
187 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
188 SettableFuture.create();
189 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
191 //set dpn last connected time to be before dpn hold time seconds from now
192 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
193 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
195 // fire handshake - send hello reply
196 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
197 ofpListenerAC.getValue().onHelloMessage(hello);
199 // notify about connection ready
200 connectionReadyListenerAC.getValue().onConnectionReady();
202 // deliver hello send output (void)
204 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
205 voidResponseFx.set(helloResponse);
207 // deliver getFeature output
209 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
210 .setDatapathId(BigInteger.TEN)
211 .setVersion(OFConstants.OFP_VERSION_1_3)
213 .setTables((short) 15)
215 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
216 featureResponseFx.set(rpcFeaturesOutput);
218 Mockito.verify(deviceConnectedHandler,
219 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));