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.openflowjava.protocol.api.util.EncodeConstants;
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;
49 import org.opendaylight.yangtools.yang.common.Uint16;
50 import org.opendaylight.yangtools.yang.common.Uint32;
51 import org.opendaylight.yangtools.yang.common.Uint64;
52 import org.opendaylight.yangtools.yang.common.Uint8;
55 * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
57 @RunWith(MockitoJUnitRunner.class)
58 public class ConnectionManagerImplTest {
60 // timeout of final step [ms]
61 private static final int FINAL_STEP_TIMEOUT = 500;
62 private ConnectionManagerImpl connectionManagerImpl;
64 private ConnectionAdapter connection;
66 private DeviceConnectedHandler deviceConnectedHandler;
68 private NotificationPublishService notificationPublishService;
70 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
72 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
74 DataBroker dataBroker;
76 private static final Uint32 ECHO_REPLY_TIMEOUT = Uint32.valueOf(500);
77 private static final Uint16 DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = Uint16.ZERO;
78 private static final Uint16 DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = Uint16.valueOf(60);
82 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
83 60L, TimeUnit.SECONDS,
84 new SynchronousQueue<>(), "ofppool");
86 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
87 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
88 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
89 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
90 .build(), threadPool, dataBroker, notificationPublishService);
92 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
93 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
94 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
95 Mockito.when(connection.isAlive()).thenReturn(true);
96 Mockito.when(connection.barrier(ArgumentMatchers.any()))
97 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
101 public void tearDown() throws InterruptedException {
107 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
108 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
109 * invoking onConnectionReady first, scenario:
111 * <li>send hello to device (rpc with void output)</li>
112 * <li>receive hello from device (notification)</li>
113 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
114 * <li>wait for rpc to finish with getFeatureOutput</li>
117 * @throws InterruptedException - interrupted exception
120 public void testOnSwitchConnected1() throws Exception {
121 connectionManagerImpl.onSwitchConnected(connection);
122 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
123 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
125 // prepare void reply (hello rpc output)
126 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
127 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
128 // prepare getFeature reply (getFeture rpc output)
129 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
130 SettableFuture.create();
131 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
135 connectionReadyListenerAC.getValue().onConnectionReady();
137 // deliver hello send output (void)
139 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
140 voidResponseFx.set(helloResponse);
142 //set dpn last connected time to be before dpn hold time seconds from now
143 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
144 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toJava()));
147 final HelloMessage hello = new HelloMessageBuilder()
148 .setVersion(EncodeConstants.OF_VERSION_1_3)
151 ofpListenerAC.getValue().onHelloMessage(hello);
153 // deliver getFeature output
155 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
156 .setDatapathId(Uint64.TEN)
157 .setVersion(EncodeConstants.OF_VERSION_1_3)
159 .setTables(Uint8.valueOf(15))
161 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
162 featureResponseFx.set(rpcFeaturesOutput);
164 Mockito.verify(deviceConnectedHandler,
165 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
170 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
171 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
172 * invoking onHelloMessage, scenario:
174 * <li>receive hello from device (notification)</li>
175 * <li>send hello to device (rpc with void output)</li>
176 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
177 * <li>wait for rpc to finish with getFeatureOutput</li>
180 * @throws InterruptedException - interrupted exception
183 public void testOnSwitchConnected2() throws Exception {
184 connectionManagerImpl.onSwitchConnected(connection);
185 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
186 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
188 // prepare void reply (hello rpc output)
189 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
190 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
191 // prepare getFeature reply (getFeture rpc output)
192 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
193 SettableFuture.create();
194 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
196 //set dpn last connected time to be before dpn hold time seconds from now
197 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
198 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toJava()));
200 // fire handshake - send hello reply
201 final HelloMessage hello = new HelloMessageBuilder()
202 .setVersion(EncodeConstants.OF_VERSION_1_3)
205 ofpListenerAC.getValue().onHelloMessage(hello);
207 // notify about connection ready
208 connectionReadyListenerAC.getValue().onConnectionReady();
210 // deliver hello send output (void)
212 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
213 voidResponseFx.set(helloResponse);
215 // deliver getFeature output
217 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
218 .setDatapathId(Uint64.TEN)
219 .setVersion(EncodeConstants.OF_VERSION_1_3)
221 .setTables(Uint8.valueOf(15))
223 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
224 featureResponseFx.set(rpcFeaturesOutput);
226 Mockito.verify(deviceConnectedHandler,
227 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));