Merge "Bump odlparent/yangtools/mdsal/controller"
[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 static org.mockito.ArgumentMatchers.any;
11
12 import com.google.common.util.concurrent.SettableFuture;
13 import java.math.BigInteger;
14 import java.net.InetSocketAddress;
15 import java.time.LocalDateTime;
16 import java.util.concurrent.SynchronousQueue;
17 import java.util.concurrent.TimeUnit;
18 import org.junit.After;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.ArgumentMatchers;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
30 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
31 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener;
32 import org.opendaylight.openflowplugin.api.OFConstants;
33 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
34 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
35 import org.opendaylight.openflowplugin.impl.util.ThreadPoolLoggingExecutor;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
49
50 /**
51  * Test of {@link ConnectionManagerImpl} - lightweight version, using basic ways (TDD).
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class ConnectionManagerImplTest {
55
56     // timeout of final step [ms]
57     private static final int FINAL_STEP_TIMEOUT = 500;
58     private ConnectionManagerImpl connectionManagerImpl;
59     @Mock
60     private ConnectionAdapter connection;
61     @Mock
62     private DeviceConnectedHandler deviceConnectedHandler;
63     @Mock
64     private NotificationPublishService notificationPublishService;
65     @Captor
66     private ArgumentCaptor<ConnectionReadyListener> connectionReadyListenerAC;
67     @Captor
68     private ArgumentCaptor<OpenflowProtocolListener> ofpListenerAC;
69     @Mock
70     DataBroker dataBroker;
71
72     private static final long ECHO_REPLY_TIMEOUT = 500;
73     private static final int DEVICE_CONNECTION_RATE_LIMIT_PER_MIN = 0;
74     private static final int DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS = 60;
75     private static final boolean ENABLE_CUSTOM_TRUST_MANAGER = false;
76
77     @Before
78     public void setUp() {
79         final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
80                 60L, TimeUnit.SECONDS,
81                 new SynchronousQueue<>(), "ofppool");
82
83         connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
84                 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
85                 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
86                 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
87                 .setEnableCustomTrustManager(ENABLE_CUSTOM_TRUST_MANAGER)
88                 .build(), threadPool, dataBroker, notificationPublishService);
89
90         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
91         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
92         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
93         Mockito.when(connection.isAlive()).thenReturn(true);
94         Mockito.when(connection.barrier(ArgumentMatchers.any()))
95                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
96     }
97
98     @After
99     public void tearDown() throws InterruptedException {
100         Thread.sleep(200L);
101     }
102
103     /**
104      * Test method for
105      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
106      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
107      * invoking onConnectionReady first, scenario:
108      * <ol>
109      * <li>send hello to device (rpc with void output)</li>
110      * <li>receive hello from device (notification)</li>
111      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
112      * <li>wait for rpc to finish with getFeatureOutput</li>
113      * </ol>
114      *
115      * @throws InterruptedException - interrupted exception
116      */
117     @Test
118     public void testOnSwitchConnected1() throws Exception {
119         connectionManagerImpl.onSwitchConnected(connection);
120         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
121         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
122
123         // prepare void reply (hello rpc output)
124         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
125         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
126         // prepare getFeature reply (getFeture rpc output)
127         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
128                 SettableFuture.create();
129         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
130
131
132         // fire handshake
133         connectionReadyListenerAC.getValue().onConnectionReady();
134
135         // deliver hello send output (void)
136         Thread.sleep(100L);
137         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
138         voidResponseFx.set(helloResponse);
139
140         //set dpn last connected time to be before dpn hold time seconds from now
141         connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
142                 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
143
144         // send hello reply
145         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
146         ofpListenerAC.getValue().onHelloMessage(hello);
147
148         // deliver getFeature output
149         Thread.sleep(100L);
150         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
151                 .setDatapathId(BigInteger.TEN)
152                 .setVersion(OFConstants.OFP_VERSION_1_3)
153                 .setXid(2L)
154                 .setTables((short) 15)
155                 .build();
156         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
157         featureResponseFx.set(rpcFeaturesOutput);
158
159         Mockito.verify(deviceConnectedHandler,
160                 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
161     }
162
163     /**
164      * Test method for
165      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
166      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
167      * invoking onHelloMessage, scenario:
168      * <ol>
169      * <li>receive hello from device (notification)</li>
170      * <li>send hello to device (rpc with void output)</li>
171      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
172      * <li>wait for rpc to finish with getFeatureOutput</li>
173      * </ol>
174      *
175      * @throws InterruptedException - interrupted exception
176      */
177     @Test
178     public void testOnSwitchConnected2() throws Exception {
179         connectionManagerImpl.onSwitchConnected(connection);
180         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
181         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
182
183         // prepare void reply (hello rpc output)
184         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
185         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
186         // prepare getFeature reply (getFeture rpc output)
187         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
188                 SettableFuture.create();
189         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
190
191         //set dpn last connected time to be before dpn hold time seconds from now
192         connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
193                 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
194
195         // fire handshake - send hello reply
196         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
197         ofpListenerAC.getValue().onHelloMessage(hello);
198
199         // notify about connection ready
200         connectionReadyListenerAC.getValue().onConnectionReady();
201
202         // deliver hello send output (void)
203         Thread.sleep(100L);
204         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
205         voidResponseFx.set(helloResponse);
206
207         // deliver getFeature output
208         Thread.sleep(100L);
209         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
210                 .setDatapathId(BigInteger.TEN)
211                 .setVersion(OFConstants.OFP_VERSION_1_3)
212                 .setXid(2L)
213                 .setTables((short) 15)
214                 .build();
215         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
216         featureResponseFx.set(rpcFeaturesOutput);
217
218         Mockito.verify(deviceConnectedHandler,
219                 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));
220     }
221 }