Merge "Connection context create device info"
[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 org.junit.After;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
25 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
26 import org.opendaylight.openflowplugin.api.OFConstants;
27 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
28 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40
41 /**
42  * test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD)
43  */
44 @RunWith(MockitoJUnitRunner.class)
45 public class ConnectionManagerImplTest {
46
47     /** timeout of final step [ms] */
48     private static final int FINAL_STEP_TIMEOUT = 500;
49     private ConnectionManagerImpl connectionManagerImpl;
50     @Mock
51     private ConnectionAdapter connection;
52     @Mock
53     private DeviceConnectedHandler deviceConnectedHandler;
54     @Captor
55     private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
56     @Captor
57     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
58
59     private final static int ECHO_REPLY_TIMEOUT = 500;
60
61     /**
62      * before each test method
63      */
64     @Before
65     public void setUp() {
66         connectionManagerImpl = new ConnectionManagerImpl(ECHO_REPLY_TIMEOUT);
67         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
68         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
69         final InetAddress inetAddress = deviceAddress.getAddress();
70         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
71         Mockito.when(connection.isAlive()).thenReturn(true);
72         Mockito.when(connection.barrier(Matchers.<BarrierInput>any()))
73                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
74 //        Mockito.when(connection.getRemoteAddress().getAddress()).thenReturn(inetAddress);
75     }
76
77     /**
78      * after each test method
79      * @throws InterruptedException
80      */
81     @After
82     public void tearDown() throws InterruptedException {
83         Thread.sleep(200L);
84     }
85
86     /**
87      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
88      * invoking onConnectionReady first, scenario:
89      * <ol>
90      *  <li>send hello to device (rpc with void output)</li>
91      *  <li>receive hello from device (notification)</li>
92      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
93      *  <li>wait for rpc to finish with getFeatureOutput</li>
94      * </ol>
95      * @throws InterruptedException
96      */
97     @Test
98     public void testOnSwitchConnected1() throws Exception {
99         connectionManagerImpl.onSwitchConnected(connection);
100         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
101         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
102
103         // prepare void reply (hello rpc output)
104         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
105         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
106         // prepare getFeature reply (getFeture rpc output)
107         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
108         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
109
110
111         // fire handshake
112         connectionReadyListenerAC.getValue().onConnectionReady();
113
114         // deliver hello send output (void)
115         Thread.sleep(100L);
116         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
117         voidResponseFx.set(helloResponse);
118
119         // send hello reply
120         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
121         ofpListenerAC.getValue().onHelloMessage(hello);
122
123         // deliver getFeature output
124         Thread.sleep(100L);
125         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
126         .setDatapathId(BigInteger.TEN)
127         .setVersion(OFConstants.OFP_VERSION_1_3)
128         .setXid(2L)
129         .setTables((short) 15)
130         .build();
131         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
132         featureResponseFx.set(rpcFeaturesOutput);
133
134         Mockito.verify(deviceConnectedHandler, Mockito.timeout(500)).deviceConnected(Matchers.any(ConnectionContext.class));
135     }
136
137     /**
138      * Test method for {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
139      * invoking onHelloMessage, scenario:
140      * <ol>
141      *  <li>receive hello from device (notification)</li>
142      *  <li>send hello to device (rpc with void output)</li>
143      *  <li>send getFeature to device (rpc with getFeatureOutput)</li>
144      *  <li>wait for rpc to finish with getFeatureOutput</li>
145      * </ol>
146      * @throws InterruptedException
147      */
148     @Test
149     public void testOnSwitchConnected2() throws Exception {
150         connectionManagerImpl.onSwitchConnected(connection);
151         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
152         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
153
154         // prepare void reply (hello rpc output)
155         final SettableFuture<RpcResult<Void>> voidResponseFx = SettableFuture.<RpcResult<Void>>create();
156         Mockito.when(connection.hello(Matchers.any(HelloInput.class))).thenReturn(voidResponseFx);
157         // prepare getFeature reply (getFeture rpc output)
158         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx = SettableFuture.<RpcResult<GetFeaturesOutput>>create();
159         Mockito.when(connection.getFeatures(Matchers.any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
160
161
162         // fire handshake - send hello reply
163         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
164         ofpListenerAC.getValue().onHelloMessage(hello);
165
166         // notify about connection ready
167         connectionReadyListenerAC.getValue().onConnectionReady();
168
169         // deliver hello send output (void)
170         Thread.sleep(100L);
171         final RpcResult<Void> helloResponse = RpcResultBuilder.success((Void) null).build();
172         voidResponseFx.set(helloResponse);
173
174         // deliver getFeature output
175         Thread.sleep(100L);
176         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
177         .setDatapathId(BigInteger.TEN)
178         .setVersion(OFConstants.OFP_VERSION_1_3)
179         .setXid(2L)
180         .setTables((short) 15)
181         .build();
182         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
183         featureResponseFx.set(rpcFeaturesOutput);
184
185         Mockito.verify(deviceConnectedHandler, Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(Matchers.any(ConnectionContext.class));
186     }
187 }