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.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;
28 import java.lang.reflect.Field;
29 import java.util.List;
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;
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;
55 * Test class for BridgeConfigurationManagerImpl
57 * @author Marcus Koontz
58 * @author Alexis de Talhouet
59 * @author Sam Hague (shague@redhat.com)
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;
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;
86 public void testGetBridgeUuid() {
87 when(southbound.getBridgeUuid(any(Node.class), anyString()))
89 .thenReturn(BRIDGE_UUID);
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());
99 public void testIsNodeNeutronReady() throws Exception {
100 when(southbound.getBridge(any(Node.class), anyString()))
104 verifyNoMoreInteractions(configurationService);
105 assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
106 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
108 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
109 assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
110 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
112 verify(configurationService, times(2)).getIntegrationBridgeName();
113 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
117 public void testIsNodeOverlayReady() throws Exception {
118 when(southbound.getBridge(any(Node.class), anyString()))
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);
128 verifyNoMoreInteractions(configurationService);
130 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
131 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
133 doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
135 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
136 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
138 assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
139 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
141 verify(configurationService, times(2)).getNetworkBridgeName();
142 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
146 public void testIsPortOnBridge() throws Exception {
147 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
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());
159 public void testIsNodeTunnelReady() throws Exception {
160 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
164 verifyNoMoreInteractions(configurationService);
165 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
166 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
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));
173 verify(configurationService, times(1)).isL3ForwardingEnabled();
174 verify(configurationService, times(3)).getIntegrationBridgeName();
175 verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
179 public void testIsNodeVlanReady() throws Exception {
180 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
184 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
188 when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
190 verifyNoMoreInteractions(configurationService);
191 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
192 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
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);
200 assertFalse("Error, did not return correct boolean from isNodeVlanReady",
201 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
203 assertTrue("Error, did not return correct boolean from isNodeVlanReady",
204 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
206 verify(configurationService, times(3)).getIntegrationBridgeName();
207 verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
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));
216 assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
220 public void testPrepareNode() {
221 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
222 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
224 PowerMockito.mockStatic(ConfigProperties.class);
225 when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
227 when(southbound.addBridge(any(Node.class), anyString(), anyList(), eq(DatapathTypeSystem.class))).thenReturn(true);
228 when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
230 bridgeConfigurationManagerImpl.prepareNode(node);
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);
242 for (String networkType : networkTypes) {
243 when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
244 when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
246 doAnswer(new Answer<Boolean>() {
248 public Boolean answer(InvocationOnMock invocation) {
251 }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
253 doAnswer(new Answer<Boolean>() {
255 public Boolean answer(InvocationOnMock invocation) {
258 }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
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));
265 assertTrue("Error, isCreated is not true for " + networkType,
266 bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
267 switch (networkType) {
269 verify(neutronNetworkMock, times(1)).getProviderNetworkType();
272 verify(neutronNetworkMock, times(2)).getProviderNetworkType();
275 verify(neutronNetworkMock, times(3)).getProviderNetworkType();
278 reset(neutronNetworkMock);
280 reset(bridgeConfigurationManagerImplSpy);
285 public void testGetPhysicalInterfaceName() throws Exception {
286 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
289 .thenReturn(PROVIDER_MAPPINGS);
290 String networkNames[] = {PHYSNET1, PHYSNET2};
291 String interfaceNames[] = {ETH1, ETH2};
293 verifyNoMoreInteractions(configurationService);
294 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
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],
304 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
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());
313 public void testGetAllPhysicalInterfaceNames() throws Exception {
314 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
316 .thenReturn(PROVIDER_MAPPINGS);
318 verifyNoMoreInteractions(configurationService);
319 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
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));
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());
337 public void testSetDependencies() throws Exception {
338 ConfigurationService configurationService = mock(ConfigurationService.class);
339 NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
340 Southbound southbound = mock(Southbound.class);
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);
347 bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
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);
354 private Object getField(String fieldName) throws Exception {
355 Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
356 field.setAccessible(true);
357 return field.get(bridgeConfigurationManagerImpl);