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.anyBoolean;
14 import static org.mockito.Matchers.anyInt;
15 import static org.mockito.Matchers.anyLong;
16 import static org.mockito.Matchers.anyString;
17 import static org.mockito.Matchers.eq;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.when;
22 import java.lang.reflect.Constructor;
23 import java.lang.reflect.Field;
24 import java.net.InetAddress;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
32 import org.apache.commons.lang3.tuple.Pair;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.Mockito;
38 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronFloatingIP;
39 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
40 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
41 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronRouter;
42 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronRouter_Interface;
43 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
44 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
45 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
46 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
47 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
48 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
49 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
50 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
51 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
52 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
53 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
55 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
56 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
57 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
58 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
59 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
60 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
67 import org.osgi.framework.ServiceReference;
68 import org.powermock.api.mockito.PowerMockito;
69 import org.powermock.api.support.membermodification.MemberMatcher;
70 import org.powermock.api.support.membermodification.MemberModifier;
71 import org.powermock.core.classloader.annotations.PrepareForTest;
72 import org.powermock.modules.junit4.PowerMockRunner;
73 import org.powermock.reflect.Whitebox;
76 * Unit test for {@link NeutronL3Adapter}
78 @PrepareForTest({ServiceHelper.class, InetAddress.class, NeutronL3Adapter.class})
79 @RunWith(PowerMockRunner.class)
80 public class NeutronL3AdapterTest {
82 @Mock private NeutronL3Adapter neutronL3Adapter;
84 private static final String ID = "45";
85 private static final String IP = "127.0.0.1";
86 private static final String MALFORM_IP = "127.0.0.1.5";
87 private static final String INTF_NAME = "br-int";
88 private static final String EXTERNAL_ROUTER_MAC_UPDATE = "";
89 private static final String UUID = "7da709ff-397f-4778-a0e8-994811272fdb";
90 private static final String FIXED_IP_ADDRESS = "192.168.1.0";
91 private static final String FLOATING_IP_ADDRESS = "192.168.1.1";
92 private static final String OWNER_ROUTER_INTERFACE = "network:router_interface";
93 private static final String OWNER_FLOATING_IP = "network:floatingip";
94 private static final String MAC_ADDRESS = "00:00:5E:00:02:01";
95 private static final String MAC_ADDRESS_2 = "00:00:5E:00:02:02";
96 private static final String PORT_INT = "port_int";
97 private static final String SEG_ID = "2";
98 private static final String CIDR = "192.168.100.0/24";
99 private static final String OFPort = "OFPort|45";
100 private static final String IP_MASK = "127.0.0.1/32";
102 @SuppressWarnings("rawtypes")
103 private Class floatingIpClass;
104 private Object floatingIpObject;
107 public void setUp() throws Exception{
108 neutronL3Adapter = PowerMockito.mock(NeutronL3Adapter.class, Mockito.CALLS_REAL_METHODS);
110 // init instance variables
111 MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter, true);
113 // floating ip (nested private class from NeutronL3Adapter)
114 floatingIpClass = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
115 floatingIpObject = createFloatingIpObject();
119 public void testUpdateExternalRouterMac() throws Exception {
120 // Suppress the called to these functions
121 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "flushExistingIpRewrite"));
122 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "rebuildExistingIpRewrite"));
124 neutronL3Adapter.updateExternalRouterMac(EXTERNAL_ROUTER_MAC_UPDATE);
126 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("flushExistingIpRewrite");
127 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("rebuildExistingIpRewrite");
131 public void testhandleNeutronSubnetEvent() throws Exception {
132 // Nothing to be done here
133 neutronL3Adapter.handleNeutronSubnetEvent(mock(NeutronSubnet.class), Action.ADD);
137 public void testHandleNeutronPortEvent() throws Exception {
138 Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<>();
140 Neutron_IPs neutronIP = mock(Neutron_IPs.class);
141 when(neutronIP.getSubnetUUID()).thenReturn(UUID);
142 List<Neutron_IPs> list_neutronIP = new ArrayList<>();
143 list_neutronIP.add(neutronIP);
144 NeutronPort neutronPort = mock(NeutronPort.class);
145 when(neutronPort.getDeviceOwner()).thenReturn(OWNER_ROUTER_INTERFACE);
146 when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
148 // init instance variables
149 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , mock(INeutronPortCRUD.class));
150 subnetIdToRouterInterfaceCache.put(UUID, mock(NeutronRouter_Interface.class));
151 MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
153 // Suppress the called to these functions
154 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "updateL3ForNeutronPort", NeutronPort.class, boolean.class));
155 Mockito.doNothing().when(neutronL3Adapter).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), any(Action.class));
157 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
158 Mockito.verify(neutronL3Adapter).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), eq(Action.ADD));
160 when(neutronPort.getDeviceOwner()).thenReturn("");
161 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
162 Mockito.verify(neutronL3Adapter, times(2)).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), eq(Action.ADD));
163 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(false));
165 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
166 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(true));
170 public void testhandleNeutronRouterEvent() throws Exception {
171 // Nothing to be done here
172 neutronL3Adapter.handleNeutronRouterEvent(mock(NeutronRouter.class), Action.ADD);
176 public void testHandleNeutronRouterInterfaceEvent() throws Exception {
178 NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
179 when(neutronRouterInterface.getPortUUID()).thenReturn(UUID);
180 when(neutronRouterInterface.getSubnetUUID()).thenReturn(UUID);
182 Neutron_IPs neutronIP = mock(Neutron_IPs.class);
183 when(neutronIP.getSubnetUUID()).thenReturn(UUID);
184 NeutronPort neutronPort = mock(NeutronPort.class);
185 List<Neutron_IPs> list_neutronIP = new ArrayList<>();
186 list_neutronIP.add(neutronIP);
187 when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
188 List<NeutronPort> list_neutronPort = new ArrayList<>();
189 list_neutronPort.add(neutronPort);
190 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
191 when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
193 // init instance variables
194 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
196 // Suppress the called to these functions
197 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForNeutronRouterInterface", NeutronRouter_Interface.class, Boolean.class));
198 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "updateL3ForNeutronPort", NeutronPort.class, boolean.class));
200 neutronL3Adapter.handleNeutronRouterInterfaceEvent(mock(NeutronRouter.class), neutronRouterInterface, Action.ADD);
201 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", any(NeutronRouter_Interface.class), eq(false));
202 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(false));
204 neutronL3Adapter.handleNeutronRouterInterfaceEvent(mock(NeutronRouter.class), neutronRouterInterface, Action.DELETE);
205 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", any(NeutronRouter_Interface.class), eq(true));
206 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(true));
210 public void testHandleNeutronFloatingIPEvent() throws Exception {
212 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
213 when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
214 when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
215 when(neutronFloatingIP.getID()).thenReturn(UUID);
217 // Suppress the called to these functions
218 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPArpAdd", NeutronFloatingIP.class));
219 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPInbound", NeutronFloatingIP.class, Action.class));
220 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPOutbound", NeutronFloatingIP.class, Action.class));
221 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPArpDelete", String.class));
223 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
224 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPArpAdd", any(NeutronFloatingIP.class));
225 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPInbound", any(NeutronFloatingIP.class), eq(Action.ADD));
226 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPOutbound", any(NeutronFloatingIP.class), eq(Action.ADD));
228 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
229 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPArpDelete", anyString());
230 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPInbound", any(NeutronFloatingIP.class), eq(Action.DELETE));
231 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPOutbound", any(NeutronFloatingIP.class), eq(Action.DELETE));
234 @SuppressWarnings({ "unchecked", "rawtypes" })
236 public void testProgramFlowsForFloatingIPInbound() throws Exception {
237 Map floatIpDataMapCache = new HashMap();
239 NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
240 when(neutronFloatingIp.getID()).thenReturn(ID);
242 // init instance variables
243 floatIpDataMapCache .put(ID, floatingIpObject);
244 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
246 // Suppress the called to these functions
247 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage1", Long.class, Long.class, String.class, String.class, String.class, Action.class));
249 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPInbound", neutronFloatingIp, Action.ADD);
250 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage1", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
253 @SuppressWarnings({ "unchecked", "rawtypes" })
255 public void testProgramFlowsForFloatingIPOutbound() throws Exception {
256 Map floatIpDataMapCache = new HashMap();
258 NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
259 when(neutronFloatingIp.getID()).thenReturn(ID);
261 // init instance variables
262 floatIpDataMapCache.put(ID, floatingIpObject);
263 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
265 // Suppress the called to these functions
266 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
268 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPOutbound", neutronFloatingIp, Action.ADD);
269 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
272 @SuppressWarnings({ "unchecked", "rawtypes" })
274 public void testFlushExistingIpRewrite() throws Exception {
275 Map floatIpDataMapCache = new HashMap();
277 // init instance variables
278 floatIpDataMapCache.put(ID, floatingIpObject);
279 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
281 // Suppress the called to these functions
282 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
283 PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
284 Whitebox.invokeMethod(neutronL3Adapter, "flushExistingIpRewrite");
285 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.DELETE));
288 @SuppressWarnings({ "unchecked", "rawtypes" })
290 public void testRebuildExistingIpRewrite() throws Exception {
291 Map floatIpDataMapCache = new HashMap();
293 // init instance variables
294 floatIpDataMapCache.put(ID, floatingIpObject);
295 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
297 // Suppress the called to these functions
298 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
300 PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
301 Whitebox.invokeMethod(neutronL3Adapter, "rebuildExistingIpRewrite");
302 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
305 @SuppressWarnings({ "unchecked", "rawtypes" })
307 public void testProgramFlowsForFloatingIPArpAdd() throws Exception {
308 Map<String, Pair> neutronPortToDpIdCache = new HashMap<>();
309 Map<String, String> networkIdToRouterMacCache = new HashMap<>();
310 Map floatIpDataMapCache = new HashMap();
312 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
313 when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
314 when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
315 when(neutronFloatingIP.getPortUUID()).thenReturn(UUID);
316 NeutronPort neutronPort = mock(NeutronPort.class);
317 when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
318 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
319 when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
320 when(neutronNetwork.getID()).thenReturn(ID);
322 // init instance variables
323 floatIpDataMapCache.put(ID, floatingIpObject);
324 neutronPortToDpIdCache.put(UUID, mock(Pair.class));
325 networkIdToRouterMacCache.put(ID, MAC_ADDRESS);
326 INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
327 when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
328 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
329 when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
330 PowerMockito.doReturn(neutronPort).when(neutronL3Adapter, "findNeutronPortForFloatingIp", anyString());
331 PowerMockito.doReturn(15L).when(neutronL3Adapter, "findOFPortForExtPatch", anyLong());
332 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
333 MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
334 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
335 MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
336 MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
338 // Suppress the called to these functions
339 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findNeutronPortForFloatingIp", String.class));
340 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findOFPortForExtPatch", Long.class));
341 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
343 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpAdd", neutronFloatingIP);
344 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findNeutronPortForFloatingIp", anyString());
345 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findOFPortForExtPatch", anyLong());
346 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
349 @SuppressWarnings({ "unchecked", "rawtypes" })
351 public void testProgramFlowsForFloatingIPArpDelete() throws Exception {
352 Map floatIpDataMapCache = new HashMap();
354 // init instance variables
355 floatIpDataMapCache.put(ID, floatingIpObject);
356 MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
358 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
360 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpDelete", ID);
361 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
365 public void testFindNeutronPortForFloatingIp() throws Exception {
366 NeutronPort neutronPort = mock(NeutronPort.class);
367 when(neutronPort.getDeviceOwner()).thenReturn(OWNER_FLOATING_IP);
368 when(neutronPort.getDeviceID()).thenReturn(ID);
369 List<NeutronPort> list_neutronPort = new ArrayList<>();
370 list_neutronPort.add(neutronPort);
371 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
372 when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
374 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
376 assertEquals("Error, did not return the correct NeutronPort", neutronPort, Whitebox.invokeMethod(neutronL3Adapter, "findNeutronPortForFloatingIp", ID));
380 public void testFindOFPortForExtPatch() throws Exception {
381 ConfigurationService configurationService = mock(ConfigurationService.class);
382 when(configurationService.getPatchPortName(any(Pair.class))).thenReturn(PORT_INT);
383 MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
384 List<Node> nodes = new ArrayList<>();
385 nodes.add(mock(Node.class));
386 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
387 when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
388 MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
389 Southbound southbound = mock(Southbound.class);
390 when(southbound.getDataPathId(any(Node.class))).thenReturn(Long.valueOf(ID));
391 OvsdbTerminationPointAugmentation terminationPointOfBridge = mock(OvsdbTerminationPointAugmentation.class);
392 when(terminationPointOfBridge.getOfport()).thenReturn(Long.valueOf(ID));
393 when(southbound.getTerminationPointOfBridge(any(Node.class), anyString())).thenReturn(terminationPointOfBridge);
394 MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
396 assertEquals("Error, did not return the correct NeutronPort", Long.valueOf(ID), Whitebox.invokeMethod(neutronL3Adapter, "findOFPortForExtPatch", Long.valueOf(ID)));
400 public void testHandleNeutronNetworkEvent() throws Exception {
401 // Nothing to be done here
402 Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronNetworkEvent", mock(NeutronNetwork.class), Action.ADD);
406 public void testHandleInterfaceEvent() throws Exception {
407 Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
408 // init instance variables
409 TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
410 MemberModifier.field(NeutronL3Adapter.class, "tenantNetworkManager").set(neutronL3Adapter , tenantNetworkManager);
411 MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
414 NodeId nodeId = mock(NodeId.class);
415 when(nodeId.getValue()).thenReturn(ID);
416 Node node = mock(Node.class);
417 when(node.getNodeId()).thenReturn(nodeId);
419 OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
420 when(intf.getInterfaceUuid()).thenReturn(mock(Uuid.class));
421 when(intf.getName()).thenReturn(INTF_NAME);
423 NeutronPort neutronPort = mock(NeutronPort.class);
424 when(neutronPort.getPortUUID()).thenReturn(UUID);
426 when(tenantNetworkManager.getTenantPort(intf)).thenReturn(neutronPort);
428 // Suppress the called to these functions
429 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
430 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventAdd", String.class, Long.class, Uuid.class));
431 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventDelete", OvsdbTerminationPointAugmentation.class, Long.class));
433 PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
434 Mockito.doNothing().when(neutronL3Adapter).handleNeutronPortEvent(any(NeutronPort.class), any(Action.class));
436 neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.ADD);
437 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
438 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventAdd", any(String.class), anyLong(), any(Uuid.class));
439 Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.ADD);
441 neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.DELETE);
442 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventDelete", any(OvsdbTerminationPointAugmentation.class), anyLong());
443 Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.DELETE);
447 public void testHandleInterfaceEventAdd() throws Exception {
448 Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
449 // init instance variables
450 MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
451 int temp = neutronPortToDpIdCache.size();
453 Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventAdd", "", Long.valueOf(5), mock(Uuid.class));
455 assertEquals("Error, did not add the port", temp+1, neutronPortToDpIdCache.size());
458 @SuppressWarnings("unchecked")
460 public void testHandleInterfaceEventDelete() throws Exception {
461 Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
462 OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
463 Uuid uuid = mock(Uuid.class);
464 when(intf.getInterfaceUuid()).thenReturn(uuid );
465 Pair<Long, Uuid> pair = mock(Pair.class);
466 when(pair.getRight()).thenReturn(uuid);
468 // init instance variables
469 neutronPortToDpIdCache.put("key", pair);
470 MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
471 int temp = neutronPortToDpIdCache.size();
473 Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventDelete", intf, Long.valueOf(ID));
475 assertEquals("Error, did not remove the port", temp-1, neutronPortToDpIdCache.size());
479 public void testUpdateL3ForNeutronPort() throws Exception {
480 Map<String, String> networkIdToRouterMacCache = new HashMap<>();
482 Neutron_IPs neutronIp = mock(Neutron_IPs.class);
483 when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
484 List<Neutron_IPs> neutronIps = new ArrayList<>();
485 neutronIps.add(neutronIp);
486 NeutronPort neutronPort = mock(NeutronPort.class);
487 when(neutronPort.getNetworkUUID()).thenReturn(UUID);
488 when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS_2);
489 when(neutronPort.getFixedIPs()).thenReturn(neutronIps);
490 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
491 when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
492 List<Node> nodes = new ArrayList<>();
493 nodes.add(mock(Node.class));
494 PowerMockito.doReturn(15L).when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class));
496 // init instance variables
497 networkIdToRouterMacCache .put(UUID, MAC_ADDRESS);
498 MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
499 INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
500 when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
501 MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
502 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
503 when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
504 MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
505 MemberModifier.field(NeutronL3Adapter.class, "flgDistributedARPEnabled").set(neutronL3Adapter , true);
507 // Suppress the called to these functions
508 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
509 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage1", Node.class, Long.class, String.class, String.class, String.class, Action.class));
510 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
512 Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, false);
513 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
514 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
515 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
517 Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, true);
518 PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
519 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
520 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
524 public void testProgramL3ForwardingStage1() throws Exception {
526 NodeId nodeId = mock(NodeId.class);
527 when(nodeId.getValue()).thenReturn(ID);
528 Node node = mock(Node.class);
529 when(node.getNodeId()).thenReturn(nodeId);
531 // Suppress the called to these functions
532 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage2", Node.class, Long.class, String.class, String.class, String.class, Action.class));
534 // init instance variables
535 PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD)).thenReturn(new Status(StatusCode.SUCCESS));
536 PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE)).thenReturn(new Status(StatusCode.SUCCESS));
539 Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD);
540 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
543 Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.DELETE);
544 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
548 public void testProgramL3ForwardingStage2() throws Exception {
549 NodeId nodeId = mock(NodeId.class);
550 when(nodeId.getValue()).thenReturn(ID);
551 Node node = mock(Node.class);
552 when(node.getNodeId()).thenReturn(nodeId);
554 assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage2", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, MALFORM_IP, Action.ADD));
556 PowerMockito.mockStatic(InetAddress.class);
557 InetAddress inetAddress = mock(InetAddress.class);
558 PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
560 assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage2", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
564 public void testProgramFlowsForNeutronRouterInterface() throws Exception {
565 Map<String, String> networkIdToRouterMacCache = new HashMap<>();
566 Map<String, List<Neutron_IPs>> networkIdToRouterIpListCache = new HashMap<>();
567 Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<>();
569 NeutronRouter_Interface intf = mock(NeutronRouter_Interface.class);
570 when(intf.getPortUUID()).thenReturn(UUID);
571 when(intf.getSubnetUUID()).thenReturn(UUID);
572 Neutron_IPs neutronIp = mock(Neutron_IPs.class);
573 when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
574 List<Neutron_IPs> ips = new ArrayList<>();
576 NeutronPort neutronPort = mock(NeutronPort.class);
577 when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
578 when(neutronPort.getFixedIPs()).thenReturn(ips);
579 when(neutronPort.getNetworkUUID()).thenReturn(UUID);
580 NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
581 when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
582 when(neutronSubnet.getGatewayIP()).thenReturn(IP);
583 when(neutronSubnet.getCidr()).thenReturn("cidr");
584 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
585 when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
586 when(neutronNetwork.getRouterExternal()).thenReturn(false); //might change that to true
587 when(neutronNetwork.getNetworkUUID()).thenReturn(UUID);
588 NeutronRouter neutronRouter = mock(NeutronRouter.class);
590 Node node = mock(Node.class);
591 List<Node> nodes = new ArrayList<>();
594 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
595 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForNeutronRouterInterfacePair", Node.class, Long.class, NeutronRouter_Interface.class, NeutronRouter_Interface.class, NeutronNetwork.class, String.class, String.class, String.class, int.class, Action.class, Boolean.class));
596 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowForNetworkFromExternal", Node.class, Long.class, String.class, String.class, String.class, int.class, Action.class));
597 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
598 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage1", Node.class, Long.class, String.class, String.class, Action.class));
600 // init instance variables
601 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
602 PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
603 PowerMockito.when(neutronPortCache.getAllPorts()).thenReturn(new ArrayList<NeutronPort>());
604 INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
605 PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
606 INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
607 PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
608 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
609 PowerMockito.when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
610 MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
611 MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterIpListCache").set(neutronL3Adapter , networkIdToRouterIpListCache);
612 MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
613 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
614 MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
615 MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
616 MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
617 PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
619 int networkIdToRouterMacCacheSize, networkIdToRouterIpListCacheSize, subnetIdToRouterInterfaceCacheSize;
620 networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
621 networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
622 subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
624 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterface", intf, false);
626 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
627 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterfacePair", any(Node.class), anyLong(), any(NeutronRouter_Interface.class), any(NeutronRouter_Interface.class), any(NeutronNetwork.class), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD), anyBoolean());
628 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
629 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
630 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
631 assertEquals("Error, did not add the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() -1);
632 assertEquals("Error, did not add the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() -1);
633 assertEquals("Error, did not add the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() -1);
635 networkIdToRouterMacCache.put(UUID, MAC_ADDRESS);
636 networkIdToRouterIpListCache.put(UUID, ips);
637 subnetIdToRouterInterfaceCache.put(UUID, intf);
638 networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
639 networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
640 subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
642 Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronRouterInterfaceEvent", neutronRouter, intf, Action.DELETE);
644 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", intf, true);
645 PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
646 PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("programFlowsForNeutronRouterInterfacePair", any(Node.class), anyLong(), any(NeutronRouter_Interface.class), any(NeutronRouter_Interface.class), any(NeutronNetwork.class), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE), anyBoolean());
647 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
648 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
649 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
650 assertEquals("Error, did not remove the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() +1);
651 assertEquals("Error, did not remove the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() +1);
652 assertEquals("Error, did not remove the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() +1);
656 public void testProgramFlowForNetworkFromExternal() throws Exception {
657 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage1", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
659 Whitebox.invokeMethod(neutronL3Adapter, "programFlowForNetworkFromExternal", mock(Node.class), Long.valueOf(12), "", "", "", 4, Action.ADD);
661 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
665 public void testProgramFlowsForNeutronRouterInterfacePair() throws Exception {
666 NeutronRouter_Interface srcNeutronRouterInterface = mock(NeutronRouter_Interface.class);
667 when(srcNeutronRouterInterface.getSubnetUUID()).thenReturn(UUID);
668 when(srcNeutronRouterInterface.getID()).thenReturn(ID);
669 when(srcNeutronRouterInterface.getPortUUID()).thenReturn(UUID);
670 NeutronRouter_Interface dstNeutronRouterInterface = mock(NeutronRouter_Interface.class);
671 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
672 when(neutronNetwork.getTenantID()).thenReturn(ID);
673 when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
674 Neutron_IPs ip = mock(Neutron_IPs.class);
675 when(ip.getIpAddress()).thenReturn(IP);
676 List<Neutron_IPs> ips = new ArrayList<>();
680 NeutronPort neutronPort = mock(NeutronPort.class);
681 when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
682 when(neutronPort.getFixedIPs()).thenReturn(ips);
683 NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
684 when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
685 when(neutronSubnet.getCidr()).thenReturn(CIDR);
687 INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
688 PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
689 MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
690 INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
691 PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
692 MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
693 INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
694 PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
695 MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
697 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage1", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
698 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getMaskLenFromCidr", String.class));
700 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterfacePair", mock(Node.class), Long.valueOf(12), srcNeutronRouterInterface, dstNeutronRouterInterface, neutronNetwork, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD, false);
701 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
703 Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterfacePair", mock(Node.class), Long.valueOf(12), srcNeutronRouterInterface, dstNeutronRouterInterface, neutronNetwork, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD, true);
704 PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
705 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getMaskLenFromCidr", anyString());
706 PowerMockito.verifyPrivate(neutronL3Adapter, times(4)).invoke("programFlowsForNeutronRouterInterfacePair", any(Node.class), anyLong(), any(NeutronRouter_Interface.class), any(NeutronRouter_Interface.class), any(NeutronNetwork.class), anyString(), anyString(), anyString(), anyInt(), any(Action.class), eq(false)); //3 + 1 above
710 public void testProgramRouterInterfaceStage1() throws Exception {
712 NodeId nodeId = mock(NodeId.class);
713 when(nodeId.getValue()).thenReturn(ID);
714 Node node = mock(Node.class);
715 when(node.getNodeId()).thenReturn(nodeId);
717 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage2", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
719 PowerMockito.when(neutronL3Adapter, "programRouterInterfaceStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
722 Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD);
724 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
726 Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.DELETE);
728 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
732 public void testProgramRouterInterfaceStage2() throws Exception {
733 NodeId nodeId = mock(NodeId.class);
734 when(nodeId.getValue()).thenReturn(ID);
735 Node node = mock(Node.class);
736 when(node.getNodeId()).thenReturn(nodeId);
738 assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage2", node, Long.valueOf(45), SEG_ID, SEG_ID , MAC_ADDRESS, MALFORM_IP, 4, Action.ADD));
740 PowerMockito.mockStatic(InetAddress.class);
741 InetAddress inetAddress = mock(InetAddress.class);
742 PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
744 assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage2", node, Long.valueOf(45), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD));
749 public void testProgramStaticArpStage1() throws Exception {
751 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage2", Long.class, String.class, String.class, String.class, Action.class));
753 PowerMockito.when(neutronL3Adapter, "programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
756 Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
758 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
760 Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
762 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
766 public void testProgramStaticArpStage2() throws Exception {
767 assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage2", Long.valueOf(45), PORT_INT, MAC_ADDRESS, MALFORM_IP, Action.ADD));
769 PowerMockito.mockStatic(InetAddress.class);
770 InetAddress inetAddress = mock(InetAddress.class);
771 PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
773 assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage2", Long.valueOf(45), PORT_INT, MAC_ADDRESS, IP, Action.ADD));
777 public void testProgramInboundIpRewriteStage1() throws Exception {
779 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage2", Long.class, Long.class, String.class, String.class, String.class, Action.class));
781 PowerMockito.when(neutronL3Adapter, "programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
784 Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
786 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
788 Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12),PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
790 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
794 public void testProgramInboundIpRewriteStage2() throws Exception {
795 assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage2", Long.valueOf(45), Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
797 PowerMockito.mockStatic(InetAddress.class);
798 InetAddress inetAddress = mock(InetAddress.class);
799 PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
801 assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage2", Long.valueOf(45), Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
805 public void testProgramIpRewriteExclusionStage1() throws Exception {
807 NodeId nodeId = mock(NodeId.class);
808 when(nodeId.getValue()).thenReturn(ID);
809 Node node = mock(Node.class);
810 when(node.getNodeId()).thenReturn(nodeId);
812 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage2", Node.class, Long.class, String.class, String.class, Action.class));
814 PowerMockito.when(neutronL3Adapter, "programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
817 Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.ADD);
819 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
821 Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.DELETE);
823 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
827 public void testProgramIpRewriteExclusionStage2() throws Exception {
828 NodeId nodeId = mock(NodeId.class);
829 when(nodeId.getValue()).thenReturn(ID);
830 Node node = mock(Node.class);
831 when(node.getNodeId()).thenReturn(nodeId);
833 assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage2", node, Long.valueOf(45), SEG_ID, CIDR, Action.ADD));
836 @SuppressWarnings("unchecked")
838 public void testProgramOutboundIpRewriteStage1() throws Exception{
840 MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage2", floatingIpClass, Action.class));
842 PowerMockito.when(neutronL3Adapter, "programOutboundIpRewriteStage2", any(floatingIpClass), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
845 Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.ADD);
847 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.ADD));
849 Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.DELETE);
851 PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.DELETE));
855 public void testPrepareProgramOutboundIpRewriteStage2() throws Exception {
856 assertEquals("Error, did not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
858 PowerMockito.mockStatic(InetAddress.class);
859 InetAddress inetAddress = mock(InetAddress.class);
860 PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
862 assertEquals("Error, did not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
866 public void testGetMaskLenFromCidr() throws Exception {
867 assertEquals("Error, did not return the correct mask", 32, Whitebox.invokeMethod(neutronL3Adapter, "getMaskLenFromCidr", IP_MASK));
871 public void testGetDpidForIntegrationBridge() throws Exception {
872 Southbound southbound = mock(Southbound.class);
873 ConfigurationService configurationService = mock(ConfigurationService.class);
875 MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
876 MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
878 PowerMockito.when(southbound.getBridge(any(Node.class), anyString())).thenReturn(mock(OvsdbBridgeAugmentation.class));
879 PowerMockito.when(configurationService.getIntegrationBridgeName()).thenReturn("");
880 PowerMockito.when(southbound.getDataPathId(any(Node.class))).thenReturn(45L);
882 assertEquals("Error, did not return the correct Dpid", 45L, Whitebox.invokeMethod(neutronL3Adapter, "getDpidForIntegrationBridge", mock(Node.class)));
886 public void testencodeExcplicitOFPort() throws Exception {
887 assertEquals("Error, did not correctly encode the port", OFPort, NeutronL3Adapter.encodeExcplicitOFPort(45L));
891 public void testSetDependencies() throws Exception {
892 TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
893 ConfigurationService configurationService = mock(ConfigurationService.class);
894 ArpProvider arpProvider = mock(ArpProvider.class);
895 InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
896 OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
897 RoutingProvider routingProvider = mock(RoutingProvider.class);
898 L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
899 NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
900 Southbound southbound = mock(Southbound.class);
902 PowerMockito.mockStatic(ServiceHelper.class);
903 PowerMockito.when(ServiceHelper.getGlobalInstance(TenantNetworkManager.class, neutronL3Adapter)).thenReturn(tenantNetworkManager);
904 PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, neutronL3Adapter)).thenReturn(configurationService);
905 PowerMockito.when(ServiceHelper.getGlobalInstance(ArpProvider.class, neutronL3Adapter)).thenReturn(arpProvider);
906 PowerMockito.when(ServiceHelper.getGlobalInstance(InboundNatProvider.class, neutronL3Adapter)).thenReturn(inboundNatProvider);
907 PowerMockito.when(ServiceHelper.getGlobalInstance(OutboundNatProvider.class, neutronL3Adapter)).thenReturn(outboundNatProvider);
908 PowerMockito.when(ServiceHelper.getGlobalInstance(RoutingProvider.class, neutronL3Adapter)).thenReturn(routingProvider);
909 PowerMockito.when(ServiceHelper.getGlobalInstance(L3ForwardingProvider.class, neutronL3Adapter)).thenReturn(l3ForwardingProvider);
910 PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, neutronL3Adapter)).thenReturn(nodeCacheManager);
911 PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, neutronL3Adapter)).thenReturn(southbound);
913 neutronL3Adapter.setDependencies(mock(ServiceReference.class));
915 assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
916 assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
917 assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
918 assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
919 assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
920 assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
921 assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
922 assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
923 assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
927 public void testSetDependenciesObject() throws Exception{
928 MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , false);
930 INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
931 neutronL3Adapter.setDependencies(iNeutronNetworkCRUD);
932 assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
934 INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
935 neutronL3Adapter.setDependencies(iNeutronPortCRUD);
936 assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
938 INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
939 neutronL3Adapter.setDependencies(iNeutronSubnetCRUD);
940 assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
942 ArpProvider arpProvider = mock(ArpProvider.class);
943 neutronL3Adapter.setDependencies(arpProvider);
944 assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
946 InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
947 neutronL3Adapter.setDependencies(inboundNatProvider);
948 assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
950 OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
951 neutronL3Adapter.setDependencies(outboundNatProvider);
952 assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
954 RoutingProvider routingProvider = mock(RoutingProvider.class);
955 neutronL3Adapter.setDependencies(routingProvider);
956 assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
958 L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
959 neutronL3Adapter.setDependencies(l3ForwardingProvider);
960 assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
962 MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , true);
965 private Object getField(String fieldName) throws Exception {
966 Field field = NeutronL3Adapter.class.getDeclaredField(fieldName);
967 field.setAccessible(true);
968 return field.get(neutronL3Adapter);
971 @SuppressWarnings("rawtypes")
972 private Object createFloatingIpObject() throws Exception{
973 Class clazz = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
974 Constructor [] constructors = clazz.getConstructors();
975 Constructor c = constructors[0];
976 return c.newInstance(neutronL3Adapter, 415L, 415L, "a", "b", "c", "d", "e");