Add option to use netdev datapath_type
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / BridgeConfigurationManagerImplTest.java
1 /*
2 * Copyright (c) 2014 Intel Corp. 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.ovsdb.openstack.netvirt.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyList;
16 import static org.mockito.Matchers.anyString;
17 import static org.mockito.Matchers.eq;
18 import static org.mockito.Mockito.RETURNS_MOCKS;
19 import static org.mockito.Mockito.doAnswer;
20 import static org.mockito.Mockito.doReturn;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.reset;
23 import static org.mockito.Mockito.times;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.mockito.Mockito.when;
27
28 import java.lang.reflect.Field;
29 import java.util.List;
30
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
38 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
39 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
40 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
42 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
43 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48
49 import org.osgi.framework.ServiceReference;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.core.classloader.annotations.PrepareForTest;
52 import org.powermock.modules.junit4.PowerMockRunner;
53
54 /**
55  * Test class for BridgeConfigurationManagerImpl
56  *
57  * @author Marcus Koontz
58  * @author Alexis de Talhouet
59  * @author Sam Hague (shague@redhat.com)
60  */
61 @RunWith(PowerMockRunner.class)
62 @PrepareForTest({ServiceHelper.class, ConfigProperties.class})
63 public class BridgeConfigurationManagerImplTest {
64     @Mock private Node node;
65     @Mock private OvsdbBridgeAugmentation bridge;
66     @Mock private OvsdbTerminationPointAugmentation port;
67     @Mock private NeutronNetwork neutronNetwork;
68     @Mock private ConfigurationService configurationService;
69     @Mock private Southbound southbound;
70     @InjectMocks public static BridgeConfigurationManagerImpl bridgeConfigurationManagerImpl;
71
72     private static final String ADDRESS = "127.0.0.1";
73     private static final String BR_INT = "br-int";
74     private static final String ETH1 = "eth1";
75     private static final String ETH2 = "eth2";
76     private static final String ETH3 = "eth3";
77     private static final String PORT_BR_INT = "br-int";
78     private static final String BRIDGE_UUID = "f527b951-3934-4182-9f29-33fc09f6f0c6";
79     private static final String PHYSNET1 = "physnet1";
80     private static final String PHYSNET2 = "physnet2";
81     private static final String PHYSNET3 = "physnet3";
82     private static final String PROVIDER_MAPPINGS = PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2;
83     private static final String PROVIDER_MAPPINGS_DEFAULT = PHYSNET1 + ":" + ETH1;
84
85     @Test
86     public void testGetBridgeUuid() {
87         when(southbound.getBridgeUuid(any(Node.class), anyString()))
88                 .thenReturn(null)
89                 .thenReturn(BRIDGE_UUID);
90
91         assertEquals("Error, null should have been returned", null,
92                 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
93         assertEquals("Error, did not return UUID of correct bridge", BRIDGE_UUID,
94                 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
95         verify(southbound, times(2)).getBridgeUuid(any(Node.class), anyString());
96     }
97
98     @Test
99     public void testIsNodeNeutronReady() throws Exception {
100         when(southbound.getBridge(any(Node.class), anyString()))
101                 .thenReturn(null)
102                 .thenReturn(bridge);
103
104         verifyNoMoreInteractions(configurationService);
105         assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
106                 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
107
108         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
109         assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
110                 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
111
112         verify(configurationService, times(2)).getIntegrationBridgeName();
113         verify(southbound, times(2)).getBridge(any(Node.class), anyString());
114     }
115
116     @Test
117     public void testIsNodeOverlayReady() throws Exception {
118         when(southbound.getBridge(any(Node.class), anyString()))
119                 .thenReturn(null)
120                 .thenReturn(bridge);
121
122         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
123                 PowerMockito.spy(new BridgeConfigurationManagerImpl());
124         doReturn(false).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
125         bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
126         bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
127
128         verifyNoMoreInteractions(configurationService);
129
130         assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
131                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
132
133         doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
134
135         assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
136                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
137
138         assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
139                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
140
141         verify(configurationService, times(2)).getNetworkBridgeName();
142         verify(southbound, times(2)).getBridge(any(Node.class), anyString());
143     }
144
145     @Test
146     public void testIsPortOnBridge() throws Exception {
147         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
148                 .thenReturn(null)
149                 .thenReturn(port);
150
151         assertFalse("Error, port " + PORT_BR_INT + " should not be found",
152                 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
153         assertTrue("Error, port " + PORT_BR_INT + " should be found",
154                 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
155         verify(southbound, times(2)).extractTerminationPointAugmentation(any(Node.class), anyString());
156     }
157
158     @Test
159     public void testIsNodeTunnelReady() throws Exception {
160         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
161                 .thenReturn(false)
162                 .thenReturn(true);
163
164         verifyNoMoreInteractions(configurationService);
165         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
166                 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
167
168         when(configurationService.isL3ForwardingEnabled()).thenReturn(false);
169         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
170         assertTrue("Error, did not return correct boolean from isNodeTunnelReady",
171                 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
172
173         verify(configurationService, times(1)).isL3ForwardingEnabled();
174         verify(configurationService, times(3)).getIntegrationBridgeName();
175         verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
176     }
177
178     @Test
179     public void testIsNodeVlanReady() throws Exception {
180         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
181                 .thenReturn(false)
182                 .thenReturn(true);
183
184         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
185                 .thenReturn(null)
186                 .thenReturn(port);
187
188         when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
189
190         verifyNoMoreInteractions(configurationService);
191         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
192                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
193
194         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
195                 PowerMockito.spy(new BridgeConfigurationManagerImpl());
196         doReturn(ETH1).when(bridgeConfigurationManagerImplSpy).getPhysicalInterfaceName(any(Node.class), anyString());
197         bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
198         bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
199
200         assertFalse("Error, did not return correct boolean from isNodeVlanReady",
201                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
202
203         assertTrue("Error, did not return correct boolean from isNodeVlanReady",
204                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
205
206         verify(configurationService, times(3)).getIntegrationBridgeName();
207         verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
208     }
209
210     @Test
211     public void testIsNodeL3Ready() {
212         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(true);
213         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString())).thenReturn(mock(OvsdbTerminationPointAugmentation.class));
214         when(southbound.readBridgeNode(any(Node.class), anyString())).thenReturn(mock(Node.class));
215
216         assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
217     }
218
219     @Test
220     public void testPrepareNode() {
221         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
222         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
223
224         PowerMockito.mockStatic(ConfigProperties.class);
225         when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
226
227         when(southbound.addBridge(any(Node.class), anyString(), anyList(), eq(DatapathTypeSystem.class))).thenReturn(true);
228         when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
229
230         bridgeConfigurationManagerImpl.prepareNode(node);
231     }
232
233     @Test
234     public void testCreateLocalNetwork() throws Exception {
235         NeutronNetwork neutronNetworkMock = mock(NeutronNetwork.class, RETURNS_MOCKS);
236         String networkTypes[] = {"vlan", "vxlan", "gre"};
237         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
238                 PowerMockito.spy(new BridgeConfigurationManagerImpl());
239         bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
240         bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
241
242         for (String networkType : networkTypes) {
243             when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
244             when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
245
246             doAnswer(new Answer<Boolean>() {
247                 @Override
248                 public Boolean answer(InvocationOnMock invocation) {
249                     return Boolean.TRUE;
250                 }
251             }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
252
253             doAnswer(new Answer<Boolean>() {
254                 @Override
255                 public Boolean answer(InvocationOnMock invocation) {
256                     return Boolean.TRUE;
257                 }
258             }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
259
260             assertTrue("bridgeConfigMock.isNodeVlanReady is not true",
261                     bridgeConfigurationManagerImplSpy.isNodeVlanReady(node, node, neutronNetworkMock));
262             assertTrue("bridgeConfigMock.isNodeTunnelReady is not true",
263                     bridgeConfigurationManagerImplSpy.isNodeTunnelReady(node, node));
264
265             assertTrue("Error, isCreated is not true for " + networkType,
266                     bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
267             switch (networkType) {
268                 case "vlan":
269                     verify(neutronNetworkMock, times(1)).getProviderNetworkType();
270                     break;
271                 case "vxlan":
272                     verify(neutronNetworkMock, times(2)).getProviderNetworkType();
273                     break;
274                 case "gre":
275                     verify(neutronNetworkMock, times(3)).getProviderNetworkType();
276                     break;
277             }
278             reset(neutronNetworkMock);
279             reset(node);
280             reset(bridgeConfigurationManagerImplSpy);
281         }
282     }
283
284     @Test
285     public void testGetPhysicalInterfaceName() throws Exception {
286         when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
287                 .thenReturn(null)
288                 .thenReturn(null)
289                 .thenReturn(PROVIDER_MAPPINGS);
290         String networkNames[] = {PHYSNET1, PHYSNET2};
291         String interfaceNames[] = {ETH1, ETH2};
292
293         verifyNoMoreInteractions(configurationService);
294         when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
295
296         assertNull("Error, should not have found " + PHYSNET2 + ":" + ETH2,
297                 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET2));
298         assertEquals("Error, should have found " + PHYSNET1 + ":" + ETH1,
299                 ETH1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET1));
300         for (int i = 0; i < networkNames.length; i++) {
301             assertEquals("Error, network: " + networkNames[i]
302                             + ", did not match interface: "+ interfaceNames[i],
303                     interfaceNames[i],
304                     bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
305         }
306         assertNull(PHYSNET1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET3));
307         verify(configurationService, times(5)).getProviderMappingsKey();
308         verify(configurationService, times(2)).getDefaultProviderMapping();
309         verify(southbound, times(5)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
310     }
311
312     @Test
313     public void testGetAllPhysicalInterfaceNames() throws Exception {
314         when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
315                 .thenReturn(null)
316                 .thenReturn(PROVIDER_MAPPINGS);
317
318         verifyNoMoreInteractions(configurationService);
319         when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
320
321         List<String> interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
322         assertEquals("Error, should have found 1 interface", 1, interfaces.size());
323         assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
324         assertFalse("Error, should not have found " + ETH2, interfaces.contains(ETH2));
325         interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
326         assertEquals("Error, should have found 2 interfaces", 2, interfaces.size());
327         assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
328         assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH2));
329         assertFalse("Error, should not have found " + ETH3, interfaces.contains(ETH3));
330
331         verify(configurationService, times(2)).getProviderMappingsKey();
332         verify(configurationService, times(1)).getDefaultProviderMapping();
333         verify(southbound, times(2)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
334     }
335
336     @Test
337     public void testSetDependencies() throws Exception {
338         ConfigurationService configurationService = mock(ConfigurationService.class);
339         NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
340         Southbound southbound = mock(Southbound.class);
341
342         PowerMockito.mockStatic(ServiceHelper.class);
343         PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, bridgeConfigurationManagerImpl)).thenReturn(configurationService);
344         PowerMockito.when(ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, bridgeConfigurationManagerImpl)).thenReturn(networkingProviderManager);
345         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, bridgeConfigurationManagerImpl)).thenReturn(southbound);
346
347         bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
348
349         assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
350         assertEquals("Error, did not return the correct object", getField("networkingProviderManager"), networkingProviderManager);
351         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
352     }
353
354     private Object getField(String fieldName) throws Exception {
355         Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
356         field.setAccessible(true);
357         return field.get(bridgeConfigurationManagerImpl);
358     }
359 }