2 * Copyright (c) 2015 Inocybe 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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import java.lang.reflect.Field;
18 import java.lang.reflect.Method;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
31 import org.opendaylight.neutron.spi.INeutronPortCRUD;
32 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
33 import org.opendaylight.neutron.spi.NeutronFloatingIP;
34 import org.opendaylight.neutron.spi.NeutronNetwork;
35 import org.opendaylight.neutron.spi.NeutronPort;
36 import org.opendaylight.neutron.spi.NeutronRouter;
37 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
38 import org.opendaylight.neutron.spi.NeutronSubnet;
39 import org.opendaylight.neutron.spi.Neutron_IPs;
40 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
41 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
42 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
44 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
47 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
48 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
49 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
50 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
51 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
52 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
55 import org.osgi.framework.BundleContext;
56 import org.osgi.framework.ServiceReference;
57 import org.powermock.api.mockito.PowerMockito;
58 import org.powermock.core.classloader.annotations.PrepareForTest;
59 import org.powermock.modules.junit4.PowerMockRunner;
62 * Unit test for {@link NeutronL3Adapter}
64 @PrepareForTest({ConfigProperties.class, ServiceHelper.class})
65 @RunWith(PowerMockRunner.class)
66 public class NeutronL3AdapterTest {
68 @InjectMocks private NeutronL3Adapter neutronL3Adapter;
70 @Mock private ConfigurationService configurationService;
71 @Mock private TenantNetworkManager tenantNetworkManager;
72 @Mock private INeutronNetworkCRUD neutronNetworkCache;
73 @Mock private INeutronSubnetCRUD neutronSubnetCache;
74 @Mock private INeutronPortCRUD neutronPortCache;
75 @Mock private NodeCacheManager nodeCacheManager;
76 @Mock private Southbound southbound;
78 @Mock private NeutronPort neutronPort;
80 private Set<String> inboundIpRewriteCache;
81 private Set<String> outboundIpRewriteCache;
82 private Set<String> outboundIpRewriteExclusionCache;
83 private Set<String> routerInterfacesCache;
84 private Set<String> staticArpEntryCache;
85 private Set<String> l3ForwardingCache;
86 private Map<String, String> networkIdToRouterMacCache;
87 private Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache;
89 private static final String HOST_ADDRESS = "127.0.0.1";
92 public void setUp() throws Exception{
93 PowerMockito.mockStatic(ConfigProperties.class);
94 PowerMockito.when(ConfigProperties.getProperty(neutronL3Adapter.getClass(), "ovsdb.l3.fwd.enabled")).thenReturn("yes");
95 PowerMockito.when(ConfigProperties.getProperty(neutronL3Adapter.getClass(), "ovsdb.l3.arp.responder.disabled")).thenReturn("no");
97 when(configurationService.isL3ForwardingEnabled()).thenReturn(true);
99 this.getMethod("initL3AdapterMembers").invoke(neutronL3Adapter);
101 this.getNeutronL3AdapterFields();
110 private void getNeutronL3AdapterFields() throws Exception{
111 inboundIpRewriteCache = (Set<String>) getField("inboundIpRewriteCache");
112 outboundIpRewriteCache = (Set<String>) getField("outboundIpRewriteCache");
113 outboundIpRewriteExclusionCache = (Set<String>) getField("outboundIpRewriteExclusionCache");
114 routerInterfacesCache = (Set<String>) getField("routerInterfacesCache");
115 staticArpEntryCache = (Set<String>) getField("staticArpEntryCache");
116 l3ForwardingCache = (Set<String>) getField("l3ForwardingCache");
117 networkIdToRouterMacCache = (Map<String, String>) getField("networkIdToRouterMacCache");
118 subnetIdToRouterInterfaceCache = (Map<String, NeutronRouter_Interface>) getField("subnetIdToRouterInterfaceCache");
121 private void setUpVar(){
122 Neutron_IPs neutronIP = mock(Neutron_IPs.class);
123 NeutronRouter neutronRouter = mock(NeutronRouter.class);
125 NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
126 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
127 Node node = mock(Node.class);
128 NodeId nodeID = mock(NodeId.class);
130 //Row row = mock(Row.class);
131 //Bridge bridge = mock(Bridge.class);
132 Status status = mock(Status.class);
134 List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
135 list_neutronIP.add(neutronIP);
137 List<NeutronPort> list_neutronPort = new ArrayList<>();
138 list_neutronPort.add(neutronPort);
140 List<Node> list_nodes = new ArrayList<Node>();
141 list_nodes.add(node);
143 //ConcurrentMap<String, Row> rowMap = mock(ConcurrentMap.class);
144 //rowMap.put("key", row);
146 //Column<GenericTableSchema, Set<String>> bridgeColumnIds = mock(Column.class);
147 Set<String> dpids = new HashSet();
150 when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
151 when(neutronPort.getPortUUID()).thenReturn("portUUID");
152 when(neutronPort.getTenantID()).thenReturn("tenantID");
153 when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
154 when(neutronPort.getMacAddress()).thenReturn("macAddress1").thenReturn("macAddress2");
156 when(neutronIP.getSubnetUUID()).thenReturn("subnetUUID");
157 when(neutronIP.getIpAddress()).thenReturn(HOST_ADDRESS);
159 when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
160 when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
162 when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
164 when(neutronSubnet.getNetworkUUID()).thenReturn("networkUUID");
166 when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
168 when(neutronNetwork.getRouterExternal()).thenReturn(false); // default true
169 when(neutronNetwork.getProviderSegmentationID()).thenReturn("providerSegmentationId1","providerSegmentationId2", "providerSegmentationId3");
170 when(neutronNetwork.getTenantID()).thenReturn("tenantId");
171 when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
173 when(neutronSubnet.getGatewayIP()).thenReturn("gatewayIp");
174 when(neutronSubnet.getCidr()).thenReturn(HOST_ADDRESS + "/32");
175 when(neutronSubnet.getSubnetUUID()).thenReturn("subnetUUID");
177 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(false);
178 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(true);
180 when(node.getNodeId()).thenReturn(nodeID);
182 when(nodeID.getValue()).thenReturn("nodeId");
184 when(status.isSuccess()).thenReturn(true);
186 /* TODO SB_MIGRATION */
187 //when(connectionService.getBridgeNodes()).thenReturn(list_nodes);
189 when(configurationService.getDefaultGatewayMacAddress(any(Node.class))).thenReturn("defaultGatewayMacAddress");
190 when(configurationService.getIntegrationBridgeName()).thenReturn("brName");
192 //when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(rowMap);
193 //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
194 //when(ovsdbConfigurationService.getRow(any(Node.class), anyString(), anyString())).thenReturn(row);
196 //when(bridge.getName()).thenReturn("brName");
197 //when(bridge.getDatapathIdColumn()).thenReturn(bridgeColumnIds);
199 //when(bridgeColumnIds.getData()).thenReturn(dpids);
201 when(nodeCacheManager.getBridgeNodes()).thenReturn(list_nodes);
206 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
207 * Device owner = network:router_interface
210 public void testHandleNeutronPortEvent1() {
211 when(neutronPort.getDeviceOwner()).thenReturn("network:router_interface");
213 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
214 // Affected by the add
215 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
216 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
217 /* TODO SB_MIGRATION */
218 //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
219 // assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
220 // assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
221 // assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
223 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
224 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
226 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
227 // Affected by the delete
228 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 0, networkIdToRouterMacCache.size());
229 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
230 // assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
232 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
233 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
234 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
235 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
236 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
240 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
244 public void testHandleNeutronPortEvent2() {
245 when(neutronPort.getDeviceOwner()).thenReturn("");
247 // populate subnetIdToRouterInterfaceCache to pass the
248 // if (neutronRouterInterface != null)
249 NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
250 when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
251 when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
252 subnetIdToRouterInterfaceCache.put("subnetUUID", neutronRouterInterface);
254 /* device owner = "" */
255 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
256 // Affected by the add
257 /* TODO SB_MIGRATION */
258 //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
259 assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
260 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
261 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
262 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
264 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
266 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
267 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
269 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
270 // Affected by the delete
271 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
272 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
274 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
275 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
276 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
277 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
278 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
280 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
284 * Test method {@link NeutronL3Adapter#handleNeutronFloatingIPEvent(NeutronFloatingIP, Action)}
287 public void testandleNeutronFloatingIPEvent() throws Exception{
288 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
289 when(neutronFloatingIP.getFixedIPAddress()).thenReturn(HOST_ADDRESS);
290 when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(HOST_ADDRESS);
291 when(neutronFloatingIP.getFloatingNetworkUUID()).thenReturn("floatingNetworkUUID");
293 networkIdToRouterMacCache.put("floatingNetworkUUID", "routerMacAddress");
295 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
297 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
298 // Affected by the add
299 /* TODO SB_MIGRATION */
300 //assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
301 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
302 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
304 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
305 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
306 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
307 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
309 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
311 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
312 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
313 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
314 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
315 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
316 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
317 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
318 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
322 public void testSetDependencies() throws Exception {
323 TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
324 ConfigurationService configurationService = mock(ConfigurationService.class);
325 ArpProvider arpProvider = mock(ArpProvider.class);
326 InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
327 OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
328 RoutingProvider routingProvider = mock(RoutingProvider.class);
329 L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
330 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
331 Southbound southbound = mock(Southbound.class);
333 PowerMockito.mockStatic(ServiceHelper.class);
334 PowerMockito.when(ServiceHelper.getGlobalInstance(TenantNetworkManager.class, neutronL3Adapter)).thenReturn(tenantNetworkManager);
335 PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, neutronL3Adapter)).thenReturn(configurationService);
336 PowerMockito.when(ServiceHelper.getGlobalInstance(ArpProvider.class, neutronL3Adapter)).thenReturn(arpProvider);
337 PowerMockito.when(ServiceHelper.getGlobalInstance(InboundNatProvider.class, neutronL3Adapter)).thenReturn(inboundNatProvider);
338 PowerMockito.when(ServiceHelper.getGlobalInstance(OutboundNatProvider.class, neutronL3Adapter)).thenReturn(outboundNatProvider);
339 PowerMockito.when(ServiceHelper.getGlobalInstance(RoutingProvider.class, neutronL3Adapter)).thenReturn(routingProvider);
340 PowerMockito.when(ServiceHelper.getGlobalInstance(L3ForwardingProvider.class, neutronL3Adapter)).thenReturn(l3ForwardingProvider);
341 PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, neutronL3Adapter)).thenReturn(nodeCacheManager);
342 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, neutronL3Adapter)).thenReturn(southbound);
344 neutronL3Adapter.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
346 assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
347 assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
348 assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
349 assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
350 assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
351 assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
352 assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
353 assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
354 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
358 public void testSetDependenciesObject() throws Exception{
359 INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
360 neutronL3Adapter.setDependencies(iNeutronNetworkCRUD);
361 assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
363 INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
364 neutronL3Adapter.setDependencies(iNeutronPortCRUD);
365 assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
367 INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
368 neutronL3Adapter.setDependencies(iNeutronSubnetCRUD);
369 assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
371 ArpProvider arpProvider = mock(ArpProvider.class);
372 neutronL3Adapter.setDependencies(arpProvider);
373 assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
375 InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
376 neutronL3Adapter.setDependencies(inboundNatProvider);
377 assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
379 OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
380 neutronL3Adapter.setDependencies(outboundNatProvider);
381 assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
383 RoutingProvider routingProvider = mock(RoutingProvider.class);
384 neutronL3Adapter.setDependencies(routingProvider);
385 assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
387 L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
388 neutronL3Adapter.setDependencies(l3ForwardingProvider);
389 assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
392 private Object getField(String fieldName) throws Exception {
393 Field field = NeutronL3Adapter.class.getDeclaredField(fieldName);
394 field.setAccessible(true);
395 return field.get(neutronL3Adapter);
398 private Method getMethod(String methodName) throws Exception {
399 Method method = neutronL3Adapter.getClass().getDeclaredMethod(methodName, new Class[] {});
400 method.setAccessible(true);