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