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 com.google.common.util.concurrent.SettableFuture;
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.net.InetSocketAddress;
14 import java.util.concurrent.SynchronousQueue;
15 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.Captor;
23 import org.mockito.Matchers;
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.openflow.md.core.ThreadPoolLoggingExecutor;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
48 @RunWith(MockitoJUnitRunner.class)
49 public class ConnectionManagerImplTest {
51 /** timeout of final step [ms] */
52 private static final int FINAL_STEP_TIMEOUT = 500;
53 private ConnectionManagerImpl connectionManagerImpl;
55 private ConnectionAdapter connection;
57 private DeviceConnectedHandler deviceConnectedHandler;
59 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
61 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
63 private final static int ECHO_REPLY_TIMEOUT = 500;
66 * before each test method
70 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
71 60L, TimeUnit.SECONDS,
72 new SynchronousQueue<>(), "ofppool");
74 connectionManagerImpl = new ConnectionManagerImpl(ECHO_REPLY_TIMEOUT, threadPool);
75 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
76 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
77 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
78 Mockito.when(connection.isAlive()).thenReturn(true);
79 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
80 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
84 * after each test method
85 * @throws InterruptedException
88 public void tearDown() throws InterruptedException {
93 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
94 * invoking onConnectionReady first, scenario:
96 * <li>send hello to device (rpc with void output)</li>
97 * <li>receive hello from device (notification)</li>
98 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
99 * <li>wait for rpc to finish with getFeatureOutput</li>
101 * @throws InterruptedException
104 public void testOnSwitchConnected1() throws Exception {
105 connectionManagerImpl.onSwitchConnected(connection);
106 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
107 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
109 // prepare void reply (hello rpc output)
110 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
111 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
112 // prepare getFeature reply (getFeture rpc output)
113 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
114 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
118 connectionReadyListenerAC.getValue().onConnectionReady();
120 // deliver hello send output (void)
122 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
123 voidResponseFx.set(helloResponse);
126 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
127 ofpListenerAC.getValue().onHelloMessage(hello);
129 // deliver getFeature output
131 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
132 .setDatapathId(BigInteger.TEN)
133 .setVersion(OFConstants.OFP_VERSION_1_3)
135 .setTables((short) 15)
137 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
138 featureResponseFx.set(rpcFeaturesOutput);
140 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
144 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
145 * invoking onHelloMessage, scenario:
147 * <li>receive hello from device (notification)</li>
148 * <li>send hello to device (rpc with void output)</li>
149 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
150 * <li>wait for rpc to finish with getFeatureOutput</li>
152 * @throws InterruptedException
155 public void testOnSwitchConnected2() throws Exception {
156 connectionManagerImpl.onSwitchConnected(connection);
157 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
158 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
160 // prepare void reply (hello rpc output)
161 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
162 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
163 // prepare getFeature reply (getFeture rpc output)
164 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
165 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
168 // fire handshake - send hello reply
169 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
170 ofpListenerAC.getValue().onHelloMessage(hello);
172 // notify about connection ready
173 connectionReadyListenerAC.getValue().onConnectionReady();
175 // deliver hello send output (void)
177 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
178 voidResponseFx.set(helloResponse);
180 // deliver getFeature output
182 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
183 .setDatapathId(BigInteger.TEN)
184 .setVersion(OFConstants.OFP_VERSION_1_3)
186 .setTables((short) 15)
188 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
189 featureResponseFx.set(rpcFeaturesOutput);
191 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));