Add UT for SouthboundMapper and SouthboundProvider
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / impl / NeutronL3AdapterTest.java
1 /*
2  * Copyright (c) 2015 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.neutron.spi.INeutronNetworkCRUD;
39 import org.opendaylight.neutron.spi.INeutronPortCRUD;
40 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
41 import org.opendaylight.neutron.spi.NeutronFloatingIP;
42 import org.opendaylight.neutron.spi.NeutronNetwork;
43 import org.opendaylight.neutron.spi.NeutronPort;
44 import org.opendaylight.neutron.spi.NeutronRouter;
45 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
46 import org.opendaylight.neutron.spi.NeutronSubnet;
47 import org.opendaylight.neutron.spi.Neutron_IPs;
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;
66 import org.osgi.framework.BundleContext;
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;
74
75 /**
76  * Unit test for {@link NeutronL3Adapter}
77  */
78 @PrepareForTest({ServiceHelper.class, InetAddress.class, NeutronL3Adapter.class})
79 @RunWith(PowerMockRunner.class)
80 public class NeutronL3AdapterTest {
81
82     @Mock private NeutronL3Adapter neutronL3Adapter;
83
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";
101
102     @SuppressWarnings("rawtypes")
103     private Class floatingIpClass;
104     private Object floatingIpObject;
105
106     @Before
107     public void setUp() throws Exception{
108         neutronL3Adapter = PowerMockito.mock(NeutronL3Adapter.class, Mockito.CALLS_REAL_METHODS);
109
110         // init instance variables
111         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter, true);
112
113         // floating ip (nested private class from NeutronL3Adapter)
114         floatingIpClass = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
115         floatingIpObject = createFloatingIpObject();
116     }
117
118     @Test
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"));
123
124         neutronL3Adapter.updateExternalRouterMac(EXTERNAL_ROUTER_MAC_UPDATE);
125
126         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("flushExistingIpRewrite");
127         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("rebuildExistingIpRewrite");
128     }
129
130     @Test
131     public void testhandleNeutronSubnetEvent() throws Exception {
132         // Nothing to be done here
133         neutronL3Adapter.handleNeutronSubnetEvent(mock(NeutronSubnet.class), Action.ADD);
134     }
135
136     @Test
137     public void testHandleNeutronPortEvent() throws Exception {
138         Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<String, NeutronRouter_Interface>();
139         // Mock variables
140         Neutron_IPs neutronIP = mock(Neutron_IPs.class);
141         when(neutronIP.getSubnetUUID()).thenReturn(UUID);
142         List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
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);
147
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);
152
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));
156
157         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
158         Mockito.verify(neutronL3Adapter).handleNeutronRouterInterfaceEvent(any(NeutronRouter.class), any(NeutronRouter_Interface.class), eq(Action.ADD));
159
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));
164
165         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
166         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("updateL3ForNeutronPort", any(NeutronPort.class), eq(true));
167     }
168
169     @Test
170     public void testhandleNeutronRouterEvent() throws Exception {
171         // Nothing to be done here
172         neutronL3Adapter.handleNeutronRouterEvent(mock(NeutronRouter.class), Action.ADD);
173     }
174
175     @Test
176     public void testHandleNeutronRouterInterfaceEvent() throws Exception {
177         // Mock variables
178         NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
179         when(neutronRouterInterface.getPortUUID()).thenReturn(UUID);
180         when(neutronRouterInterface.getSubnetUUID()).thenReturn(UUID);
181
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<Neutron_IPs>();
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);
192
193         // init instance variables
194         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
195
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));
199
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));
203
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));
207     }
208
209     @Test
210     public void testHandleNeutronFloatingIPEvent() throws Exception {
211         // Mock variables
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);
216
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));
222
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));
227
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));
232     }
233
234     @SuppressWarnings({ "unchecked", "rawtypes" })
235     @Test
236     public void testProgramFlowsForFloatingIPInbound() throws Exception {
237         Map floatIpDataMapCache = new HashMap();
238
239         NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
240         when(neutronFloatingIp.getID()).thenReturn(ID);
241
242         // init instance variables
243         floatIpDataMapCache .put(ID, floatingIpObject);
244         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
245
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));
248
249         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPInbound", neutronFloatingIp, Action.ADD);
250         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage1", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
251     }
252
253     @SuppressWarnings({ "unchecked", "rawtypes" })
254     @Test
255     public void testProgramFlowsForFloatingIPOutbound() throws Exception {
256         Map floatIpDataMapCache = new HashMap();
257
258         NeutronFloatingIP neutronFloatingIp = mock(NeutronFloatingIP.class);
259         when(neutronFloatingIp.getID()).thenReturn(ID);
260
261         // init instance variables
262         floatIpDataMapCache.put(ID, floatingIpObject);
263         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
264
265         // Suppress the called to these functions
266         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
267
268         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPOutbound", neutronFloatingIp, Action.ADD);
269         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
270     }
271
272     @SuppressWarnings({ "unchecked", "rawtypes" })
273     @Test
274     public void testFlushExistingIpRewrite() throws Exception {
275         Map floatIpDataMapCache = new HashMap();
276
277         // init instance variables
278         floatIpDataMapCache.put(ID, floatingIpObject);
279         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
280
281         // Suppress the called to these functions
282         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
283
284         Whitebox.invokeMethod(neutronL3Adapter, "flushExistingIpRewrite");
285         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.DELETE));
286     }
287
288     @SuppressWarnings({ "unchecked", "rawtypes" })
289     @Test
290     public void testRebuildExistingIpRewrite() throws Exception {
291         Map floatIpDataMapCache = new HashMap();
292
293         // init instance variables
294         floatIpDataMapCache.put(ID, floatingIpObject);
295         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
296
297         // Suppress the called to these functions
298         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage1", floatingIpClass, Action.class));
299
300         Whitebox.invokeMethod(neutronL3Adapter, "rebuildExistingIpRewrite");
301         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
302     }
303
304     @SuppressWarnings({ "unchecked", "rawtypes" })
305     @Test
306     public void testProgramFlowsForFloatingIPArpAdd() throws Exception {
307         Map<String, Pair> neutronPortToDpIdCache = new HashMap<String, Pair>();
308         Map<String, String> networkIdToRouterMacCache = new HashMap<String, String>();
309         Map floatIpDataMapCache = new HashMap();
310
311         NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
312         when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
313         when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
314         when(neutronFloatingIP.getPortUUID()).thenReturn(UUID);
315         NeutronPort neutronPort = mock(NeutronPort.class);
316         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
317         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
318         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
319         when(neutronNetwork.getID()).thenReturn(ID);
320
321         // init instance variables
322         floatIpDataMapCache.put(ID, floatingIpObject);
323         neutronPortToDpIdCache.put(UUID, mock(Pair.class));
324         networkIdToRouterMacCache.put(ID, MAC_ADDRESS);
325         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
326         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
327         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
328         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
329         PowerMockito.doReturn(neutronPort).when(neutronL3Adapter, "findNeutronPortForFloatingIp", anyString());
330         PowerMockito.doReturn(Long.valueOf(15)).when(neutronL3Adapter, "findOFPortForExtPatch", anyLong());
331         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
332         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
333         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
334         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
335         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
336
337         // Suppress the called to these functions
338         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findNeutronPortForFloatingIp", String.class));
339         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findOFPortForExtPatch", Long.class));
340         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
341
342         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpAdd", neutronFloatingIP);
343         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findNeutronPortForFloatingIp", anyString());
344         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findOFPortForExtPatch", anyLong());
345         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
346     }
347
348     @SuppressWarnings({ "unchecked", "rawtypes" })
349     @Test
350     public void testProgramFlowsForFloatingIPArpDelete() throws Exception {
351         Map floatIpDataMapCache = new HashMap();
352
353      // init instance variables
354         floatIpDataMapCache.put(ID, floatingIpObject);
355         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
356
357         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
358
359         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpDelete", ID);
360         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
361     }
362
363     @Test
364     public void testFindNeutronPortForFloatingIp() throws Exception {
365         NeutronPort neutronPort = mock(NeutronPort.class);
366         when(neutronPort.getDeviceOwner()).thenReturn(OWNER_FLOATING_IP);
367         when(neutronPort.getDeviceID()).thenReturn(ID);
368         List<NeutronPort> list_neutronPort = new ArrayList<>();
369         list_neutronPort.add(neutronPort);
370         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
371         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
372
373         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
374
375         assertEquals("Error, did not return the correct NeutronPort", neutronPort, Whitebox.invokeMethod(neutronL3Adapter, "findNeutronPortForFloatingIp", ID));
376     }
377
378     @Test
379     public void testFindOFPortForExtPatch() throws Exception {
380         ConfigurationService configurationService = mock(ConfigurationService.class);
381         when(configurationService.getPatchPortName(any(Pair.class))).thenReturn(PORT_INT);
382         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
383         List<Node> nodes = new ArrayList<Node>();
384         nodes.add(mock(Node.class));
385         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
386         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
387         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
388         Southbound southbound = mock(Southbound.class);
389         when(southbound.getDataPathId(any(Node.class))).thenReturn(Long.valueOf(ID));
390         OvsdbTerminationPointAugmentation terminationPointOfBridge = mock(OvsdbTerminationPointAugmentation.class);
391         when(terminationPointOfBridge.getOfport()).thenReturn(Long.valueOf(ID));
392         when(southbound.getTerminationPointOfBridge(any(Node.class), anyString())).thenReturn(terminationPointOfBridge);
393         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
394
395         assertEquals("Error, did not return the correct NeutronPort", Long.valueOf(ID), Whitebox.invokeMethod(neutronL3Adapter, "findOFPortForExtPatch", Long.valueOf(ID)));
396     }
397
398     @Test
399     public void testHandleNeutronNetworkEvent() throws Exception {
400         // Nothing to be done here
401         Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronNetworkEvent", mock(NeutronNetwork.class), Action.ADD);
402     }
403
404     @Test
405     public void testHandleInterfaceEvent() throws Exception {
406         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<String, Pair<Long,Uuid>>();
407         // init instance variables
408         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
409         MemberModifier.field(NeutronL3Adapter.class, "tenantNetworkManager").set(neutronL3Adapter , tenantNetworkManager);
410         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
411
412         // Mock variables
413         NodeId nodeId = mock(NodeId.class);
414         when(nodeId.getValue()).thenReturn(ID);
415         Node node = mock(Node.class);
416         when(node.getNodeId()).thenReturn(nodeId);
417
418         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
419         when(intf.getInterfaceUuid()).thenReturn(mock(Uuid.class));
420         when(intf.getName()).thenReturn(INTF_NAME);
421
422         NeutronPort neutronPort = mock(NeutronPort.class);
423         when(neutronPort.getPortUUID()).thenReturn(UUID);
424
425         when(tenantNetworkManager.getTenantPort(intf)).thenReturn(neutronPort);
426
427         // Suppress the called to these functions
428         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
429         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventAdd", String.class, Long.class, Uuid.class));
430         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventDelete", OvsdbTerminationPointAugmentation.class, Long.class));
431
432         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(Long.valueOf(45));
433         Mockito.doNothing().when(neutronL3Adapter).handleNeutronPortEvent(any(NeutronPort.class), any(Action.class));
434
435         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.ADD);
436         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
437         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventAdd", any(String.class), anyLong(), any(Uuid.class));
438         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.ADD);
439
440         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.DELETE);
441         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventDelete", any(OvsdbTerminationPointAugmentation.class), anyLong());
442         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.DELETE);
443     }
444
445     @Test
446     public void testHandleInterfaceEventAdd() throws Exception {
447         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<String, Pair<Long,Uuid>>();
448         // init instance variables
449         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
450         int temp = neutronPortToDpIdCache.size();
451
452         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventAdd", "", Long.valueOf(5), mock(Uuid.class));
453
454         assertEquals("Error, did not add the port", temp+1, neutronPortToDpIdCache.size());
455     }
456
457     @SuppressWarnings("unchecked")
458     @Test
459     public void testHandleInterfaceEventDelete() throws Exception {
460         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<String, Pair<Long,Uuid>>();
461         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
462         Uuid uuid = mock(Uuid.class);
463         when(intf.getInterfaceUuid()).thenReturn(uuid );
464         Pair<Long, Uuid> pair = mock(Pair.class);
465         when(pair.getRight()).thenReturn(uuid);
466
467         // init instance variables
468         neutronPortToDpIdCache.put("key", pair);
469         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
470         int temp = neutronPortToDpIdCache.size();
471
472         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventDelete", intf, Long.valueOf(ID));
473
474         assertEquals("Error, did not remove the port", temp-1, neutronPortToDpIdCache.size());
475     }
476
477     @Test
478     public void testUpdateL3ForNeutronPort() throws Exception {
479         Map<String, String> networkIdToRouterMacCache = new HashMap<String, String>();
480
481         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
482         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
483         List<Neutron_IPs> neutronIps = new ArrayList<Neutron_IPs>();
484         neutronIps.add(neutronIp);
485         NeutronPort neutronPort = mock(NeutronPort.class);
486         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
487         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS_2);
488         when(neutronPort.getFixedIPs()).thenReturn(neutronIps);
489         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
490         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
491         List<Node> nodes = new ArrayList<Node>();
492         nodes.add(mock(Node.class));
493         PowerMockito.doReturn(Long.valueOf(15)).when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class));
494
495         // init instance variables
496         networkIdToRouterMacCache .put(UUID, MAC_ADDRESS);
497         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
498         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
499         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
500         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
501         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
502         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
503         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
504         MemberModifier.field(NeutronL3Adapter.class, "flgDistributedARPEnabled").set(neutronL3Adapter , true);
505
506         // Suppress the called to these functions
507         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
508         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage1", Node.class, Long.class, String.class, String.class, String.class, Action.class));
509         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
510
511         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, false);
512         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
513         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
514         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
515
516         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, true);
517         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
518         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
519         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
520     }
521
522     // either add or remove item in l3ForwardingCache
523     @Test
524     public void testProgramL3ForwardingStage1() throws Exception {
525         Set<String> l3ForwardingCache = new HashSet<String>();
526
527         NodeId nodeId = mock(NodeId.class);
528         when(nodeId.getValue()).thenReturn(ID);
529         Node node = mock(Node.class);
530         when(node.getNodeId()).thenReturn(nodeId);
531
532         // Suppress the called to these functions
533         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage2", Node.class, Long.class, String.class, String.class, String.class, Action.class));
534
535         // init instance variables
536         MemberModifier.field(NeutronL3Adapter.class, "l3ForwardingCache").set(neutronL3Adapter , l3ForwardingCache );
537         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD)).thenReturn(new Status(StatusCode.SUCCESS));
538         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE)).thenReturn(new Status(StatusCode.SUCCESS));
539
540         int temp = l3ForwardingCache.size();
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         assertEquals("Error, did not add the value", temp, l3ForwardingCache.size()-1);
545
546         l3ForwardingCache.add(node.getNodeId().getValue() + ":" + SEG_ID + ":" + IP);
547         temp = l3ForwardingCache.size();
548
549         Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.DELETE);
550         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
551         assertEquals("Error, did not delete the value", temp, l3ForwardingCache.size()+1);
552     }
553
554     @Test
555     public void testProgramL3ForwardingStage2() throws Exception {
556         NodeId nodeId = mock(NodeId.class);
557         when(nodeId.getValue()).thenReturn(ID);
558         Node node = mock(Node.class);
559         when(node.getNodeId()).thenReturn(nodeId);
560
561         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));
562
563         PowerMockito.mockStatic(InetAddress.class);
564         InetAddress inetAddress = mock(InetAddress.class);
565         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
566
567         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));
568     }
569
570     @Test
571     public void testProgramFlowsForNeutronRouterInterface() throws Exception {
572         Map<String, String> networkIdToRouterMacCache = new HashMap<String, String>();
573         Map<String, List<Neutron_IPs>> networkIdToRouterIpListCache = new HashMap<String, List<Neutron_IPs>>();
574         Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<String, NeutronRouter_Interface>();
575
576         NeutronRouter_Interface intf = mock(NeutronRouter_Interface.class);
577         when(intf.getPortUUID()).thenReturn(UUID);
578         when(intf.getSubnetUUID()).thenReturn(UUID);
579         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
580         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
581         List<Neutron_IPs> ips = new ArrayList<Neutron_IPs>();
582         ips.add(neutronIp);
583         NeutronPort neutronPort = mock(NeutronPort.class);
584         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
585         when(neutronPort.getFixedIPs()).thenReturn(ips);
586         NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
587         when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
588         when(neutronSubnet.getGatewayIP()).thenReturn(IP);
589         when(neutronSubnet.getCidr()).thenReturn("cidr");
590         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
591         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
592         when(neutronNetwork.getRouterExternal()).thenReturn(false); //might change that to true
593         when(neutronNetwork.getNetworkUUID()).thenReturn(UUID);
594
595         Node node = mock(Node.class);
596         List<Node> nodes = new ArrayList<Node>();
597         nodes.add(node);
598
599         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
600         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));
601         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowForNetworkFromExternal", Node.class, Long.class, String.class, String.class, String.class, int.class, Action.class));
602         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
603         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage1", Node.class, Long.class, String.class, String.class, Action.class));
604
605         // init instance variables
606         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
607         PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
608         INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
609         PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
610         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
611         PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
612         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
613         PowerMockito.when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
614         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
615         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterIpListCache").set(neutronL3Adapter , networkIdToRouterIpListCache);
616         MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
617         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
618         MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
619         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
620         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
621         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(Long.valueOf(45));
622
623         int networkIdToRouterMacCacheSize, networkIdToRouterIpListCacheSize, subnetIdToRouterInterfaceCacheSize;
624         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
625         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
626         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
627
628         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterface", intf, false);
629
630         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
631         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());
632         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
633         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
634         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
635         assertEquals("Error, did not add the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() -1);
636         assertEquals("Error, did not add the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() -1);
637         assertEquals("Error, did not add the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() -1);
638
639         networkIdToRouterMacCache.put(UUID, MAC_ADDRESS);
640         networkIdToRouterIpListCache.put(UUID, ips);
641         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
642         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
643         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
644
645         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterface", intf, true);
646
647         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
648         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.DELETE), anyBoolean());
649         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
650         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
651         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
652         assertEquals("Error, did not remove the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() +1);
653         assertEquals("Error, did not remove the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() +1);
654         assertEquals("Error, did not remove the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() +1);
655     }
656
657     @Test
658     public void testProgramFlowForNetworkFromExternal() throws Exception {
659         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage1", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
660
661         Whitebox.invokeMethod(neutronL3Adapter, "programFlowForNetworkFromExternal", mock(Node.class), Long.valueOf(12), "", "", "", 4, Action.ADD);
662
663         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
664     }
665
666     @Test
667     public void testProgramFlowsForNeutronRouterInterfacePair() throws Exception {
668         NeutronRouter_Interface srcNeutronRouterInterface = mock(NeutronRouter_Interface.class);
669         when(srcNeutronRouterInterface.getSubnetUUID()).thenReturn(UUID);
670         when(srcNeutronRouterInterface.getID()).thenReturn(ID);
671         when(srcNeutronRouterInterface.getPortUUID()).thenReturn(UUID);
672         NeutronRouter_Interface dstNeutronRouterInterface = mock(NeutronRouter_Interface.class);
673         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
674         when(neutronNetwork.getTenantID()).thenReturn(ID);
675         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
676         Neutron_IPs ip = mock(Neutron_IPs.class);
677         when(ip.getIpAddress()).thenReturn(IP);
678         List<Neutron_IPs> ips = new ArrayList<Neutron_IPs>();
679         ips.add(ip);
680         ips.add(ip);
681         ips.add(ip);
682         NeutronPort neutronPort = mock(NeutronPort.class);
683         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
684         when(neutronPort.getFixedIPs()).thenReturn(ips);
685         NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
686         when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
687         when(neutronSubnet.getCidr()).thenReturn(CIDR);
688
689         INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
690         PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
691         MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
692         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
693         PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
694         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
695         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
696         PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
697         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
698
699         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage1", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
700         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getMaskLenFromCidr", String.class));
701
702         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterfacePair", mock(Node.class), Long.valueOf(12), srcNeutronRouterInterface, dstNeutronRouterInterface, neutronNetwork, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD, false);
703         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
704
705         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterfacePair", mock(Node.class), Long.valueOf(12), srcNeutronRouterInterface, dstNeutronRouterInterface, neutronNetwork, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD, true);
706         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class));
707         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getMaskLenFromCidr", anyString());
708         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
709     }
710
711     @Test
712     public void testProgramRouterInterfaceStage1() throws Exception {
713         Set<String> routerInterfacesCache = new HashSet<String>();
714
715         NodeId nodeId = mock(NodeId.class);
716         when(nodeId.getValue()).thenReturn(ID);
717         Node node = mock(Node.class);
718         when(node.getNodeId()).thenReturn(nodeId);
719
720         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programRouterInterfaceStage2", Node.class, Long.class, String.class, String.class, String.class, String.class, int.class, Action.class));
721
722         MemberModifier.field(NeutronL3Adapter.class, "routerInterfacesCache").set(neutronL3Adapter , routerInterfacesCache);
723         PowerMockito.when(neutronL3Adapter, "programRouterInterfaceStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
724
725         int t = routerInterfacesCache.size();
726
727         Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.ADD);
728
729         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
730         assertEquals("Error, did not add the interface", t, routerInterfacesCache.size() - 1);
731
732         t = routerInterfacesCache.size();
733         Whitebox.invokeMethod(neutronL3Adapter, "programRouterInterfaceStage1", node, Long.valueOf(12), SEG_ID, SEG_ID, MAC_ADDRESS, IP, 4, Action.DELETE);
734
735         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programRouterInterfaceStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
736         assertEquals("Error, did not delete the interface", t, routerInterfacesCache.size() + 1);
737     }
738
739     // either add or remove routerInterfacesCache
740     @Test
741     public void testProgramRouterInterfaceStage2() throws Exception {
742         NodeId nodeId = mock(NodeId.class);
743         when(nodeId.getValue()).thenReturn(ID);
744         Node node = mock(Node.class);
745         when(node.getNodeId()).thenReturn(nodeId);
746
747         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));
748
749         PowerMockito.mockStatic(InetAddress.class);
750         InetAddress inetAddress = mock(InetAddress.class);
751         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
752
753         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));
754
755     }
756
757     //either add or remove staticArpEntryCache
758     @Test
759     public void testProgramStaticArpStage1() throws Exception {
760         Set<String> staticArpEntryCache = new HashSet<String>();
761
762         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage2", Long.class, String.class, String.class, String.class, Action.class));
763
764         MemberModifier.field(NeutronL3Adapter.class, "staticArpEntryCache").set(neutronL3Adapter , staticArpEntryCache);
765         PowerMockito.when(neutronL3Adapter, "programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
766
767         int t = staticArpEntryCache.size();
768
769         Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
770
771         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
772         assertEquals("Error, did not add the static arp", t, staticArpEntryCache.size() - 1);
773
774         t = staticArpEntryCache.size();
775         Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
776
777         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
778         assertEquals("Error, did not delete the static arp", t, staticArpEntryCache.size() + 1);
779     }
780
781     @Test
782     public void testProgramStaticArpStage2() throws Exception {
783         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));
784
785         PowerMockito.mockStatic(InetAddress.class);
786         InetAddress inetAddress = mock(InetAddress.class);
787         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
788
789         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));
790     }
791
792     // either add or remove inboundIpRewriteCache
793     @Test
794     public void testProgramInboundIpRewriteStage1() throws Exception {
795         Set<String> inboundIpRewriteCache = new HashSet<String>();
796
797         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage2", Long.class, Long.class, String.class, String.class, String.class, Action.class));
798
799         MemberModifier.field(NeutronL3Adapter.class, "inboundIpRewriteCache").set(neutronL3Adapter , inboundIpRewriteCache);
800         PowerMockito.when(neutronL3Adapter, "programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
801
802         int t = inboundIpRewriteCache.size();
803
804         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
805
806         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
807         assertEquals("Error, did not add the inboundIpRewrite", t, inboundIpRewriteCache.size() - 1);
808
809         t = inboundIpRewriteCache.size();
810         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12),PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
811
812         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
813         assertEquals("Error, did not delete the inboundIpRewrite", t, inboundIpRewriteCache.size() + 1);
814     }
815
816     @Test
817     public void testProgramInboundIpRewriteStage2() throws Exception {
818         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));
819
820         PowerMockito.mockStatic(InetAddress.class);
821         InetAddress inetAddress = mock(InetAddress.class);
822         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
823
824         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));
825     }
826
827     // either add or remove outboundIpRewriteExclusionCache
828     @Test
829     public void testProgramIpRewriteExclusionStage1() throws Exception {
830         Set<String> outboundIpRewriteExclusionCache = new HashSet<String>();
831
832         NodeId nodeId = mock(NodeId.class);
833         when(nodeId.getValue()).thenReturn(ID);
834         Node node = mock(Node.class);
835         when(node.getNodeId()).thenReturn(nodeId);
836
837         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage2", Node.class, Long.class, String.class, String.class, Action.class));
838
839         MemberModifier.field(NeutronL3Adapter.class, "outboundIpRewriteExclusionCache").set(neutronL3Adapter , outboundIpRewriteExclusionCache);
840         PowerMockito.when(neutronL3Adapter, "programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
841
842         int t = outboundIpRewriteExclusionCache.size();
843
844         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.ADD);
845
846         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
847         assertEquals("Error, did not add the outboundIpRewriteExclusion", t, outboundIpRewriteExclusionCache.size() - 1);
848
849         t = outboundIpRewriteExclusionCache.size();
850         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.DELETE);
851
852         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
853         assertEquals("Error, did not delete the outboundIpRewriteExclusion", t, outboundIpRewriteExclusionCache.size() + 1);
854     }
855
856     @Test
857     public void testProgramIpRewriteExclusionStage2() throws Exception {
858         NodeId nodeId = mock(NodeId.class);
859         when(nodeId.getValue()).thenReturn(ID);
860         Node node = mock(Node.class);
861         when(node.getNodeId()).thenReturn(nodeId);
862
863         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));
864     }
865
866     // either add or remove outboundIpRewriteCache
867     @SuppressWarnings("unchecked")
868     @Test
869     public void testProgramOutboundIpRewriteStage1() throws Exception{
870         Set<String> outboundIpRewriteCache = new HashSet<String>();
871
872         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage2", floatingIpClass, Action.class));
873
874         MemberModifier.field(NeutronL3Adapter.class, "outboundIpRewriteCache").set(neutronL3Adapter , outboundIpRewriteCache);
875         PowerMockito.when(neutronL3Adapter, "programOutboundIpRewriteStage2", any(floatingIpClass), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
876
877         int t = outboundIpRewriteCache.size();
878
879         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.ADD);
880
881         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.ADD));
882         assertEquals("Error, did not add the outbound ip", t, outboundIpRewriteCache.size() - 1);
883
884         t = outboundIpRewriteCache.size();
885         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.DELETE);
886
887         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.DELETE));
888         assertEquals("Error, did not delete the outbound ip", t, outboundIpRewriteCache.size() + 1);
889     }
890
891     @Test
892     public void testPrepareProgramOutboundIpRewriteStage2() throws Exception {
893         assertEquals("Error, did not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
894
895         PowerMockito.mockStatic(InetAddress.class);
896         InetAddress inetAddress = mock(InetAddress.class);
897         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
898
899         assertEquals("Error, did not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
900     }
901
902     @Test
903     public void testGetMaskLenFromCidr() throws Exception {
904         assertEquals("Error, did not return the correct mask", 32, Whitebox.invokeMethod(neutronL3Adapter, "getMaskLenFromCidr", IP_MASK));
905     }
906
907     @Test
908     public void testGetDpidForIntegrationBridge() throws Exception {
909         Southbound southbound = mock(Southbound.class);
910         ConfigurationService configurationService = mock(ConfigurationService.class);
911
912         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
913         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
914
915         PowerMockito.when(southbound.getBridge(any(Node.class), anyString())).thenReturn(mock(OvsdbBridgeAugmentation.class));
916         PowerMockito.when(configurationService.getIntegrationBridgeName()).thenReturn("");
917         PowerMockito.when(southbound.getDataPathId(any(Node.class))).thenReturn(Long.valueOf(45));
918
919         assertEquals("Error, did not return the correct Dpid", Long.valueOf(45), Whitebox.invokeMethod(neutronL3Adapter, "getDpidForIntegrationBridge", mock(Node.class)));
920     }
921
922     @Test
923     public void testencodeExcplicitOFPort() throws Exception {
924         assertEquals("Error, did not correctly encode the port", OFPort, NeutronL3Adapter.encodeExcplicitOFPort(Long.valueOf(45)));
925     }
926
927     @Test
928     public void testSetDependencies() throws Exception {
929         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
930         ConfigurationService configurationService = mock(ConfigurationService.class);
931         ArpProvider arpProvider = mock(ArpProvider.class);
932         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
933         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
934         RoutingProvider routingProvider = mock(RoutingProvider.class);
935         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
936         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
937         Southbound southbound = mock(Southbound.class);
938
939         PowerMockito.mockStatic(ServiceHelper.class);
940         PowerMockito.when(ServiceHelper.getGlobalInstance(TenantNetworkManager.class, neutronL3Adapter)).thenReturn(tenantNetworkManager);
941         PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, neutronL3Adapter)).thenReturn(configurationService);
942         PowerMockito.when(ServiceHelper.getGlobalInstance(ArpProvider.class, neutronL3Adapter)).thenReturn(arpProvider);
943         PowerMockito.when(ServiceHelper.getGlobalInstance(InboundNatProvider.class, neutronL3Adapter)).thenReturn(inboundNatProvider);
944         PowerMockito.when(ServiceHelper.getGlobalInstance(OutboundNatProvider.class, neutronL3Adapter)).thenReturn(outboundNatProvider);
945         PowerMockito.when(ServiceHelper.getGlobalInstance(RoutingProvider.class, neutronL3Adapter)).thenReturn(routingProvider);
946         PowerMockito.when(ServiceHelper.getGlobalInstance(L3ForwardingProvider.class, neutronL3Adapter)).thenReturn(l3ForwardingProvider);
947         PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, neutronL3Adapter)).thenReturn(nodeCacheManager);
948         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, neutronL3Adapter)).thenReturn(southbound);
949
950         neutronL3Adapter.setDependencies(mock(BundleContext.class), mock(ServiceReference.class));
951
952         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
953         assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
954         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
955         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
956         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
957         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
958         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
959         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
960         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
961     }
962
963     @Test
964     public void testSetDependenciesObject() throws Exception{
965         INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
966         neutronL3Adapter.setDependencies(iNeutronNetworkCRUD);
967         assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
968
969         INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
970         neutronL3Adapter.setDependencies(iNeutronPortCRUD);
971         assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
972
973         INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
974         neutronL3Adapter.setDependencies(iNeutronSubnetCRUD);
975         assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
976
977         ArpProvider arpProvider = mock(ArpProvider.class);
978         neutronL3Adapter.setDependencies(arpProvider);
979         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
980
981         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
982         neutronL3Adapter.setDependencies(inboundNatProvider);
983         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
984
985         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
986         neutronL3Adapter.setDependencies(outboundNatProvider);
987         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
988
989         RoutingProvider routingProvider = mock(RoutingProvider.class);
990         neutronL3Adapter.setDependencies(routingProvider);
991         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
992
993         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
994         neutronL3Adapter.setDependencies(l3ForwardingProvider);
995         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
996     }
997
998     private Object getField(String fieldName) throws Exception {
999         Field field = NeutronL3Adapter.class.getDeclaredField(fieldName);
1000         field.setAccessible(true);
1001         return field.get(neutronL3Adapter);
1002     }
1003
1004     @SuppressWarnings("rawtypes")
1005     private Object createFloatingIpObject() throws Exception{
1006         Class clazz = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
1007         Constructor [] constructors = clazz.getConstructors();
1008         Constructor c  = constructors[0];
1009         return c.newInstance(new NeutronL3Adapter(), Long.valueOf(415), Long.valueOf(415), "a", "b", "c", "d", "e");
1010     }
1011 }