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