09703be1bb471bfddd77d1687e51f3a56d7cddc4
[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
76     @Before
77     public void setUp() {
78         final ThreadPoolLoggingExecutor threadPool = new ThreadPoolLoggingExecutor(0, Integer.MAX_VALUE,
79                 60L, TimeUnit.SECONDS,
80                 new SynchronousQueue<>(), "ofppool");
81
82         connectionManagerImpl = new ConnectionManagerImpl(new OpenflowProviderConfigBuilder()
83                 .setEchoReplyTimeout(new NonZeroUint32Type(ECHO_REPLY_TIMEOUT))
84                 .setDeviceConnectionRateLimitPerMin(DEVICE_CONNECTION_RATE_LIMIT_PER_MIN)
85                 .setDeviceConnectionHoldTimeInSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS)
86                 .build(), threadPool, dataBroker, notificationPublishService);
87
88         connectionManagerImpl.setDeviceConnectedHandler(deviceConnectedHandler);
89         final InetSocketAddress deviceAddress = InetSocketAddress.createUnresolved("yahoo", 42);
90         Mockito.when(connection.getRemoteAddress()).thenReturn(deviceAddress);
91         Mockito.when(connection.isAlive()).thenReturn(true);
92         Mockito.when(connection.barrier(ArgumentMatchers.any()))
93                 .thenReturn(RpcResultBuilder.success(new BarrierOutputBuilder().build()).buildFuture());
94     }
95
96     @After
97     public void tearDown() throws InterruptedException {
98         Thread.sleep(200L);
99     }
100
101     /**
102      * Test method for
103      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
104      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
105      * invoking onConnectionReady first, scenario:
106      * <ol>
107      * <li>send hello to device (rpc with void output)</li>
108      * <li>receive hello from device (notification)</li>
109      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
110      * <li>wait for rpc to finish with getFeatureOutput</li>
111      * </ol>
112      *
113      * @throws InterruptedException - interrupted exception
114      */
115     @Test
116     public void testOnSwitchConnected1() throws Exception {
117         connectionManagerImpl.onSwitchConnected(connection);
118         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
119         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
120
121         // prepare void reply (hello rpc output)
122         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
123         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
124         // prepare getFeature reply (getFeture rpc output)
125         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
126                 SettableFuture.create();
127         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
128
129
130         // fire handshake
131         connectionReadyListenerAC.getValue().onConnectionReady();
132
133         // deliver hello send output (void)
134         Thread.sleep(100L);
135         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
136         voidResponseFx.set(helloResponse);
137
138         //set dpn last connected time to be before dpn hold time seconds from now
139         connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
140                 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
141
142         // send hello reply
143         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
144         ofpListenerAC.getValue().onHelloMessage(hello);
145
146         // deliver getFeature output
147         Thread.sleep(100L);
148         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
149                 .setDatapathId(BigInteger.TEN)
150                 .setVersion(OFConstants.OFP_VERSION_1_3)
151                 .setXid(2L)
152                 .setTables((short) 15)
153                 .build();
154         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
155         featureResponseFx.set(rpcFeaturesOutput);
156
157         Mockito.verify(deviceConnectedHandler,
158                 Mockito.timeout(500)).deviceConnected(any(ConnectionContext.class));
159     }
160
161     /**
162      * Test method for
163      * {@link org.opendaylight.openflowplugin.impl.connection.ConnectionManagerImpl#onSwitchConnected(
164      * org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter)}.
165      * invoking onHelloMessage, scenario:
166      * <ol>
167      * <li>receive hello from device (notification)</li>
168      * <li>send hello to device (rpc with void output)</li>
169      * <li>send getFeature to device (rpc with getFeatureOutput)</li>
170      * <li>wait for rpc to finish with getFeatureOutput</li>
171      * </ol>
172      *
173      * @throws InterruptedException - interrupted exception
174      */
175     @Test
176     public void testOnSwitchConnected2() throws Exception {
177         connectionManagerImpl.onSwitchConnected(connection);
178         Mockito.verify(connection).setConnectionReadyListener(connectionReadyListenerAC.capture());
179         Mockito.verify(connection).setMessageListener(ofpListenerAC.capture());
180
181         // prepare void reply (hello rpc output)
182         final SettableFuture<RpcResult<HelloOutput>> voidResponseFx = SettableFuture.create();
183         Mockito.when(connection.hello(any(HelloInput.class))).thenReturn(voidResponseFx);
184         // prepare getFeature reply (getFeture rpc output)
185         final SettableFuture<RpcResult<GetFeaturesOutput>> featureResponseFx =
186                 SettableFuture.create();
187         Mockito.when(connection.getFeatures(any(GetFeaturesInput.class))).thenReturn(featureResponseFx);
188
189         //set dpn last connected time to be before dpn hold time seconds from now
190         connectionManagerImpl.getDeviceConnectionStatusProvider().addDeviceLastConnectionTime(BigInteger.TEN,
191                 LocalDateTime.now().minusSeconds(DEVICE_CONNECTION_HOLD_TIME_IN_SECONDS));
192
193         // fire handshake - send hello reply
194         final HelloMessage hello = new HelloMessageBuilder().setVersion(OFConstants.OFP_VERSION_1_3).setXid(1L).build();
195         ofpListenerAC.getValue().onHelloMessage(hello);
196
197         // notify about connection ready
198         connectionReadyListenerAC.getValue().onConnectionReady();
199
200         // deliver hello send output (void)
201         Thread.sleep(100L);
202         final RpcResult<HelloOutput> helloResponse = RpcResultBuilder.success((HelloOutput) null).build();
203         voidResponseFx.set(helloResponse);
204
205         // deliver getFeature output
206         Thread.sleep(100L);
207         final GetFeaturesOutput getFeatureOutput = new GetFeaturesOutputBuilder()
208                 .setDatapathId(BigInteger.TEN)
209                 .setVersion(OFConstants.OFP_VERSION_1_3)
210                 .setXid(2L)
211                 .setTables((short) 15)
212                 .build();
213         final RpcResult<GetFeaturesOutput> rpcFeaturesOutput = RpcResultBuilder.success(getFeatureOutput).build();
214         featureResponseFx.set(rpcFeaturesOutput);
215
216         Mockito.verify(deviceConnectedHandler,
217                 Mockito.timeout(FINAL_STEP_TIMEOUT)).deviceConnected(any(ConnectionContext.class));
218     }
219 }