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