2e7d468a211726d73fcd95a739db64cd2f45123c
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / impl / BridgeConfigurationManagerImplTest.java
1 /*
2  * Copyright (c) 2014, 2016 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.netvirt.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.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;
28
29 import java.lang.reflect.Field;
30 import java.util.List;
31
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
49         .Node;
50 import org.osgi.framework.ServiceReference;
51
52 /**
53  * Test class for BridgeConfigurationManagerImpl
54  *
55  * @author Marcus Koontz
56  * @author Alexis de Talhouet
57  * @author Sam Hague (shague@redhat.com)
58  */
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;
68
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;
81
82     @Test
83     public void testGetBridgeUuid() {
84         when(southbound.getBridgeUuid(any(Node.class), anyString()))
85                 .thenReturn(null)
86                 .thenReturn(BRIDGE_UUID);
87
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());
93     }
94
95     @Test
96     public void testIsNodeNeutronReady() throws Exception {
97         when(southbound.getBridge(any(Node.class), anyString()))
98                 .thenReturn(null)
99                 .thenReturn(bridge);
100
101         verifyNoMoreInteractions(configurationService);
102         assertFalse("Error, did not return correct boolean from isNodeNeutronReady",
103                 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
104
105         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
106         assertTrue("Error, did not return correct boolean from isNodeNeutronReady",
107                 bridgeConfigurationManagerImpl.isNodeNeutronReady(node));
108
109         verify(configurationService, times(2)).getIntegrationBridgeName();
110         verify(southbound, times(2)).getBridge(any(Node.class), anyString());
111     }
112
113     @Test
114     public void testIsNodeOverlayReady() throws Exception {
115         when(southbound.getBridge(any(Node.class), anyString()))
116                 .thenReturn(null)
117                 .thenReturn(bridge);
118
119         BridgeConfigurationManagerImpl bridgeConfigurationManagerImplSpy =
120                 spy(new BridgeConfigurationManagerImpl());
121         doReturn(false).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
122         bridgeConfigurationManagerImplSpy.setConfigurationService(configurationService);
123         bridgeConfigurationManagerImplSpy.setSouthbound(southbound);
124
125         verifyNoMoreInteractions(configurationService);
126
127         assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
128                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
129
130         doReturn(true).when(bridgeConfigurationManagerImplSpy).isNodeNeutronReady(any(Node.class));
131
132         assertFalse("Error, did not return correct boolean from isNodeOverlayReady",
133                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
134
135         assertTrue("Error, did not return correct boolean from isNodeOverlayReady",
136                 bridgeConfigurationManagerImplSpy.isNodeOverlayReady(node));
137
138         verify(configurationService, times(2)).getNetworkBridgeName();
139         verify(southbound, times(2)).getBridge(any(Node.class), anyString());
140     }
141
142     @Test
143     public void testIsPortOnBridge() throws Exception {
144         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
145                 .thenReturn(null)
146                 .thenReturn(port);
147
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());
153     }
154
155     @Test
156     public void testIsNodeTunnelReady() throws Exception {
157         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
158                 .thenReturn(false)
159                 .thenReturn(true);
160
161         verifyNoMoreInteractions(configurationService);
162         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
163                 bridgeConfigurationManagerImpl.isNodeTunnelReady(node, node));
164
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));
169
170         verify(configurationService, times(1)).isL3ForwardingEnabled();
171         verify(configurationService, times(3)).getIntegrationBridgeName();
172         verify(southbound, times(2)).isBridgeOnOvsdbNode(any(Node.class), anyString());
173     }
174
175     @Test
176     public void testIsNodeVlanReady() throws Exception {
177         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString()))
178                 .thenReturn(false)
179                 .thenReturn(true);
180
181         when(southbound.extractTerminationPointAugmentation(any(Node.class), anyString()))
182                 .thenReturn(null)
183                 .thenReturn(port);
184
185         when(neutronNetwork.getProviderPhysicalNetwork()).thenReturn("test");
186
187         verifyNoMoreInteractions(configurationService);
188         assertFalse("Error, did not return correct boolean from isNodeTunnelReady",
189                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
190
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);
196
197         assertFalse("Error, did not return correct boolean from isNodeVlanReady",
198                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
199
200         assertTrue("Error, did not return correct boolean from isNodeVlanReady",
201                 bridgeConfigurationManagerImpl.isNodeVlanReady(node, node, neutronNetwork));
202
203         verify(configurationService, times(3)).getIntegrationBridgeName();
204         verify(neutronNetwork, times(2)).getProviderPhysicalNetwork();
205     }
206
207     @Test
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));
212
213         assertTrue("Error, isNodeL3Ready didn't return true", bridgeConfigurationManagerImpl.isNodeL3Ready(node, node));
214     }
215
216     @Test
217     public void testPrepareNode() {
218         when(configurationService.getIntegrationBridgeName()).thenReturn(BR_INT);
219         when(southbound.isBridgeOnOvsdbNode(any(Node.class), anyString())).thenReturn(false);
220
221         // TODO Figure out the strings
222         //when(ConfigProperties.getProperty(any(Class.class), anyString())).thenReturn(ADDRESS);
223
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);
229
230         // TODO This test doesn't verify anything
231     }
232
233     @Test
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);
241
242         for (String networkType : networkTypes) {
243             when(neutronNetworkMock.getProviderNetworkType()).thenReturn(networkType);
244             when(southbound.readOvsdbNode(any(Node.class))).thenReturn(node);
245
246             doAnswer(invocation -> Boolean.TRUE).when(bridgeConfigurationManagerImplSpy).isNodeVlanReady(any(Node.class), any(Node.class), any(NeutronNetwork.class));
247
248             doAnswer(invocation -> Boolean.TRUE).when(bridgeConfigurationManagerImplSpy).isNodeTunnelReady(any(Node.class), any(Node.class));
249
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));
254
255             assertTrue("Error, isCreated is not true for " + networkType,
256                     bridgeConfigurationManagerImplSpy.createLocalNetwork(node, neutronNetworkMock));
257             switch (networkType) {
258                 case "vlan":
259                     verify(neutronNetworkMock, times(1)).getProviderNetworkType();
260                     break;
261                 case "vxlan":
262                     verify(neutronNetworkMock, times(2)).getProviderNetworkType();
263                     break;
264                 case "gre":
265                     verify(neutronNetworkMock, times(3)).getProviderNetworkType();
266                     break;
267             }
268             reset(neutronNetworkMock);
269             reset(node);
270             reset(bridgeConfigurationManagerImplSpy);
271         }
272     }
273
274     @Test
275     public void testGetPhysicalInterfaceName() throws Exception {
276         when(southbound.getOtherConfig(any(Node.class), Matchers.eq(OvsdbTables.OPENVSWITCH), anyString()))
277                 .thenReturn(null)
278                 .thenReturn(null)
279                 .thenReturn(PROVIDER_MAPPINGS);
280         String networkNames[] = {PHYSNET1, PHYSNET2};
281         String interfaceNames[] = {ETH1, ETH2};
282
283         verifyNoMoreInteractions(configurationService);
284         when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
285
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],
293                     interfaceNames[i],
294                     bridgeConfigurationManagerImpl.getPhysicalInterfaceName(node, networkNames[i]));
295         }
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());
300     }
301
302     @Test
303     public void testGetAllPhysicalInterfaceNames() throws Exception {
304         when(southbound.getOtherConfig(any(Node.class), eq(OvsdbTables.OPENVSWITCH), anyString()))
305                 .thenReturn(null)
306                 .thenReturn(PROVIDER_MAPPINGS);
307
308         verifyNoMoreInteractions(configurationService);
309         when(configurationService.getDefaultProviderMapping()).thenReturn(PROVIDER_MAPPINGS_DEFAULT);
310
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));
320
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());
324     }
325
326     @Test
327     public void testSetDependencies() throws Exception {
328         ConfigurationService configurationService = mock(ConfigurationService.class);
329         NetworkingProviderManager networkingProviderManager = mock(NetworkingProviderManager.class);
330         Southbound southbound = mock(Southbound.class);
331
332         ServiceHelper.overrideGlobalInstance(ConfigurationService.class, configurationService);
333         ServiceHelper.overrideGlobalInstance(NetworkingProviderManager.class, networkingProviderManager);
334         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
335
336         bridgeConfigurationManagerImpl.setDependencies(mock(ServiceReference.class));
337
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);
341     }
342
343     private Object getField(String fieldName) throws Exception {
344         Field field = BridgeConfigurationManagerImpl.class.getDeclaredField(fieldName);
345         field.setAccessible(true);
346         return field.get(bridgeConfigurationManagerImpl);
347     }
348 }