Fix checkstyle warnings for rpc package
[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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
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 long 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(new OpenflowProviderConfigBuilder()
75                 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
76                 .build(), threadPool);
77
78         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
79         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
80         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
81         Mockito.when(connection.isAlive()).thenReturn(true);
82         Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
83                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
84     }
85
86     /**
87      * after each test method
88      * @throws InterruptedException
89      */
90     @After
91     public void tearDown() throws InterruptedException {
92         Thread.sleep(200L);
93     }
94
95     /**
96      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
97      * invoking onConnectionReady first, scenario:
98      * <ol>
99      *  <li>send hello to device (rpc with void output)</li>
100      *  <li>receive hello from device (notification)</li>
101      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
102      *  <li>wait for rpc to finish with getFeatureOutput</li>
103      * </ol>
104      * @throws InterruptedException
105      */
106     @Test
107     public void testOnSwitchConnected1() throws Exception {
108         connectionManagerImpl.onSwitchConnected(connection);
109         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
110         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
111
112         // prepare void reply (hello rpc output)
113         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
114         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
115         // prepare getFeature reply (getFeture rpc output)
116         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
117         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
118
119
120         // fire handshake
121         connectionReadyListenerAC.getValue().onConnectionReady();
122
123         // deliver hello send output (void)
124         Thread.sleep(100L);
125         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
126         voidResponseFx.set(helloResponse);
127
128         // send hello reply
129         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
130         ofpListenerAC.getValue().onHelloMessage(hello);
131
132         // deliver getFeature output
133         Thread.sleep(100L);
134         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
135         .setDatapathId(BigInteger.TEN)
136         .setVersion(OFConstants.OFP_VERSION_1_3)
137         .setXid(2L)
138         .setTables((short) 15)
139         .build();
140         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
141         featureResponseFx.set(rpcFeaturesOutput);
142
143         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
144     }
145
146     /**
147      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
148      * invoking onHelloMessage, scenario:
149      * <ol>
150      *  <li>receive hello from device (notification)</li>
151      *  <li>send hello to device (rpc with void output)</li>
152      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
153      *  <li>wait for rpc to finish with getFeatureOutput</li>
154      * </ol>
155      * @throws InterruptedException
156      */
157     @Test
158     public void testOnSwitchConnected2() throws Exception {
159         connectionManagerImpl.onSwitchConnected(connection);
160         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
161         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
162
163         // prepare void reply (hello rpc output)
164         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
165         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
166         // prepare getFeature reply (getFeture rpc output)
167         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
168         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
169
170
171         // fire handshake - send hello reply
172         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
173         ofpListenerAC.getValue().onHelloMessage(hello);
174
175         // notify about connection ready
176         connectionReadyListenerAC.getValue().onConnectionReady();
177
178         // deliver hello send output (void)
179         Thread.sleep(100L);
180         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
181         voidResponseFx.set(helloResponse);
182
183         // deliver getFeature output
184         Thread.sleep(100L);
185         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
186         .setDatapathId(BigInteger.TEN)
187         .setVersion(OFConstants.OFP_VERSION_1_3)
188         .setXid(2L)
189         .setTables((short) 15)
190         .build();
191         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
192         featureResponseFx.set(rpcFeaturesOutput);
193
194         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
195     }
196 }