001df46d8d2ba3871af1118f83d4d06154405341
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / connection / ConnectionManagerImplTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.connection;
9
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;
42
43 /**
44  * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
45  */
46 @RunWith(MockitoJUnitRunner.class)
47 public class ConnectionManagerImplTest {
48
49     /** timeout of final step [ms] */
50     private static final int FINAL_STEP_TIMEOUT = 500;
51     private ConnectionManagerImpl connectionManagerImpl;
52     @Mock
53     private ConnectionAdapter connection;
54     @Mock
55     private DeviceConnectedHandler deviceConnectedHandler;
56     @Captor
57     private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
58     @Captor
59     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
60
61     private final static int ECHO_REPLY_TIMEOUT = 500;
62
63     /**
64      * before each test method
65      */
66     @Before
67     public void setUp() {
68         final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
69                 60L, TimeUnit.SECONDS,
70                 new SynchronousQueue<>(), "ofppool");
71
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());
80     }
81
82     /**
83      * after each test method
84      * @throws InterruptedException
85      */
86     @After
87     public void tearDown() throws InterruptedException {
88         Thread.sleep(200L);
89     }
90
91     /**
92      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
93      * invoking onConnectionReady first, scenario:
94      * <ol>
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>
99      * </ol>
100      * @throws InterruptedException
101      */
102     @Test
103     public void testOnSwitchConnected1() throws Exception {
104         connectionManagerImpl.onSwitchConnected(connection);
105         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
106         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
107
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);
114
115
116         // fire handshake
117         connectionReadyListenerAC.getValue().onConnectionReady();
118
119         // deliver hello send output (void)
120         Thread.sleep(100L);
121         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
122         voidResponseFx.set(helloResponse);
123
124         // send hello reply
125         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
126         ofpListenerAC.getValue().onHelloMessage(hello);
127
128         // deliver getFeature output
129         Thread.sleep(100L);
130         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
131         .setDatapathId(BigInteger.TEN)
132         .setVersion(OFConstants.OFP_VERSION_1_3)
133         .setXid(2L)
134         .setTables((short) 15)
135         .build();
136         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
137         featureResponseFx.set(rpcFeaturesOutput);
138
139         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
140     }
141
142     /**
143      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
144      * invoking onHelloMessage, scenario:
145      * <ol>
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>
150      * </ol>
151      * @throws InterruptedException
152      */
153     @Test
154     public void testOnSwitchConnected2() throws Exception {
155         connectionManagerImpl.onSwitchConnected(connection);
156         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
157         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
158
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);
165
166
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);
170
171         // notify about connection ready
172         connectionReadyListenerAC.getValue().onConnectionReady();
173
174         // deliver hello send output (void)
175         Thread.sleep(100L);
176         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
177         voidResponseFx.set(helloResponse);
178
179         // deliver getFeature output
180         Thread.sleep(100L);
181         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
182         .setDatapathId(BigInteger.TEN)
183         .setVersion(OFConstants.OFP_VERSION_1_3)
184         .setXid(2L)
185         .setTables((short) 15)
186         .build();
187         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
188         featureResponseFx.set(rpcFeaturesOutput);
189
190         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
191     }
192 }