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.Matchers.same;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
24 import java.util.concurrent.ConcurrentMap;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.InjectMocks;
30 import org.mockito.Mock;
31 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
32 import org.opendaylight.neutron.spi.INeutronPortCRUD;
33 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
34 import org.opendaylight.neutron.spi.NeutronFloatingIP;
35 import org.opendaylight.neutron.spi.NeutronNetwork;
36 import org.opendaylight.neutron.spi.NeutronPort;
37 import org.opendaylight.neutron.spi.NeutronRouter;
38 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
39 import org.opendaylight.neutron.spi.NeutronSubnet;
40 import org.opendaylight.neutron.spi.Neutron_IPs;
41 import org.opendaylight.ovsdb.lib.notation.Column;
42 import org.opendaylight.ovsdb.lib.notation.Row;
43 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
44 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
47 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
48 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
49 import org.opendaylight.ovsdb.openstack.netvirt.api.MultiTenantAwareRouter;
50 import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager;
51 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
52 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
53 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
54 import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
55 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
56 import org.opendaylight.ovsdb.plugin.api.Status;
57 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
58 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
61 import org.powermock.api.mockito.PowerMockito;
62 import org.powermock.core.classloader.annotations.PrepareForTest;
63 import org.powermock.modules.junit4.PowerMockRunner;
66 * Unit test for {@link NeutronL3Adapter}
68 @PrepareForTest(ConfigProperties.class)
69 @RunWith(PowerMockRunner.class)
70 public class NeutronL3AdapterTest {
72 @Mock private ConfigurationService configurationService;
73 @Mock private TenantNetworkManager tenantNetworkManager;
74 @Mock private OvsdbConfigurationService ovsdbConfigurationService;
75 @Mock private OvsdbConnectionService connectionService;
76 @Mock private INeutronNetworkCRUD neutronNetworkCache;
77 @Mock private INeutronSubnetCRUD neutronSubnetCache;
78 @Mock private INeutronPortCRUD neutronPortCache;
80 @Mock private NeutronPort neutronPort;
81 @Mock private Neutron_IPs neutronIP;
82 @Mock private NeutronRouter neutronRouter;
83 @Mock private NeutronRouter_Interface neutronRouterInterface;
84 @Mock private NeutronSubnet neutronSubnet;
85 @Mock private NeutronNetwork neutronNetwork;
86 @Mock private Node node;
87 @Mock private NodeId nodeID;
88 @Mock private Row row;
89 @Mock private Bridge bridge;
90 @Mock private Status status;
91 @InjectMocks NeutronL3Adapter neutronL3Adapter;
93 private Set<String> inboundIpRewriteCache;
94 private Set<String> outboundIpRewriteCache;
95 private Set<String> inboundIpRewriteExclusionCache;
96 private Set<String> outboundIpRewriteExclusionCache;
97 private Set<String> routerInterfacesCache;
98 private Set<String> staticArpEntryCache;
99 private Set<String> l3ForwardingCache;
100 private Set<String> defaultRouteCache;
101 private Map<String, String> networkIdToRouterMacCache;
102 private Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache;
104 private static final String HOST_ADDRESS = "127.0.0.1";
107 public void setUp() throws Exception{
108 PowerMockito.mockStatic(ConfigProperties.class);
109 PowerMockito.when(ConfigProperties.getProperty(neutronL3Adapter.getClass(), "ovsdb.l3.fwd.enabled")).thenReturn("yes");
111 neutronL3Adapter.init();
113 this.getNeutronL3AdapterFields();
117 private void getNeutronL3AdapterFields() throws Exception{
118 inboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteCache");
119 outboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteCache");
120 inboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteExclusionCache");
121 outboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteExclusionCache");
122 routerInterfacesCache = (Set<String>) getNeutronL3AdapterField("routerInterfacesCache");
123 staticArpEntryCache = (Set<String>) getNeutronL3AdapterField("staticArpEntryCache");
124 l3ForwardingCache = (Set<String>) getNeutronL3AdapterField("l3ForwardingCache");
125 defaultRouteCache = (Set<String>) getNeutronL3AdapterField("defaultRouteCache");
126 networkIdToRouterMacCache = (Map<String, String>) getNeutronL3AdapterField("networkIdToRouterMacCache");
127 subnetIdToRouterInterfaceCache = (Map<String, NeutronRouter_Interface>) getNeutronL3AdapterField("subnetIdToRouterInterfaceCache");
130 private Object getNeutronL3AdapterField(String fieldName) throws Exception {
131 Field fieldObject = NeutronL3Adapter.class.getDeclaredField(fieldName);
132 fieldObject.setAccessible(true);
133 return fieldObject.get(neutronL3Adapter);
136 private void setUpVar(){
137 List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
138 list_neutronIP.add(neutronIP);
140 List<NeutronPort> list_neutronPort = new ArrayList<>();
141 list_neutronPort.add(neutronPort);
143 List<Node> list_nodes = new ArrayList<Node>();
144 list_nodes.add(node);
146 ConcurrentMap<String, Row> rowMap = mock(ConcurrentMap.class);
147 rowMap.put("key", row);
149 Column<GenericTableSchema, Set<String>> bridgeColumnIds = mock(Column.class);
150 Set<String> dpids = new HashSet();
153 when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
154 when(neutronPort.getPortUUID()).thenReturn("portUUID");
155 when(neutronPort.getTenantID()).thenReturn("tenantID");
156 when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
157 when(neutronPort.getMacAddress()).thenReturn("macAddress1").thenReturn("macAddress2");
159 when(neutronIP.getSubnetUUID()).thenReturn("subnetUUID");
160 when(neutronIP.getIpAddress()).thenReturn(HOST_ADDRESS);
162 when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
163 when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
165 when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
166 when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
168 when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
170 when(neutronSubnet.getNetworkUUID()).thenReturn("networkUUID");
172 when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
174 when(neutronNetwork.getRouterExternal()).thenReturn(false); // default true
175 when(neutronNetwork.getProviderSegmentationID()).thenReturn("providerSegmentationId1","providerSegmentationId2", "providerSegmentationId3");
176 when(neutronNetwork.getTenantID()).thenReturn("tenantId");
177 when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
179 when(neutronSubnet.getGatewayIP()).thenReturn("gatewayIp");
180 when(neutronSubnet.getCidr()).thenReturn(HOST_ADDRESS + "/32");
181 when(neutronSubnet.getSubnetUUID()).thenReturn("subnetUUID");
183 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(false);
184 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(true);
186 when(node.getId()).thenReturn(nodeID);
188 when(nodeID.getValue()).thenReturn("nodeId");
190 when(status.isSuccess()).thenReturn(true);
192 when(connectionService.getNodes()).thenReturn(list_nodes);
194 when(configurationService.getDefaultGatewayMacAddress(any(Node.class))).thenReturn("defaultGatewayMacAddress");
195 when(configurationService.getIntegrationBridgeName()).thenReturn("brName");
197 when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(rowMap);
198 when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
199 when(ovsdbConfigurationService.getRow(any(Node.class), anyString(), anyString())).thenReturn(row);
201 when(bridge.getName()).thenReturn("brName");
202 when(bridge.getDatapathIdColumn()).thenReturn(bridgeColumnIds);
204 when(bridgeColumnIds.getData()).thenReturn(dpids);
209 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
210 * Device owner = network:router_interface
213 public void testHandleNeutronPortEvent1() {
214 when(neutronPort.getDeviceOwner()).thenReturn("network:router_interface");
216 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
217 // Affected by the add
218 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
219 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
220 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
221 assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
222 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
223 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
224 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
226 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
227 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
228 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
230 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
231 // Affected by the delete
232 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 0, networkIdToRouterMacCache.size());
233 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
234 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
236 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
237 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
238 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
239 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
240 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
241 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
242 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
246 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
250 public void testHandleNeutronPortEvent2() {
251 when(neutronPort.getDeviceOwner()).thenReturn("");
253 // populate subnetIdToRouterInterfaceCache to pass the
254 // if (neutronRouterInterface != null)
255 subnetIdToRouterInterfaceCache.put("subnetUUID", neutronRouterInterface);
257 /* device owner = "" */
258 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
259 // Affected by the add
260 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
261 assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
262 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
263 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
264 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
265 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
267 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
269 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
270 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
271 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
273 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
274 // Affected by the delete
275 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
276 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
278 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
279 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
280 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
281 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
282 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
283 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
284 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
286 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
290 * Test method {@link NeutronL3Adapter#handleNeutronFloatingIPEvent(NeutronFloatingIP, Action)}
293 public void testandleNeutronFloatingIPEvent() throws Exception{
294 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
295 when(neutronFloatingIP.getFixedIPAddress()).thenReturn(HOST_ADDRESS);
296 when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(HOST_ADDRESS);
297 when(neutronFloatingIP.getFloatingNetworkUUID()).thenReturn("floatingNetworkUUID");
299 networkIdToRouterMacCache.put("floatingNetworkUUID", "routerMacAddress");
301 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
303 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
304 // Affected by the add
305 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
306 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
307 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
309 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
310 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
311 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
312 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
313 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
314 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
316 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
318 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
319 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
320 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
321 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
322 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
323 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
324 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
325 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
326 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
327 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());