2 * Copyright (c) 2014 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.ovsdb.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.anyString;
16 import static org.mockito.Matchers.eq;
17 import static org.mockito.Mockito.RETURNS_MOCKS;
18 import static org.mockito.Mockito.doAnswer;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.reset;
22 import static org.mockito.Mockito.times;
23 import static org.mockito.Mockito.verify;
24 import static org.mockito.Mockito.verifyNoMoreInteractions;
25 import static org.mockito.Mockito.when;
27 import java.lang.reflect.Field;
28 import java.util.List;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.invocation.InvocationOnMock;
35 import org.mockito.stubbing.Answer;
36 import org.opendaylight.neutron.spi.NeutronNetwork;
37 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
38 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
39 import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
40 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
41 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
42 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.ServiceReference;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
53 * Test class for BridgeConfigurationManagerImpl
55 * @author Marcus Koontz
56 * @author Alexis de Talhouet
57 * @author Sam Hague (shague@redhat.com)
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({ServiceHelper.class, ConfigProperties.class})
61 public class BridgeConfigurationManagerImplTest {
62 @Mock private Node node;
63 @Mock private OvsdbBridgeAugmentation bridge;
64 @Mock private OvsdbTerminationPointAugmentation port;
65 @Mock private NeutronNetwork neutronNetwork;
66 @Mock private ConfigurationService configurationService;
67 @Mock private Southbound southbound;
68 @InjectMocks public static BridgeConfigurationManagerImpl bridgeConfigurationManagerImpl;
70 private static final String ADDRESS = "127.0.0.1";
71 private static final String BR_INT = "br-int";
72 private static final String ETH1 = "eth1";
73 private static final String ETH2 = "eth2";
74 private static final String ETH3 = "eth3";
75 private static final String PORT_BR_INT = "br-int";
76 private static final String BRIDGE_UUID = "f527b951-3934-4182-9f29-33fc09f6f0c6";
77 private static final String PHYSNET1 = "physnet1";
78 private static final String PHYSNET2 = "physnet2";
79 private static final String PHYSNET3 = "physnet3";
80 private static final String PROVIDER_MAPPINGS = PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2;
81 private static final String PROVIDER_MAPPINGS_DEFAULT = PHYSNET1 + ":" + ETH1;
84 public void testGetBridgeUuid() {
85 when(southbound.getBridgeUuid(any(Node.class), anyString()))
87 .thenReturn(BRIDGE_UUID);
89 assertEquals("Error, null should have been returned", null,
90 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
91 assertEquals("Error, did not return UUID of correct bridge", BRIDGE_UUID,
92 bridgeConfigurationManagerImpl.getBridgeUuid(node, BR_INT));
93 verify(southbound, times(2)).getBridgeUuid(any(Node.class), anyString());
97 public void testIsNodeNeutronReady() throws Exception {
98 when(southbound.getBridge(any(Node.class), anyString()))
102 verifyNoMoreInteractions(configurationService);
103 assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
104 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
106 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
107 assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
108 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
110 verify(configurationService, times(2)).getIntegrationBridgeName();
111 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
115 public void testIsNodeOverlayReady() throws Exception {
116 when(southbound.getBridge(any(Node.class), anyString()))
120 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
121 PowerMockito.spy(new BridgeConfigurationManagerImpl());
122 doReturn(false).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
123 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
124 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
126 verifyNoMoreInteractions(configurationService);
128 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
129 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
131 doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
133 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
134 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
136 assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
137 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
139 verify(configurationService, times(2)).getNetworkBridgeName();
140 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
144 public void testIsPortOnBridge() throws Exception {
145 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
149 assertFalse("Error, port " + PORT_BR_INT + " should not be found",
150 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
151 assertTrue("Error, port " + PORT_BR_INT + " should be found",
152 bridgeConfigurationManagerImpl.isPortOnBridge(node, PORT_BR_INT));
153 verify(southbound, times(2)).extractTerminationPointAugmentation(any(Node.class), anyString());
157 public void testIsNodeTunnelReady() throws Exception {
158 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
162 verifyNoMoreInteractions(configurationService);
163 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
164 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
166 when(configurationService.isL3ForwardingEnabled()).thenReturn(false);
167 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
168 assertTrue("Error, did not return correct boolean from isNodeTunnelReady",
169 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
171 verify(configurationService, times(1)).isL3ForwardingEnabled();
172 verify(configurationService, times(3)).getIntegrationBridgeName();
173 verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
177 public void testIsNodeVlanReady() throws Exception {
178 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
182 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
186 when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
188 verifyNoMoreInteractions(configurationService);
189 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
190 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
192 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
193 PowerMockito.spy(new BridgeConfigurationManagerImpl());
194 doReturn(ETH1).when(bridgeConfigurationManagerImplSpy).getPhysicalInterfaceName(any(Node.class), anyString());
195 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
196 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
198 assertFalse("Error, did not return correct boolean from isNodeVlanReady",
199 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
201 assertTrue("Error, did not return correct boolean from isNodeVlanReady",
202 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
204 verify(configurationService, times(3)).getIntegrationBridgeName();
205 verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
209 public void testIsNodeL3Ready() {
210 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(true);
211 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString())).thenReturn(mock(OvsdbTerminationPointAugmentation.class));
212 when(southbound.readBridgeNode(any(Node.class), anyString())).thenReturn(mock(Node.class));
214 assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
218 public void testPrepareNode() {
219 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
220 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
222 PowerMockito.mockStatic(ConfigProperties.class);
223 when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
225 when(southbound.addBridge(any(Node.class), anyString(), anyString())).thenReturn(true);
226 when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
228 bridgeConfigurationManagerImpl.prepareNode(node);
232 public void testCreateLocalNetwork() throws Exception {
233 NeutronNetwork neutronNetworkMock = mock(NeutronNetwork.class, RETURNS_MOCKS);
234 String networkTypes[] = {"vlan", "vxlan", "gre"};
235 BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
236 PowerMockito.spy(new BridgeConfigurationManagerImpl());
237 bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
238 bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
240 for (String networkType : networkTypes) {
241 when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
242 when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
244 doAnswer(new Answer<Boolean>() {
246 public Boolean answer(InvocationOnMock invocation) {
249 }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
251 doAnswer(new Answer<Boolean>() {
253 public Boolean answer(InvocationOnMock invocation) {
256 }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
258 assertTrue("bridgeConfigMock.isNodeVlanReady is not true",
259 bridgeConfigurationManagerImplSpy.isNodeVlanReady(node, node, neutronNetworkMock));
260 assertTrue("bridgeConfigMock.isNodeTunnelReady is not true",
261 bridgeConfigurationManagerImplSpy.isNodeTunnelReady(node, node));
263 assertTrue("Error, isCreated is not true for " + networkType,
264 bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
265 if (networkType.equals("vlan")) {
266 verify(neutronNetworkMock, times(1)).getProviderNetworkType();
267 } else if (networkType.equals("vxlan")) {
268 verify(neutronNetworkMock, times(2)).getProviderNetworkType();
269 } else if (networkType.equals("gre")) {
270 verify(neutronNetworkMock, times(3)).getProviderNetworkType();
272 reset(neutronNetworkMock);
274 reset(bridgeConfigurationManagerImplSpy);
279 public void testGetPhysicalInterfaceName() throws Exception {
280 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
283 .thenReturn(PROVIDER_MAPPINGS);
284 String networkNames[] = {PHYSNET1, PHYSNET2};
285 String interfaceNames[] = {ETH1, ETH2};
287 verifyNoMoreInteractions(configurationService);
288 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
290 assertNull("Error, should not have found " + PHYSNET2 + ":" + ETH2,
291 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET2));
292 assertEquals("Error, should have found " + PHYSNET1 + ":" + ETH1,
293 ETH1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET1));
294 for (int i = 0; i < networkNames.length; i++) {
295 assertEquals("Error, network: " + networkNames[i]
296 + ", did not match interface: "+ interfaceNames[i],
298 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
300 assertNull(PHYSNET1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET3));
301 verify(configurationService, times(5)).getProviderMappingsKey();
302 verify(configurationService, times(2)).getDefaultProviderMapping();
303 verify(southbound, times(5)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
307 public void testGetAllPhysicalInterfaceNames() throws Exception {
308 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
310 .thenReturn(PROVIDER_MAPPINGS);
312 verifyNoMoreInteractions(configurationService);
313 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
315 List<String> interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
316 assertEquals("Error, should have found 1 interface", 1, interfaces.size());
317 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
318 assertFalse("Error, should not have found " + ETH2, interfaces.contains(ETH2));
319 interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
320 assertEquals("Error, should have found 2 interfaces", 2, interfaces.size());
321 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
322 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH2));
323 assertFalse("Error, should not have found " + ETH3, interfaces.contains(ETH3));
325 verify(configurationService, times(2)).getProviderMappingsKey();
326 verify(configurationService, times(1)).getDefaultProviderMapping();
327 verify(southbound, times(2)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
331 public void testSetDependencies() throws Exception {
332 ConfigurationService configurationService = mock(ConfigurationService.class);
333 NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
334 Southbound southbound = mock(Southbound.class);
336 PowerMockito.mockStatic(ServiceHelper.class);
337 PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, bridgeConfigurationManagerImpl)).thenReturn(configurationService);
338 PowerMockito.when(ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, bridgeConfigurationManagerImpl)).thenReturn(networkingProviderManager);
339 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, bridgeConfigurationManagerImpl)).thenReturn(southbound);
341 bridgeConfigurationManagerImpl.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
343 assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
344 assertEquals("Error, did not return the correct object", getField("networkingProviderManager"), networkingProviderManager);
345 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
348 private Object getField(String fieldName) throws Exception {
349 Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
350 field.setAccessible(true);
351 return field.get(bridgeConfigurationManagerImpl);