2 * Copyright (c) 2014, 2016 Intel Corp. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.openstack.netvirt.impl;
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.spy;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.verifyNoMoreInteractions;
27 import static org.mockito.Mockito.when;
29 import java.lang.reflect.Field;
30 import java.util.List;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Matchers;
36 import org.mockito.Mock;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.opendaylight.netvirt.openstack.netvirt.api.OvsdbTables;
39 import org.opendaylight.netvirt.openstack.netvirt.api.ConfigurationService;
40 import org.opendaylight.netvirt.openstack.netvirt.api.NetworkingProviderManager;
41 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
42 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronNetwork;
43 import org.opendaylight.netvirt.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
47 .OvsdbTerminationPointAugmentation;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology
50 import org.osgi.framework.ServiceReference;
53 * Test class for BridgeConfigurationManagerImpl
55 * @author Marcus Koontz
56 * @author Alexis de Talhouet
57 * @author Sam Hague (shague@redhat.com)
59 @RunWith(MockitoJUnitRunner.class)
60 public class BridgeConfigurationManagerImplTest {
61 @Mock private Node node;
62 @Mock private OvsdbBridgeAugmentation bridge;
63 @Mock private OvsdbTerminationPointAugmentation port;
64 @Mock private NeutronNetwork neutronNetwork;
65 @Mock private ConfigurationService configurationService;
66 @Mock private Southbound southbound;
67 @InjectMocks public static BridgeConfigurationManagerImpl bridgeConfigurationManagerImpl;
69 private static final String ADDRESS = "127.0.0.1";
70 private static final String BR_INT = "br-int";
71 private static final String ETH1 = "eth1";
72 private static final String ETH2 = "eth2";
73 private static final String ETH3 = "eth3";
74 private static final String PORT_BR_INT = "br-int";
75 private static final String BRIDGE_UUID = "f527b951-3934-4182-9f29-33fc09f6f0c6";
76 private static final String PHYSNET1 = "physnet1";
77 private static final String PHYSNET2 = "physnet2";
78 private static final String PHYSNET3 = "physnet3";
79 private static final String PROVIDER_MAPPINGS = PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2;
80 private static final String PROVIDER_MAPPINGS_DEFAULT = PHYSNET1 + ":" + ETH1;
83 public void testGetBridgeUuid() {
84 when(southbound.getBridgeUuid(any(Node.class), anyString()))
86 .thenReturn(BRIDGE_UUID);
88 assertEquals("Error, null should have been returned", null,
89 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
90 assertEquals("Error, did not return UUID of correct bridge", BRIDGE_UUID,
91 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
92 verify(southbound, times(2)).getBridgeUuid(any(Node.class), anyString());
96 public void testIsNodeNeutronReady() throws Exception {
97 when(southbound.getBridge(any(Node.class), anyString()))
101 verifyNoMoreInteractions(configurationService);
102 assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
103 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
105 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
106 assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
107 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
109 verify(configurationService, times(2)).getIntegrationBridgeName();
110 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
114 public void testIsNodeOverlayReady() throws Exception {
115 when(southbound.getBridge(any(Node.class), anyString()))
119 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
120 spy(new BridgeConfigurationManagerImpl());
121 doReturn(false).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
122 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
123 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
125 verifyNoMoreInteractions(configurationService);
127 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
128 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
130 doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
132 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
133 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
135 assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
136 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
138 verify(configurationService, times(2)).getNetworkBridgeName();
139 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
143 public void testIsPortOnBridge() throws Exception {
144 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
148 assertFalse("Error, port " + PORT_BR_INT + " should not be found",
149 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
150 assertTrue("Error, port " + PORT_BR_INT + " should be found",
151 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
152 verify(southbound, times(2)).extractTerminationPointAugmentation(any(Node.class), anyString());
156 public void testIsNodeTunnelReady() throws Exception {
157 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
161 verifyNoMoreInteractions(configurationService);
162 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
163 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
165 when(configurationService.isL3ForwardingEnabled()).thenReturn(false);
166 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
167 assertTrue("Error, did not return correct boolean from isNodeTunnelReady",
168 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
170 verify(configurationService, times(1)).isL3ForwardingEnabled();
171 verify(configurationService, times(3)).getIntegrationBridgeName();
172 verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
176 public void testIsNodeVlanReady() throws Exception {
177 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
181 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
185 when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
187 verifyNoMoreInteractions(configurationService);
188 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
189 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
191 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
192 spy(new BridgeConfigurationManagerImpl());
193 doReturn(ETH1).when(bridgeConfigurationManagerImplSpy).getPhysicalInterfaceName(any(Node.class), anyString());
194 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
195 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
197 assertFalse("Error, did not return correct boolean from isNodeVlanReady",
198 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
200 assertTrue("Error, did not return correct boolean from isNodeVlanReady",
201 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
203 verify(configurationService, times(3)).getIntegrationBridgeName();
204 verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
208 public void testIsNodeL3Ready() {
209 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(true);
210 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString())).thenReturn(mock(OvsdbTerminationPointAugmentation.class));
211 when(southbound.readBridgeNode(any(Node.class), anyString())).thenReturn(mock(Node.class));
213 assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
217 public void testPrepareNode() {
218 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
219 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
221 // TODO Figure out the strings
222 //when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
224 when(southbound.addBridge(any(Node.class), anyString(), anyList(), eq(DatapathTypeSystem.class))).thenReturn(true);
225 when(southbound.readConfigBridge(any(Node.class), anyString())).thenReturn(mock(Node.class));
226 when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
227 when(configurationService.isL3MultipleExternalNetworkEnabled()).thenReturn(true);
228 bridgeConfigurationManagerImpl.prepareNode(node);
230 // TODO This test doesn't verify anything
234 public void testCreateLocalNetwork() throws Exception {
235 NeutronNetwork neutronNetworkMock = mock(NeutronNetwork.class, RETURNS_MOCKS);
236 String networkTypes[] = {"vlan", "vxlan", "gre"};
237 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
238 spy(new BridgeConfigurationManagerImpl());
239 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
240 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
242 for (String networkType : networkTypes) {
243 when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
244 when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
246 doAnswer(invocation -> Boolean.TRUE).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
248 doAnswer(invocation -> Boolean.TRUE).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
250 assertTrue("bridgeConfigMock.isNodeVlanReady is not true",
251 bridgeConfigurationManagerImplSpy.isNodeVlanReady(node, node, neutronNetworkMock));
252 assertTrue("bridgeConfigMock.isNodeTunnelReady is not true",
253 bridgeConfigurationManagerImplSpy.isNodeTunnelReady(node, node));
255 assertTrue("Error, isCreated is not true for " + networkType,
256 bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
257 switch (networkType) {
259 verify(neutronNetworkMock, times(1)).getProviderNetworkType();
262 verify(neutronNetworkMock, times(2)).getProviderNetworkType();
265 verify(neutronNetworkMock, times(3)).getProviderNetworkType();
268 reset(neutronNetworkMock);
270 reset(bridgeConfigurationManagerImplSpy);
275 public void testGetPhysicalInterfaceName() throws Exception {
276 when(southbound.getOtherConfig(any(Node.class), Matchers.eq(OvsdbTables.OPENVSWITCH), anyString()))
279 .thenReturn(PROVIDER_MAPPINGS);
280 String networkNames[] = {PHYSNET1, PHYSNET2};
281 String interfaceNames[] = {ETH1, ETH2};
283 verifyNoMoreInteractions(configurationService);
284 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
286 assertNull("Error, should not have found " + PHYSNET2 + ":" + ETH2,
287 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET2));
288 assertEquals("Error, should have found " + PHYSNET1 + ":" + ETH1,
289 ETH1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET1));
290 for (int i = 0; i < networkNames.length; i++) {
291 assertEquals("Error, network: " + networkNames[i]
292 + ", did not match interface: "+ interfaceNames[i],
294 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
296 assertNull(PHYSNET1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET3));
297 verify(configurationService, times(5)).getProviderMappingsKey();
298 verify(configurationService, times(2)).getDefaultProviderMapping();
299 verify(southbound, times(5)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
303 public void testGetAllPhysicalInterfaceNames() throws Exception {
304 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
306 .thenReturn(PROVIDER_MAPPINGS);
308 verifyNoMoreInteractions(configurationService);
309 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
311 List<String> interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
312 assertEquals("Error, should have found 1 interface", 1, interfaces.size());
313 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
314 assertFalse("Error, should not have found " + ETH2, interfaces.contains(ETH2));
315 interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
316 assertEquals("Error, should have found 2 interfaces", 2, interfaces.size());
317 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
318 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH2));
319 assertFalse("Error, should not have found " + ETH3, interfaces.contains(ETH3));
321 verify(configurationService, times(2)).getProviderMappingsKey();
322 verify(configurationService, times(1)).getDefaultProviderMapping();
323 verify(southbound, times(2)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
327 public void testSetDependencies() throws Exception {
328 ConfigurationService configurationService = mock(ConfigurationService.class);
329 NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
330 Southbound southbound = mock(Southbound.class);
332 ServiceHelper.overrideGlobalInstance(ConfigurationService.class, configurationService);
333 ServiceHelper.overrideGlobalInstance(NetworkingProviderManager.class, networkingProviderManager);
334 ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
336 bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
338 assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
339 assertEquals("Error, did not return the correct object", getField("networkingProviderManager"), networkingProviderManager);
340 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
343 private Object getField(String fieldName) throws Exception {
344 Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
345 field.setAccessible(true);
346 return field.get(bridgeConfigurationManagerImpl);