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);
79 private static final boolean ENABLE_CUSTOM_TRUST_MANAGER = false;
83 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
84 60L, TimeUnit.SECONDS,
85 new SynchronousQueue<>(), "ofppool");
87 connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
88 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
89 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
90 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
91 .setEnableCustomTrustManager(ENABLE_CUSTOM_TRUST_MANAGER)
92 .build(), threadPool, dataBroker, notificationPublishService);
94 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
95 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
96 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
97 Mockito.when(connection.isAlive()).thenReturn(true);
98 Mockito.when(connection.barrier(ArgumentMatchers.any()))
99 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
103 public void tearDown() throws InterruptedException {
109 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
110 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
111 * invoking onConnectionReady first, scenario:
113 * <li>send hello to device (rpc with void output)</li>
114 * <li>receive hello from device (notification)</li>
115 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
116 * <li>wait for rpc to finish with getFeatureOutput</li>
119 * @throws InterruptedException - interrupted exception
122 public void testOnSwitchConnected1() throws Exception {
123 connectionManagerImpl.onSwitchConnected(connection);
124 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
125 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
127 // prepare void reply (hello rpc output)
128 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
129 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
130 // prepare getFeature reply (getFeture rpc output)
131 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
132 SettableFuture.create();
133 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
137 connectionReadyListenerAC.getValue().onConnectionReady();
139 // deliver hello send output (void)
141 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
142 voidResponseFx.set(helloResponse);
144 //set dpn last connected time to be before dpn hold time seconds from now
145 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
146 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toJava()));
149 final HelloMessage hello = new HelloMessageBuilder()
150 .setVersion(EncodeConstants.OF_VERSION_1_3)
153 ofpListenerAC.getValue().onHelloMessage(hello);
155 // deliver getFeature output
157 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
158 .setDatapathId(Uint64.TEN)
159 .setVersion(EncodeConstants.OF_VERSION_1_3)
161 .setTables(Uint8.valueOf(15))
163 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
164 featureResponseFx.set(rpcFeaturesOutput);
166 Mockito.verify(deviceConnectedHandler,
167 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
172 * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
173 * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
174 * invoking onHelloMessage, scenario:
176 * <li>receive hello from device (notification)</li>
177 * <li>send hello to device (rpc with void output)</li>
178 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
179 * <li>wait for rpc to finish with getFeatureOutput</li>
182 * @throws InterruptedException - interrupted exception
185 public void testOnSwitchConnected2() throws Exception {
186 connectionManagerImpl.onSwitchConnected(connection);
187 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
188 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
190 // prepare void reply (hello rpc output)
191 final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
192 Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
193 // prepare getFeature reply (getFeture rpc output)
194 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
195 SettableFuture.create();
196 Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
198 //set dpn last connected time to be before dpn hold time seconds from now
199 connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
200 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS.toJava()));
202 // fire handshake - send hello reply
203 final HelloMessage hello = new HelloMessageBuilder()
204 .setVersion(EncodeConstants.OF_VERSION_1_3)
207 ofpListenerAC.getValue().onHelloMessage(hello);
209 // notify about connection ready
210 connectionReadyListenerAC.getValue().onConnectionReady();
212 // deliver hello send output (void)
214 final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
215 voidResponseFx.set(helloResponse);
217 // deliver getFeature output
219 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
220 .setDatapathId(Uint64.TEN)
221 .setVersion(EncodeConstants.OF_VERSION_1_3)
223 .setTables(Uint8.valueOf(15))
225 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
226 featureResponseFx.set(rpcFeaturesOutput);
228 Mockito.verify(deviceConnectedHandler,
229 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));