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.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;
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;
54 * Test class for BridgeConfigurationManagerImpl
56 * @author Marcus Koontz
57 * @author Alexis de Talhouet
58 * @author Sam Hague (shague@redhat.com)
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;
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;
85 public void testGetBridgeUuid() {
86 when(southbound.getBridgeUuid(any(Node.class), anyString()))
88 .thenReturn(BRIDGE_UUID);
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());
98 public void testIsNodeNeutronReady() throws Exception {
99 when(southbound.getBridge(any(Node.class), anyString()))
103 verifyNoMoreInteractions(configurationService);
104 assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
105 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
107 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
108 assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
109 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
111 verify(configurationService, times(2)).getIntegrationBridgeName();
112 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
116 public void testIsNodeOverlayReady() throws Exception {
117 when(southbound.getBridge(any(Node.class), anyString()))
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);
127 verifyNoMoreInteractions(configurationService);
129 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
130 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
132 doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
134 assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
135 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
137 assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
138 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
140 verify(configurationService, times(2)).getNetworkBridgeName();
141 verify(southbound, times(2)).getBridge(any(Node.class), anyString());
145 public void testIsPortOnBridge() throws Exception {
146 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
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());
158 public void testIsNodeTunnelReady() throws Exception {
159 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
163 verifyNoMoreInteractions(configurationService);
164 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
165 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
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));
172 verify(configurationService, times(1)).isL3ForwardingEnabled();
173 verify(configurationService, times(3)).getIntegrationBridgeName();
174 verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
178 public void testIsNodeVlanReady() throws Exception {
179 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
183 when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
187 when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
189 verifyNoMoreInteractions(configurationService);
190 assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
191 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
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);
199 assertFalse("Error, did not return correct boolean from isNodeVlanReady",
200 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
202 assertTrue("Error, did not return correct boolean from isNodeVlanReady",
203 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
205 verify(configurationService, times(3)).getIntegrationBridgeName();
206 verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
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));
215 assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
219 public void testPrepareNode() {
220 when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
221 when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
223 PowerMockito.mockStatic(ConfigProperties.class);
224 when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
226 when(southbound.addBridge(any(Node.class), anyString(), anyList())).thenReturn(true);
227 when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
229 bridgeConfigurationManagerImpl.prepareNode(node);
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);
241 for (String networkType : networkTypes) {
242 when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
243 when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
245 doAnswer(new Answer<Boolean>() {
247 public Boolean answer(InvocationOnMock invocation) {
250 }).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
252 doAnswer(new Answer<Boolean>() {
254 public Boolean answer(InvocationOnMock invocation) {
257 }).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
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));
264 assertTrue("Error, isCreated is not true for " + networkType,
265 bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
266 switch (networkType) {
268 verify(neutronNetworkMock, times(1)).getProviderNetworkType();
271 verify(neutronNetworkMock, times(2)).getProviderNetworkType();
274 verify(neutronNetworkMock, times(3)).getProviderNetworkType();
277 reset(neutronNetworkMock);
279 reset(bridgeConfigurationManagerImplSpy);
284 public void testGetPhysicalInterfaceName() throws Exception {
285 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
288 .thenReturn(PROVIDER_MAPPINGS);
289 String networkNames[] = {PHYSNET1, PHYSNET2};
290 String interfaceNames[] = {ETH1, ETH2};
292 verifyNoMoreInteractions(configurationService);
293 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
295 assertNull("Error, should not have found " + PHYSNET2 + ":" + ETH2,
296 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET2));
297 assertEquals("Error, should have found " + PHYSNET1 + ":" + ETH1,
298 ETH1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET1));
299 for (int i = 0; i < networkNames.length; i++) {
300 assertEquals("Error, network: " + networkNames[i]
301 + ", did not match interface: "+ interfaceNames[i],
303 bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
305 assertNull(PHYSNET1, bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, PHYSNET3));
306 verify(configurationService, times(5)).getProviderMappingsKey();
307 verify(configurationService, times(2)).getDefaultProviderMapping();
308 verify(southbound, times(5)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
312 public void testGetAllPhysicalInterfaceNames() throws Exception {
313 when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
315 .thenReturn(PROVIDER_MAPPINGS);
317 verifyNoMoreInteractions(configurationService);
318 when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
320 List<String> interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
321 assertEquals("Error, should have found 1 interface", 1, interfaces.size());
322 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
323 assertFalse("Error, should not have found " + ETH2, interfaces.contains(ETH2));
324 interfaces = bridgeConfigurationManagerImpl.getAllPhysicalInterfaceNames(node);
325 assertEquals("Error, should have found 2 interfaces", 2, interfaces.size());
326 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH1));
327 assertTrue("Error, should have found " + ETH1, interfaces.contains(ETH2));
328 assertFalse("Error, should not have found " + ETH3, interfaces.contains(ETH3));
330 verify(configurationService, times(2)).getProviderMappingsKey();
331 verify(configurationService, times(1)).getDefaultProviderMapping();
332 verify(southbound, times(2)).getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString());
336 public void testSetDependencies() throws Exception {
337 ConfigurationService configurationService = mock(ConfigurationService.class);
338 NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
339 Southbound southbound = mock(Southbound.class);
341 PowerMockito.mockStatic(ServiceHelper.class);
342 PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, bridgeConfigurationManagerImpl)).thenReturn(configurationService);
343 PowerMockito.when(ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, bridgeConfigurationManagerImpl)).thenReturn(networkingProviderManager);
344 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, bridgeConfigurationManagerImpl)).thenReturn(southbound);
346 bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
348 assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
349 assertEquals("Error, did not return the correct object", getField("networkingProviderManager"), networkingProviderManager);
350 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
353 private Object getField(String fieldName) throws Exception {
354 Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
355 field.setAccessible(true);
356 return field.get(bridgeConfigurationManagerImpl);