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.InetSocketAddress;
13 import java.util.concurrent.SynchronousQueue;
14 import java.util.concurrent.TimeUnit;
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
26 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
27 import org.opendaylight.openflowplugin.api.OFConstants;
28 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
29 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
30 import org.opendaylight.openflowplugin.openflow.md.core.ThreadPoolLoggingExecutor;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
46 @RunWith(MockitoJUnitRunner.class)
47 public class ConnectionManagerImplTest {
49 /** timeout of final step [ms] */
50 private static final int FINAL_STEP_TIMEOUT = 500;
51 private ConnectionManagerImpl connectionManagerImpl;
53 private ConnectionAdapter connection;
55 private DeviceConnectedHandler deviceConnectedHandler;
57 private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
59 private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
61 private final static int ECHO_REPLY_TIMEOUT = 500;
64 * before each test method
68 final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
69 60L, TimeUnit.SECONDS,
70 new SynchronousQueue<>(), "ofppool");
72 connectionManagerImpl = new ConnectionManagerImpl(threadPool);
73 connectionManagerImpl.setEchoReplyTimeout(ECHO_REPLY_TIMEOUT);
74 connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
75 final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
76 Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
77 Mockito.when(connection.isAlive()).thenReturn(true);
78 Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
79 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
83 * after each test method
84 * @throws InterruptedException
87 public void tearDown() throws InterruptedException {
92 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
93 * invoking onConnectionReady first, scenario:
95 * <li>send hello to device (rpc with void output)</li>
96 * <li>receive hello from device (notification)</li>
97 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
98 * <li>wait for rpc to finish with getFeatureOutput</li>
100 * @throws InterruptedException
103 public void testOnSwitchConnected1() throws Exception {
104 connectionManagerImpl.onSwitchConnected(connection);
105 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
106 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
108 // prepare void reply (hello rpc output)
109 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
110 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
111 // prepare getFeature reply (getFeture rpc output)
112 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
113 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
117 connectionReadyListenerAC.getValue().onConnectionReady();
119 // deliver hello send output (void)
121 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
122 voidResponseFx.set(helloResponse);
125 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
126 ofpListenerAC.getValue().onHelloMessage(hello);
128 // deliver getFeature output
130 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
131 .setDatapathId(BigInteger.TEN)
132 .setVersion(OFConstants.OFP_VERSION_1_3)
134 .setTables((short) 15)
136 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
137 featureResponseFx.set(rpcFeaturesOutput);
139 Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
143 * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
144 * invoking onHelloMessage, scenario:
146 * <li>receive hello from device (notification)</li>
147 * <li>send hello to device (rpc with void output)</li>
148 * <li>send getFeature to device (rpc with getFeatureOutput)</li>
149 * <li>wait for rpc to finish with getFeatureOutput</li>
151 * @throws InterruptedException
154 public void testOnSwitchConnected2() throws Exception {
155 connectionManagerImpl.onSwitchConnected(connection);
156 Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
157 Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
159 // prepare void reply (hello rpc output)
160 final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
161 Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
162 // prepare getFeature reply (getFeture rpc output)
163 final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
164 Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
167 // fire handshake - send hello reply
168 final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
169 ofpListenerAC.getValue().onHelloMessage(hello);
171 // notify about connection ready
172 connectionReadyListenerAC.getValue().onConnectionReady();
174 // deliver hello send output (void)
176 final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
177 voidResponseFx.set(helloResponse);
179 // deliver getFeature output
181 final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
182 .setDatapathId(BigInteger.TEN)
183 .setVersion(OFConstants.OFP_VERSION_1_3)
185 .setTables((short) 15)
187 final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
188 featureResponseFx.set(rpcFeaturesOutput);
190 Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));