BUG 5164 Cleanup Floating IP Rules when VM is deleted.
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / NeutronL3AdapterTest.java
1 /*
2  * Copyright (c) 2015, 2016 Inocybe and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
10
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;
21
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;
29 import java.util.Map;
30 import java.util.Set;
31
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.INeutronFloatingIPCRUD;
46 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
47 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
48 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
49 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
50 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
51 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
52 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
53 import org.opendaylight.ovsdb.openstack.netvirt.api.EventDispatcher;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
55 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
56 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
57 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
58 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
59 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
60 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
61 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
62 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
63 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
64 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
67 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
69
70 import org.osgi.framework.ServiceReference;
71 import org.powermock.api.mockito.PowerMockito;
72 import org.powermock.api.support.membermodification.MemberMatcher;
73 import org.powermock.api.support.membermodification.MemberModifier;
74 import org.powermock.core.classloader.annotations.PrepareForTest;
75 import org.powermock.modules.junit4.PowerMockRunner;
76 import org.powermock.reflect.Whitebox;
77
78 /**
79  * Unit test for {@link NeutronL3Adapter}
80  */
81 @PrepareForTest({ServiceHelper.class, InetAddress.class, NeutronL3Adapter.class, DistributedArpService.class})
82 @RunWith(PowerMockRunner.class)
83 public class NeutronL3AdapterTest {
84
85     @Mock private NeutronL3Adapter neutronL3Adapter;
86     @Mock private DistributedArpService distributedArpService;
87
88     private static final String ID = "45";
89     private static final String IP = "127.0.0.1";
90     private static final String MALFORM_IP = "127.0.0.1.5";
91     private static final String INTF_NAME = "br-int";
92     private static final String EXTERNAL_ROUTER_MAC_UPDATE = "";
93     private static final String UUID = "7da709ff-397f-4778-a0e8-994811272fdb";
94     private static final String FIXED_IP_ADDRESS = "192.168.1.0";
95     private static final String FLOATING_IP_ADDRESS = "192.168.1.1";
96     private static final String OWNER_ROUTER_INTERFACE = "network:router_interface";
97     private static final String OWNER_FLOATING_IP = "network:floatingip";
98     private static final String MAC_ADDRESS = "00:00:5E:00:02:01";
99     private static final String MAC_ADDRESS_2 = "00:00:5E:00:02:02";
100     private static final String PORT_INT = "port_int";
101     private static final String SEG_ID = "2";
102     private static final String CIDR = "192.168.100.0/24";
103     private static final String OFPort = "OFPort|45";
104     private static final String IP_MASK = "127.0.0.1/32";
105
106     @SuppressWarnings("rawtypes")
107     private Class floatingIpClass;
108     private Object floatingIpObject;
109
110     @Before
111     public void setUp() throws Exception{
112         neutronL3Adapter = PowerMockito.mock(NeutronL3Adapter.class, Mockito.CALLS_REAL_METHODS);
113         Whitebox.setInternalState(neutronL3Adapter, "distributedArpService", distributedArpService);
114         // init instance variables
115         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter, true);
116
117         // floating ip (nested private class from NeutronL3Adapter)
118         floatingIpClass = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
119         floatingIpObject = createFloatingIpObject();
120     }
121
122     @Test
123     public void testUpdateExternalRouterMac() throws Exception {
124         // Suppress the called to these functions
125         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "flushExistingIpRewrite"));
126         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "rebuildExistingIpRewrite"));
127
128         neutronL3Adapter.updateExternalRouterMac(EXTERNAL_ROUTER_MAC_UPDATE);
129
130         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("flushExistingIpRewrite");
131         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("rebuildExistingIpRewrite");
132     }
133
134     @Test
135     public void testhandleNeutronSubnetEvent() throws Exception {
136         // Nothing to be done here
137         neutronL3Adapter.handleNeutronSubnetEvent(mock(NeutronSubnet.class), Action.ADD);
138     }
139
140     @Test
141     public void testHandleNeutronPortEvent() throws Exception {
142         Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<>();
143         Set<NeutronPort> portCleanupCache = new HashSet<>();
144         // Mock variables
145         Neutron_IPs neutronIP = mock(Neutron_IPs.class);
146         when(neutronIP.getSubnetUUID()).thenReturn(UUID);
147         List<Neutron_IPs> list_neutronIP = new ArrayList<>();
148         list_neutronIP.add(neutronIP);
149         NeutronPort neutronPort = mock(NeutronPort.class);
150         when(neutronPort.getDeviceOwner()).thenReturn(OWNER_ROUTER_INTERFACE);
151         when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
152         INeutronFloatingIPCRUD neutronFloatingIpCache = mock(INeutronFloatingIPCRUD.class);
153
154         // init instance variables
155         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , mock(INeutronPortCRUD.class));
156         subnetIdToRouterInterfaceCache.put(UUID, mock(NeutronRouter_Interface.class));
157         MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
158         MemberModifier.field(NeutronL3Adapter.class, "portCleanupCache").set(neutronL3Adapter , portCleanupCache);
159         MemberModifier.field(NeutronL3Adapter.class, "neutronFloatingIpCache").set(neutronL3Adapter , neutronFloatingIpCache);
160
161         // Suppress the called to these functions
162         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "updateL3ForNeutronPort", NeutronPort.class, boolean.class));
163         Mockito.doNothing().when(neutronL3Adapter).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), any(Action.class));
164
165         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
166         Mockito.verify(neutronL3Adapter).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), eq(Action.ADD));
167
168         when(neutronPort.getDeviceOwner()).thenReturn("");
169         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
170         Mockito.verify(neutronL3Adapter, times(2)).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), eq(Action.ADD));
171         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(false));
172
173         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
174         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(true));
175     }
176
177     @Test
178     public void testhandleNeutronRouterEvent() throws Exception {
179         // Nothing to be done here
180         neutronL3Adapter.handleNeutronRouterEvent(mock(NeutronRouter.class), Action.ADD);
181     }
182
183     @Test
184     public void testHandleNeutronRouterInterfaceEvent() throws Exception {
185         // Mock variables
186         NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
187         when(neutronRouterInterface.getPortUUID()).thenReturn(UUID);
188         when(neutronRouterInterface.getSubnetUUID()).thenReturn(UUID);
189
190         Neutron_IPs neutronIP = mock(Neutron_IPs.class);
191         when(neutronIP.getSubnetUUID()).thenReturn(UUID);
192         NeutronPort neutronPort = mock(NeutronPort.class);
193         List<Neutron_IPs> list_neutronIP = new ArrayList<>();
194         list_neutronIP.add(neutronIP);
195         when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
196         List<NeutronPort> list_neutronPort = new ArrayList<>();
197         list_neutronPort.add(neutronPort);
198         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
199         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
200
201         // init instance variables
202         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
203
204         // Suppress the called to these functions
205         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForNeutronRouterInterface", NeutronRouter_Interface.class, Boolean.class));
206         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "updateL3ForNeutronPort", NeutronPort.class, boolean.class));
207
208         neutronL3Adapter.handleNeutronRouterInterfaceEvent(mock(NeutronRouter.class), neutronRouterInterface, Action.ADD);
209         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", any(NeutronRouter_Interface.class), eq(false));
210         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(false));
211
212         neutronL3Adapter.handleNeutronRouterInterfaceEvent(mock(NeutronRouter.class), neutronRouterInterface, Action.DELETE);
213         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", any(NeutronRouter_Interface.class), eq(true));
214         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(true));
215     }
216
217     @Test
218     public void testHandleNeutronFloatingIPEvent() throws Exception {
219         // Mock variables
220         NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
221         when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
222         when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
223         when(neutronFloatingIP.getID()).thenReturn(UUID);
224
225         // Suppress the called to these functions
226         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPArpAdd", NeutronFloatingIP.class));
227         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPInbound", NeutronFloatingIP.class, Action.class));
228         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPOutbound", NeutronFloatingIP.class, Action.class));
229         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForFloatingIPArpDelete", String.class));
230
231         neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
232         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPArpAdd", any(NeutronFloatingIP.class));
233         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPInbound", any(NeutronFloatingIP.class), eq(Action.ADD));
234         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPOutbound", any(NeutronFloatingIP.class), eq(Action.ADD));
235
236         neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
237         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPArpDelete", anyString());
238         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPInbound", any(NeutronFloatingIP.class), eq(Action.DELETE));
239         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForFloatingIPOutbound", any(NeutronFloatingIP.class), eq(Action.DELETE));
240     }
241
242     @SuppressWarnings({ "unchecked", "rawtypes" })
243     @Test
244     public void testProgramFlowsForFloatingIPInbound() throws Exception {
245         Map floatIpDataMapCache = new HashMap();
246
247         NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
248         when(neutronFloatingIp.getID()).thenReturn(ID);
249
250         // init instance variables
251         floatIpDataMapCache .put(ID, floatingIpObject);
252         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
253
254         // Suppress the called to these functions
255         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage1", Long.class, Long.class, String.class, String.class, String.class, Action.class));
256
257         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPInbound", neutronFloatingIp, Action.ADD);
258         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage1", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
259     }
260
261     @SuppressWarnings({ "unchecked", "rawtypes" })
262     @Test
263     public void testProgramFlowsForFloatingIPOutbound() throws Exception {
264         Map floatIpDataMapCache = new HashMap();
265
266         NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
267         when(neutronFloatingIp.getID()).thenReturn(ID);
268
269         // init instance variables
270         floatIpDataMapCache.put(ID, floatingIpObject);
271         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
272
273         // Suppress the called to these functions
274         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
275
276         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPOutbound", neutronFloatingIp, Action.ADD);
277         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
278     }
279
280     @SuppressWarnings({ "unchecked", "rawtypes" })
281     @Test
282     public void testFlushExistingIpRewrite() throws Exception {
283         Map floatIpDataMapCache = new HashMap();
284
285         // init instance variables
286         floatIpDataMapCache.put(ID, floatingIpObject);
287         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
288
289         // Suppress the called to these functions
290         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
291         PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
292         Whitebox.invokeMethod(neutronL3Adapter, "flushExistingIpRewrite");
293         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.DELETE));
294     }
295
296     @SuppressWarnings({ "unchecked", "rawtypes" })
297     @Test
298     public void testRebuildExistingIpRewrite() throws Exception {
299         Map floatIpDataMapCache = new HashMap();
300
301         // init instance variables
302         floatIpDataMapCache.put(ID, floatingIpObject);
303         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
304
305         // Suppress the called to these functions
306         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
307
308         PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
309         Whitebox.invokeMethod(neutronL3Adapter, "rebuildExistingIpRewrite");
310         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
311     }
312
313     @SuppressWarnings({ "unchecked", "rawtypes" })
314     @Test
315     public void testProgramFlowsForFloatingIPArpAdd() throws Exception {
316         Map<String, Pair> neutronPortToDpIdCache = new HashMap<>();
317         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
318         Map floatIpDataMapCache = new HashMap();
319
320         NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
321         when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
322         when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
323         when(neutronFloatingIP.getPortUUID()).thenReturn(UUID);
324         NeutronPort neutronPort = mock(NeutronPort.class);
325         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
326         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
327         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
328         when(neutronNetwork.getID()).thenReturn(ID);
329
330         // init instance variables
331         floatIpDataMapCache.put(ID, floatingIpObject);
332         neutronPortToDpIdCache.put(UUID, mock(Pair.class));
333         networkIdToRouterMacCache.put(ID, MAC_ADDRESS);
334         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
335         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
336         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
337         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
338         PowerMockito.doReturn(neutronPort).when(neutronL3Adapter, "findNeutronPortForFloatingIp", anyString());
339         PowerMockito.doReturn(15L).when(neutronL3Adapter, "findOFPortForExtPatch", anyLong());
340         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
341         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
342         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
343         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
344         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
345
346         // Suppress the called to these functions
347         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findNeutronPortForFloatingIp", String.class));
348         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findOFPortForExtPatch", Long.class));
349
350         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpAdd", neutronFloatingIP);
351         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findNeutronPortForFloatingIp", anyString());
352         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findOFPortForExtPatch", anyLong());
353     }
354
355     @SuppressWarnings({ "unchecked", "rawtypes" })
356     @Test
357     public void testProgramFlowsForFloatingIPArpDelete() throws Exception {
358         Map floatIpDataMapCache = new HashMap();
359
360      // init instance variables
361         floatIpDataMapCache.put(ID, floatingIpObject);
362         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
363
364         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpDelete", ID);
365     }
366
367     @Test
368     public void testFindNeutronPortForFloatingIp() throws Exception {
369         NeutronPort neutronPort = mock(NeutronPort.class);
370         when(neutronPort.getDeviceOwner()).thenReturn(OWNER_FLOATING_IP);
371         when(neutronPort.getDeviceID()).thenReturn(ID);
372         List<NeutronPort> list_neutronPort = new ArrayList<>();
373         list_neutronPort.add(neutronPort);
374         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
375         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
376
377         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
378
379         assertEquals("Error, did not return the correct NeutronPort", neutronPort, Whitebox.invokeMethod(neutronL3Adapter, "findNeutronPortForFloatingIp", ID));
380     }
381
382     @Test
383     public void testFindOFPortForExtPatch() throws Exception {
384         ConfigurationService configurationService = mock(ConfigurationService.class);
385         when(configurationService.getPatchPortName(any(Pair.class))).thenReturn(PORT_INT);
386         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
387         List<Node> nodes = new ArrayList<>();
388         nodes.add(mock(Node.class));
389         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
390         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
391         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
392         Southbound southbound = mock(Southbound.class);
393         when(southbound.getDataPathId(any(Node.class))).thenReturn(Long.valueOf(ID));
394         OvsdbTerminationPointAugmentation terminationPointOfBridge = mock(OvsdbTerminationPointAugmentation.class);
395         when(terminationPointOfBridge.getOfport()).thenReturn(Long.valueOf(ID));
396         when(southbound.getTerminationPointOfBridge(any(Node.class), anyString())).thenReturn(terminationPointOfBridge);
397         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
398
399         assertEquals("Error, did not return the correct NeutronPort", Long.valueOf(ID), Whitebox.invokeMethod(neutronL3Adapter, "findOFPortForExtPatch", Long.valueOf(ID)));
400     }
401
402     @Test
403     public void testHandleNeutronNetworkEvent() throws Exception {
404         // Nothing to be done here
405         Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronNetworkEvent", mock(NeutronNetwork.class), Action.ADD);
406     }
407
408     @Test
409     public void testHandleInterfaceEvent() throws Exception {
410         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
411         // init instance variables
412         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
413         MemberModifier.field(NeutronL3Adapter.class, "tenantNetworkManager").set(neutronL3Adapter , tenantNetworkManager);
414         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
415
416         // Mock variables
417         NodeId nodeId = mock(NodeId.class);
418         when(nodeId.getValue()).thenReturn(ID);
419         Node node = mock(Node.class);
420         when(node.getNodeId()).thenReturn(nodeId);
421
422         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
423         when(intf.getInterfaceUuid()).thenReturn(mock(Uuid.class));
424         when(intf.getName()).thenReturn(INTF_NAME);
425
426         NeutronPort neutronPort = mock(NeutronPort.class);
427         when(neutronPort.getPortUUID()).thenReturn(UUID);
428
429         when(tenantNetworkManager.getTenantPort(intf)).thenReturn(neutronPort);
430
431         // Suppress the called to these functions
432         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
433         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventAdd", String.class, Long.class, Uuid.class));
434         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventDelete", OvsdbTerminationPointAugmentation.class, Long.class));
435         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "storePortInCleanupCache", NeutronPort.class));
436
437         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
438         Mockito.doNothing().when(neutronL3Adapter).handleNeutronPortEvent(any(NeutronPort.class), any(Action.class));
439
440         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.ADD);
441         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
442         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventAdd", any(String.class), anyLong(), any(Uuid.class));
443         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.ADD);
444
445         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.DELETE);
446         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventDelete", any(OvsdbTerminationPointAugmentation.class), anyLong());
447         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.DELETE);
448     }
449
450     @Test
451     public void testHandleInterfaceEventAdd() throws Exception {
452         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
453         // init instance variables
454         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
455         int temp = neutronPortToDpIdCache.size();
456
457         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventAdd", "", Long.valueOf(5), mock(Uuid.class));
458
459         assertEquals("Error, did not add the port", temp+1, neutronPortToDpIdCache.size());
460     }
461
462     @SuppressWarnings("unchecked")
463     @Test
464     public void testHandleInterfaceEventDelete() throws Exception {
465         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
466         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
467         Uuid uuid = mock(Uuid.class);
468         when(intf.getInterfaceUuid()).thenReturn(uuid );
469         Pair<Long, Uuid> pair = mock(Pair.class);
470         when(pair.getRight()).thenReturn(uuid);
471
472         // init instance variables
473         neutronPortToDpIdCache.put("key", pair);
474         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
475         int temp = neutronPortToDpIdCache.size();
476
477         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventDelete", intf, Long.valueOf(ID));
478
479         assertEquals("Error, did not remove the port", temp-1, neutronPortToDpIdCache.size());
480     }
481
482     @Test
483     public void testUpdateL3ForNeutronPort() throws Exception {
484         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
485
486         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
487         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
488         List<Neutron_IPs> neutronIps = new ArrayList<>();
489         neutronIps.add(neutronIp);
490         NeutronPort neutronPort = mock(NeutronPort.class);
491         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
492         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS_2);
493         when(neutronPort.getFixedIPs()).thenReturn(neutronIps);
494         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
495         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
496         List<Node> nodes = new ArrayList<>();
497         nodes.add(mock(Node.class));
498         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
499         MemberModifier.field(NeutronL3Adapter.class, "tenantNetworkManager").set(neutronL3Adapter , tenantNetworkManager);
500         when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), eq(ID))).thenReturn(true);
501         PowerMockito.doReturn(15L).when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class));
502
503         // init instance variables
504         networkIdToRouterMacCache .put(UUID, MAC_ADDRESS);
505         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
506         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
507         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
508         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
509         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
510         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
511         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
512
513         // Suppress the called to these functions
514         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
515         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage1", Node.class, Long.class, String.class, String.class, String.class, Action.class));
516
517         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, false);
518         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
519         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
520
521         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, true);
522         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
523         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
524     }
525
526     @Test
527     public void testProgramL3ForwardingStage1() throws Exception {
528
529         NodeId nodeId = mock(NodeId.class);
530         when(nodeId.getValue()).thenReturn(ID);
531         Node node = mock(Node.class);
532         when(node.getNodeId()).thenReturn(nodeId);
533
534         // Suppress the called to these functions
535         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage2", Node.class, Long.class, String.class, String.class, String.class, Action.class));
536
537         // init instance variables
538         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD)).thenReturn(new Status(StatusCode.SUCCESS));
539         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE)).thenReturn(new Status(StatusCode.SUCCESS));
540
541
542         Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD);
543         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
544
545
546         Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.DELETE);
547         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
548     }
549
550     @Test
551     public void testProgramL3ForwardingStage2() throws Exception {
552         NodeId nodeId = mock(NodeId.class);
553         when(nodeId.getValue()).thenReturn(ID);
554         Node node = mock(Node.class);
555         when(node.getNodeId()).thenReturn(nodeId);
556
557         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));
558
559         PowerMockito.mockStatic(InetAddress.class);
560         InetAddress inetAddress = mock(InetAddress.class);
561         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
562
563         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     }
565
566     @Test
567     public void testProgramFlowsForNeutronRouterInterface() throws Exception {
568         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
569         Map<String, List<Neutron_IPs>> networkIdToRouterIpListCache = new HashMap<>();
570         Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<>();
571
572         NeutronRouter_Interface intf = mock(NeutronRouter_Interface.class);
573         when(intf.getPortUUID()).thenReturn(UUID);
574         when(intf.getSubnetUUID()).thenReturn(UUID);
575         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
576         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
577         List<Neutron_IPs> ips = new ArrayList<>();
578         ips.add(neutronIp);
579         NeutronPort neutronPort = mock(NeutronPort.class);
580         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
581         when(neutronPort.getFixedIPs()).thenReturn(ips);
582         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
583         NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
584         when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
585         when(neutronSubnet.getGatewayIP()).thenReturn(IP);
586         when(neutronSubnet.getCidr()).thenReturn("cidr");
587         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
588         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
589         when(neutronNetwork.getRouterExternal()).thenReturn(false); //might change that to true
590         when(neutronNetwork.getNetworkUUID()).thenReturn(UUID);
591         NeutronRouter neutronRouter = mock(NeutronRouter.class);
592
593         Node node = mock(Node.class);
594         List<Node> nodes = new ArrayList<>();
595         nodes.add(node);
596
597         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
598         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));
599         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowForNetworkFromExternal", Node.class, Long.class, String.class, String.class, String.class, int.class, Action.class));
600         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage1", Node.class, Long.class, String.class, String.class, Action.class));
601
602         // init instance variables
603         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
604         PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
605         PowerMockito.when(neutronPortCache.getAllPorts()).thenReturn(new ArrayList<NeutronPort>());
606         INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
607         PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
608         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
609         PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
610         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
611         PowerMockito.when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
612         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
613         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterIpListCache").set(neutronL3Adapter , networkIdToRouterIpListCache);
614         MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
615         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
616         MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
617         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
618         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
619         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
620
621         int networkIdToRouterMacCacheSize, networkIdToRouterIpListCacheSize, subnetIdToRouterInterfaceCacheSize;
622         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
623         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
624         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
625
626         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterface", intf, false);
627
628         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
629         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());
630         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
631         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
632         assertEquals("Error, did not add the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() -1);
633         assertEquals("Error, did not add the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() -1);
634         assertEquals("Error, did not add the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() -1);
635
636         networkIdToRouterMacCache.put(UUID, MAC_ADDRESS);
637         networkIdToRouterIpListCache.put(UUID, ips);
638         subnetIdToRouterInterfaceCache.put(UUID, intf);
639         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
640         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
641         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
642
643         Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronRouterInterfaceEvent", neutronRouter, intf, Action.DELETE);
644
645         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", intf, true);
646         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
647         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());
648         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), 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);
653     }
654
655     @Test
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));
658
659         Whitebox.invokeMethod(neutronL3Adapter, "programFlowForNetworkFromExternal", mock(Node.class), Long.valueOf(12), "", "", "", 4, Action.ADD);
660
661         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
662     }
663
664     @Test
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<>();
677         ips.add(ip);
678         ips.add(ip);
679         ips.add(ip);
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);
686
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);
696
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));
699
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));
702
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
707     }
708
709     @Test
710     public void testProgramRouterInterfaceStage1() throws Exception {
711
712         NodeId nodeId = mock(NodeId.class);
713         when(nodeId.getValue()).thenReturn(ID);
714         Node node = mock(Node.class);
715         when(node.getNodeId()).thenReturn(nodeId);
716
717         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage2", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
718
719         PowerMockito.when(neutronL3Adapter, "programRouterInterfaceStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
720
721
722         Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD);
723
724         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
725
726         Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.DELETE);
727
728         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
729     }
730
731     @Test
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);
737
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));
739
740         PowerMockito.mockStatic(InetAddress.class);
741         InetAddress inetAddress = mock(InetAddress.class);
742         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
743
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));
745
746     }
747
748     @Test
749     public void testProgramInboundIpRewriteStage1() throws Exception {
750
751         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage2", Long.class, Long.class, String.class, String.class, String.class, Action.class));
752
753         PowerMockito.when(neutronL3Adapter, "programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
754
755
756         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
757
758         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
759
760         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12),PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
761
762         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
763     }
764
765     @Test
766     public void testProgramInboundIpRewriteStage2() throws Exception {
767         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));
768
769         PowerMockito.mockStatic(InetAddress.class);
770         InetAddress inetAddress = mock(InetAddress.class);
771         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
772
773         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));
774     }
775
776     @Test
777     public void testProgramIpRewriteExclusionStage1() throws Exception {
778
779         NodeId nodeId = mock(NodeId.class);
780         when(nodeId.getValue()).thenReturn(ID);
781         Node node = mock(Node.class);
782         when(node.getNodeId()).thenReturn(nodeId);
783
784         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage2", Node.class, Long.class, String.class, String.class, Action.class));
785
786         PowerMockito.when(neutronL3Adapter, "programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
787
788
789         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.ADD);
790
791         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
792
793         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.DELETE);
794
795         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
796     }
797
798     @Test
799     public void testProgramIpRewriteExclusionStage2() throws Exception {
800         NodeId nodeId = mock(NodeId.class);
801         when(nodeId.getValue()).thenReturn(ID);
802         Node node = mock(Node.class);
803         when(node.getNodeId()).thenReturn(nodeId);
804
805         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));
806     }
807
808     @SuppressWarnings("unchecked")
809     @Test
810     public void testProgramOutboundIpRewriteStage1() throws Exception{
811
812         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage2", floatingIpClass, Action.class));
813
814         PowerMockito.when(neutronL3Adapter, "programOutboundIpRewriteStage2", any(floatingIpClass), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
815
816
817         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.ADD);
818
819         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.ADD));
820
821         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.DELETE);
822
823         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.DELETE));
824     }
825
826     /*@Test
827     public void testPrepareProgramOutboundIpRewriteStage2() throws Exception {
828         assertEquals("Error, did not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
829
830         PowerMockito.mockStatic(InetAddress.class);
831         InetAddress inetAddress = mock(InetAddress.class);
832         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
833
834         assertEquals("Error, did not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
835     }*/
836
837     @Test
838     public void testGetMaskLenFromCidr() throws Exception {
839         assertEquals("Error, did not return the correct mask", 32, (int) Whitebox.invokeMethod(neutronL3Adapter, "getMaskLenFromCidr", IP_MASK));
840     }
841
842     @Test
843     public void testGetDpidForIntegrationBridge() throws Exception {
844         Southbound southbound = mock(Southbound.class);
845         ConfigurationService configurationService = mock(ConfigurationService.class);
846
847         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
848         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
849
850         PowerMockito.when(southbound.getBridge(any(Node.class), anyString())).thenReturn(mock(OvsdbBridgeAugmentation.class));
851         PowerMockito.when(configurationService.getIntegrationBridgeName()).thenReturn("");
852         PowerMockito.when(southbound.getDataPathId(any(Node.class))).thenReturn(45L);
853
854         assertEquals("Error, did not return the correct Dpid", 45L, (long) Whitebox.invokeMethod(neutronL3Adapter, "getDpidForIntegrationBridge", mock(Node.class)));
855     }
856
857     @Test
858     public void testencodeExcplicitOFPort() throws Exception {
859         assertEquals("Error, did not correctly encode the port", OFPort, NeutronL3Adapter.encodeExcplicitOFPort(45L));
860     }
861
862     @Test
863     public void testSetDependencies() throws Exception {
864         EventDispatcher eventDispatcher = mock(EventDispatcher.class);
865         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
866         ConfigurationService configurationService = mock(ConfigurationService.class);
867         ArpProvider arpProvider = mock(ArpProvider.class);
868         DistributedArpService distributedArpService = mock(DistributedArpService.class);
869         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
870         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
871         RoutingProvider routingProvider = mock(RoutingProvider.class);
872         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
873         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
874         Southbound southbound = mock(Southbound.class);
875
876         ServiceHelper.overrideGlobalInstance(EventDispatcher.class, eventDispatcher);
877         ServiceHelper.overrideGlobalInstance(TenantNetworkManager.class, tenantNetworkManager);
878         ServiceHelper.overrideGlobalInstance(ConfigurationService.class, configurationService);
879         ServiceHelper.overrideGlobalInstance(ArpProvider.class, arpProvider);
880         ServiceHelper.overrideGlobalInstance(DistributedArpService.class, distributedArpService);
881         ServiceHelper.overrideGlobalInstance(InboundNatProvider.class, inboundNatProvider);
882         ServiceHelper.overrideGlobalInstance(OutboundNatProvider.class, outboundNatProvider);
883         ServiceHelper.overrideGlobalInstance(RoutingProvider.class, routingProvider);
884         ServiceHelper.overrideGlobalInstance(L3ForwardingProvider.class, l3ForwardingProvider);
885         ServiceHelper.overrideGlobalInstance(NodeCacheManager.class, nodeCacheManager);
886         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
887
888         neutronL3Adapter.setDependencies(mock(ServiceReference.class));
889
890         assertEquals("Error, did not return the correct object", getAbstractHandlerField("eventDispatcher"),
891                 eventDispatcher);
892         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
893         assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
894         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
895         assertEquals("Error, did not return the correct object", getField("distributedArpService"), distributedArpService);
896         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
897         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
898         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
899         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
900         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
901         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
902     }
903
904     @Test
905     public void testSetDependenciesObject() throws Exception{
906         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , false);
907
908         INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
909         neutronL3Adapter.setDependencies(iNeutronNetworkCRUD);
910         assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
911
912         INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
913         neutronL3Adapter.setDependencies(iNeutronPortCRUD);
914         assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
915
916         INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
917         neutronL3Adapter.setDependencies(iNeutronSubnetCRUD);
918         assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
919
920         ArpProvider arpProvider = mock(ArpProvider.class);
921         neutronL3Adapter.setDependencies(arpProvider);
922         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
923
924         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
925         neutronL3Adapter.setDependencies(inboundNatProvider);
926         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
927
928         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
929         neutronL3Adapter.setDependencies(outboundNatProvider);
930         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
931
932         RoutingProvider routingProvider = mock(RoutingProvider.class);
933         neutronL3Adapter.setDependencies(routingProvider);
934         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
935
936         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
937         neutronL3Adapter.setDependencies(l3ForwardingProvider);
938         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
939
940         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , true);
941     }
942
943     private Object getAbstractHandlerField(String fieldName) throws Exception {
944         Field field = AbstractHandler.class.getDeclaredField(fieldName);
945         field.setAccessible(true);
946         return field.get(neutronL3Adapter);
947     }
948
949     private Object getField(String fieldName) throws Exception {
950         Field field = NeutronL3Adapter.class.getDeclaredField(fieldName);
951         field.setAccessible(true);
952         return field.get(neutronL3Adapter);
953     }
954
955     @SuppressWarnings("rawtypes")
956     private Object createFloatingIpObject() throws Exception{
957         Class clazz = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
958         Constructor [] constructors = clazz.getConstructors();
959         Constructor c  = constructors[0];
960         return c.newInstance(neutronL3Adapter, 415L, 415L, "a", "b", "c", "d", "e");
961     }
962 }