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;
78 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
79 60L, TimeUnit.SECONDS,
80 new SynchronousQueue<>(), "ofppool");
82 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
83 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
84 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
85 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
86 .build(), threadPool, dataBroker, notificationPublishService);
88 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
89 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
90 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
91 Mockito.when(connection.isAlive()).thenReturn(true);
92 Mockito.when(connection.barrier(ArgumentMatchers.any()))
93 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
97 public void tearDown() throws InterruptedException {
103 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
104 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
105 * invoking onConnectionReady first, scenario:
107 * <li>send hello to device (rpc with void output)</li>
108 * <li>receive hello from device (notification)</li>
109 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
110 * <li>wait for rpc to finish with getFeatureOutput</li>
113 * @throws InterruptedException - interrupted exception
116 public void testOnSwitchConnected1() throws Exception {
117 connectionManagerImpl.onSwitchConnected(connection);
118 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
119 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
121 // prepare void reply (hello rpc output)
122 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
123 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
124 // prepare getFeature reply (getFeture rpc output)
125 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
126 SettableFuture.create();
127 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
131 connectionReadyListenerAC.getValue().onConnectionReady();
133 // deliver hello send output (void)
135 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
136 voidResponseFx.set(helloResponse);
138 //set dpn last connected time to be before dpn hold time seconds from now
139 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
140 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
143 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
144 ofpListenerAC.getValue().onHelloMessage(hello);
146 // deliver getFeature output
148 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
149 .setDatapathId(BigInteger.TEN)
150 .setVersion(OFConstants.OFP_VERSION_1_3)
152 .setTables((short) 15)
154 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
155 featureResponseFx.set(rpcFeaturesOutput);
157 Mockito.verify(deviceConnectedHandler,
158 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
163 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
164 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
165 * invoking onHelloMessage, scenario:
167 * <li>receive hello from device (notification)</li>
168 * <li>send hello to device (rpc with void output)</li>
169 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
170 * <li>wait for rpc to finish with getFeatureOutput</li>
173 * @throws InterruptedException - interrupted exception
176 public void testOnSwitchConnected2() throws Exception {
177 connectionManagerImpl.onSwitchConnected(connection);
178 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
179 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
181 // prepare void reply (hello rpc output)
182 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
183 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
184 // prepare getFeature reply (getFeture rpc output)
185 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
186 SettableFuture.create();
187 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
189 //set dpn last connected time to be before dpn hold time seconds from now
190 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
191 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
193 // fire handshake - send hello reply
194 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
195 ofpListenerAC.getValue().onHelloMessage(hello);
197 // notify about connection ready
198 connectionReadyListenerAC.getValue().onConnectionReady();
200 // deliver hello send output (void)
202 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
203 voidResponseFx.set(helloResponse);
205 // deliver getFeature output
207 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
208 .setDatapathId(BigInteger.TEN)
209 .setVersion(OFConstants.OFP_VERSION_1_3)
211 .setTables((short) 15)
213 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
214 featureResponseFx.set(rpcFeaturesOutput);
216 Mockito.verify(deviceConnectedHandler,
217 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));