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