Merge "Drop redundant ofPort check"
[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.ovsdb.openstack.netvirt.translator.NeutronFloatingIP;
39 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
40 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronPort;
41 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronRouter;
42 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronRouter_Interface;
43 import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronSubnet;
44 import org.opendaylight.ovsdb.openstack.netvirt.translator.Neutron_IPs;
45 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
46 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronPortCRUD;
47 import org.opendaylight.ovsdb.openstack.netvirt.translator.crud.INeutronSubnetCRUD;
48 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
49 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
50 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
51 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
52 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
53 import org.opendaylight.ovsdb.openstack.netvirt.api.NodeCacheManager;
54 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
55 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
56 import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
57 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
58 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
59 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
60 import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
61 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
64 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
65 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
66
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<>();
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<>();
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<>();
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         PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
284         Whitebox.invokeMethod(neutronL3Adapter, "flushExistingIpRewrite");
285         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.DELETE));
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         PowerMockito.doReturn(floatIpDataMapCache.values()).when(neutronL3Adapter, "getAllFloatingIPsWithMetadata");
301         Whitebox.invokeMethod(neutronL3Adapter, "rebuildExistingIpRewrite");
302         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage1", any(floatingIpClass), eq(Action.ADD));
303     }
304
305     @SuppressWarnings({ "unchecked", "rawtypes" })
306     @Test
307     public void testProgramFlowsForFloatingIPArpAdd() throws Exception {
308         Map<String, Pair> neutronPortToDpIdCache = new HashMap<>();
309         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
310         Map floatIpDataMapCache = new HashMap();
311
312         NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
313         when(neutronFloatingIP.getFixedIPAddress()).thenReturn(FIXED_IP_ADDRESS);
314         when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(FLOATING_IP_ADDRESS);
315         when(neutronFloatingIP.getPortUUID()).thenReturn(UUID);
316         NeutronPort neutronPort = mock(NeutronPort.class);
317         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
318         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
319         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
320         when(neutronNetwork.getID()).thenReturn(ID);
321
322         // init instance variables
323         floatIpDataMapCache.put(ID, floatingIpObject);
324         neutronPortToDpIdCache.put(UUID, mock(Pair.class));
325         networkIdToRouterMacCache.put(ID, MAC_ADDRESS);
326         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
327         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
328         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
329         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
330         PowerMockito.doReturn(neutronPort).when(neutronL3Adapter, "findNeutronPortForFloatingIp", anyString());
331         PowerMockito.doReturn(15L).when(neutronL3Adapter, "findOFPortForExtPatch", anyLong());
332         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
333         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
334         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
335         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
336         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
337
338         // Suppress the called to these functions
339         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findNeutronPortForFloatingIp", String.class));
340         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "findOFPortForExtPatch", Long.class));
341         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
342
343         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpAdd", neutronFloatingIP);
344         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findNeutronPortForFloatingIp", anyString());
345         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("findOFPortForExtPatch", anyLong());
346         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
347     }
348
349     @SuppressWarnings({ "unchecked", "rawtypes" })
350     @Test
351     public void testProgramFlowsForFloatingIPArpDelete() throws Exception {
352         Map floatIpDataMapCache = new HashMap();
353
354      // init instance variables
355         floatIpDataMapCache.put(ID, floatingIpObject);
356         MemberModifier.field(NeutronL3Adapter.class, "floatIpDataMapCache").set(neutronL3Adapter , floatIpDataMapCache);
357
358         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
359
360         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForFloatingIPArpDelete", ID);
361         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
362     }
363
364     @Test
365     public void testFindNeutronPortForFloatingIp() throws Exception {
366         NeutronPort neutronPort = mock(NeutronPort.class);
367         when(neutronPort.getDeviceOwner()).thenReturn(OWNER_FLOATING_IP);
368         when(neutronPort.getDeviceID()).thenReturn(ID);
369         List<NeutronPort> list_neutronPort = new ArrayList<>();
370         list_neutronPort.add(neutronPort);
371         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
372         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
373
374         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
375
376         assertEquals("Error, did not return the correct NeutronPort", neutronPort, Whitebox.invokeMethod(neutronL3Adapter, "findNeutronPortForFloatingIp", ID));
377     }
378
379     @Test
380     public void testFindOFPortForExtPatch() throws Exception {
381         ConfigurationService configurationService = mock(ConfigurationService.class);
382         when(configurationService.getPatchPortName(any(Pair.class))).thenReturn(PORT_INT);
383         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
384         List<Node> nodes = new ArrayList<>();
385         nodes.add(mock(Node.class));
386         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
387         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
388         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
389         Southbound southbound = mock(Southbound.class);
390         when(southbound.getDataPathId(any(Node.class))).thenReturn(Long.valueOf(ID));
391         OvsdbTerminationPointAugmentation terminationPointOfBridge = mock(OvsdbTerminationPointAugmentation.class);
392         when(terminationPointOfBridge.getOfport()).thenReturn(Long.valueOf(ID));
393         when(southbound.getTerminationPointOfBridge(any(Node.class), anyString())).thenReturn(terminationPointOfBridge);
394         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
395
396         assertEquals("Error, did not return the correct NeutronPort", Long.valueOf(ID), Whitebox.invokeMethod(neutronL3Adapter, "findOFPortForExtPatch", Long.valueOf(ID)));
397     }
398
399     @Test
400     public void testHandleNeutronNetworkEvent() throws Exception {
401         // Nothing to be done here
402         Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronNetworkEvent", mock(NeutronNetwork.class), Action.ADD);
403     }
404
405     @Test
406     public void testHandleInterfaceEvent() throws Exception {
407         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
408         // init instance variables
409         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
410         MemberModifier.field(NeutronL3Adapter.class, "tenantNetworkManager").set(neutronL3Adapter , tenantNetworkManager);
411         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
412
413         // Mock variables
414         NodeId nodeId = mock(NodeId.class);
415         when(nodeId.getValue()).thenReturn(ID);
416         Node node = mock(Node.class);
417         when(node.getNodeId()).thenReturn(nodeId);
418
419         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
420         when(intf.getInterfaceUuid()).thenReturn(mock(Uuid.class));
421         when(intf.getName()).thenReturn(INTF_NAME);
422
423         NeutronPort neutronPort = mock(NeutronPort.class);
424         when(neutronPort.getPortUUID()).thenReturn(UUID);
425
426         when(tenantNetworkManager.getTenantPort(intf)).thenReturn(neutronPort);
427
428         // Suppress the called to these functions
429         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
430         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventAdd", String.class, Long.class, Uuid.class));
431         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "handleInterfaceEventDelete", OvsdbTerminationPointAugmentation.class, Long.class));
432
433         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
434         Mockito.doNothing().when(neutronL3Adapter).handleNeutronPortEvent(any(NeutronPort.class), any(Action.class));
435
436         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.ADD);
437         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
438         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventAdd", any(String.class), anyLong(), any(Uuid.class));
439         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.ADD);
440
441         neutronL3Adapter.handleInterfaceEvent(node, intf, mock(NeutronNetwork.class), Action.DELETE);
442         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("handleInterfaceEventDelete", any(OvsdbTerminationPointAugmentation.class), anyLong());
443         Mockito.verify(neutronL3Adapter).handleNeutronPortEvent(neutronPort, Action.DELETE);
444     }
445
446     @Test
447     public void testHandleInterfaceEventAdd() throws Exception {
448         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
449         // init instance variables
450         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
451         int temp = neutronPortToDpIdCache.size();
452
453         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventAdd", "", Long.valueOf(5), mock(Uuid.class));
454
455         assertEquals("Error, did not add the port", temp+1, neutronPortToDpIdCache.size());
456     }
457
458     @SuppressWarnings("unchecked")
459     @Test
460     public void testHandleInterfaceEventDelete() throws Exception {
461         Map<String, Pair<Long, Uuid>> neutronPortToDpIdCache = new HashMap<>();
462         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
463         Uuid uuid = mock(Uuid.class);
464         when(intf.getInterfaceUuid()).thenReturn(uuid );
465         Pair<Long, Uuid> pair = mock(Pair.class);
466         when(pair.getRight()).thenReturn(uuid);
467
468         // init instance variables
469         neutronPortToDpIdCache.put("key", pair);
470         MemberModifier.field(NeutronL3Adapter.class, "neutronPortToDpIdCache").set(neutronL3Adapter , neutronPortToDpIdCache);
471         int temp = neutronPortToDpIdCache.size();
472
473         Whitebox.invokeMethod(neutronL3Adapter, "handleInterfaceEventDelete", intf, Long.valueOf(ID));
474
475         assertEquals("Error, did not remove the port", temp-1, neutronPortToDpIdCache.size());
476     }
477
478     @Test
479     public void testUpdateL3ForNeutronPort() throws Exception {
480         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
481
482         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
483         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
484         List<Neutron_IPs> neutronIps = new ArrayList<>();
485         neutronIps.add(neutronIp);
486         NeutronPort neutronPort = mock(NeutronPort.class);
487         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
488         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS_2);
489         when(neutronPort.getFixedIPs()).thenReturn(neutronIps);
490         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
491         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
492         List<Node> nodes = new ArrayList<>();
493         nodes.add(mock(Node.class));
494         PowerMockito.doReturn(15L).when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class));
495
496         // init instance variables
497         networkIdToRouterMacCache .put(UUID, MAC_ADDRESS);
498         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
499         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
500         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
501         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
502         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
503         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
504         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
505         MemberModifier.field(NeutronL3Adapter.class, "flgDistributedARPEnabled").set(neutronL3Adapter , true);
506
507         // Suppress the called to these functions
508         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
509         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage1", Node.class, Long.class, String.class, String.class, String.class, Action.class));
510         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
511
512         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, false);
513         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
514         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
515         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
516
517         Whitebox.invokeMethod(neutronL3Adapter, "updateL3ForNeutronPort", neutronPort, true);
518         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
519         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage1", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
520         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
521     }
522
523     @Test
524     public void testProgramL3ForwardingStage1() throws Exception {
525
526         NodeId nodeId = mock(NodeId.class);
527         when(nodeId.getValue()).thenReturn(ID);
528         Node node = mock(Node.class);
529         when(node.getNodeId()).thenReturn(nodeId);
530
531         // Suppress the called to these functions
532         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programL3ForwardingStage2", Node.class, Long.class, String.class, String.class, String.class, Action.class));
533
534         // init instance variables
535         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD)).thenReturn(new Status(StatusCode.SUCCESS));
536         PowerMockito.when(neutronL3Adapter, "programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE)).thenReturn(new Status(StatusCode.SUCCESS));
537
538
539         Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD);
540         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
541
542
543         Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage1", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.DELETE);
544         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programL3ForwardingStage2", any(Node.class), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
545     }
546
547     @Test
548     public void testProgramL3ForwardingStage2() throws Exception {
549         NodeId nodeId = mock(NodeId.class);
550         when(nodeId.getValue()).thenReturn(ID);
551         Node node = mock(Node.class);
552         when(node.getNodeId()).thenReturn(nodeId);
553
554         assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage2", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, MALFORM_IP, Action.ADD));
555
556         PowerMockito.mockStatic(InetAddress.class);
557         InetAddress inetAddress = mock(InetAddress.class);
558         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
559
560         assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programL3ForwardingStage2", node, Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
561     }
562
563     @Test
564     public void testProgramFlowsForNeutronRouterInterface() throws Exception {
565         Map<String, String> networkIdToRouterMacCache = new HashMap<>();
566         Map<String, List<Neutron_IPs>> networkIdToRouterIpListCache = new HashMap<>();
567         Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache = new HashMap<>();
568
569         NeutronRouter_Interface intf = mock(NeutronRouter_Interface.class);
570         when(intf.getPortUUID()).thenReturn(UUID);
571         when(intf.getSubnetUUID()).thenReturn(UUID);
572         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
573         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
574         List<Neutron_IPs> ips = new ArrayList<>();
575         ips.add(neutronIp);
576         NeutronPort neutronPort = mock(NeutronPort.class);
577         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS);
578         when(neutronPort.getFixedIPs()).thenReturn(ips);
579         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
580         NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
581         when(neutronSubnet.getNetworkUUID()).thenReturn(UUID);
582         when(neutronSubnet.getGatewayIP()).thenReturn(IP);
583         when(neutronSubnet.getCidr()).thenReturn("cidr");
584         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
585         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
586         when(neutronNetwork.getRouterExternal()).thenReturn(false); //might change that to true
587         when(neutronNetwork.getNetworkUUID()).thenReturn(UUID);
588         NeutronRouter neutronRouter = mock(NeutronRouter.class);
589
590         Node node = mock(Node.class);
591         List<Node> nodes = new ArrayList<>();
592         nodes.add(node);
593
594         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "getDpidForIntegrationBridge", Node.class));
595         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowsForNeutronRouterInterfacePair", Node.class, Long.class, NeutronRouter_Interface.class, NeutronRouter_Interface.class, NeutronNetwork.class, String.class, String.class, String.class, int.class, Action.class, Boolean.class));
596         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programFlowForNetworkFromExternal", Node.class, Long.class, String.class, String.class, String.class, int.class, Action.class));
597         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage1", Long.class, String.class, String.class, String.class, Action.class));
598         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage1", Node.class, Long.class, String.class, String.class, Action.class));
599
600         // init instance variables
601         INeutronPortCRUD neutronPortCache = mock(INeutronPortCRUD.class);
602         PowerMockito.when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
603         PowerMockito.when(neutronPortCache.getAllPorts()).thenReturn(new ArrayList<NeutronPort>());
604         INeutronSubnetCRUD neutronSubnetCache = mock(INeutronSubnetCRUD.class);
605         PowerMockito.when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
606         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
607         PowerMockito.when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
608         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
609         PowerMockito.when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
610         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterMacCache").set(neutronL3Adapter , networkIdToRouterMacCache);
611         MemberModifier.field(NeutronL3Adapter.class, "networkIdToRouterIpListCache").set(neutronL3Adapter , networkIdToRouterIpListCache);
612         MemberModifier.field(NeutronL3Adapter.class, "subnetIdToRouterInterfaceCache").set(neutronL3Adapter , subnetIdToRouterInterfaceCache);
613         MemberModifier.field(NeutronL3Adapter.class, "neutronPortCache").set(neutronL3Adapter , neutronPortCache);
614         MemberModifier.field(NeutronL3Adapter.class, "neutronSubnetCache").set(neutronL3Adapter , neutronSubnetCache);
615         MemberModifier.field(NeutronL3Adapter.class, "neutronNetworkCache").set(neutronL3Adapter , neutronNetworkCache);
616         MemberModifier.field(NeutronL3Adapter.class, "nodeCacheManager").set(neutronL3Adapter , nodeCacheManager);
617         PowerMockito.when(neutronL3Adapter, "getDpidForIntegrationBridge", any(Node.class)).thenReturn(45L);
618
619         int networkIdToRouterMacCacheSize, networkIdToRouterIpListCacheSize, subnetIdToRouterInterfaceCacheSize;
620         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
621         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
622         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
623
624         Whitebox.invokeMethod(neutronL3Adapter, "programFlowsForNeutronRouterInterface", intf, false);
625
626         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("getDpidForIntegrationBridge", any(Node.class));
627         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterfacePair", any(Node.class), anyLong(), any(NeutronRouter_Interface.class), any(NeutronRouter_Interface.class), any(NeutronNetwork.class), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD), anyBoolean());
628         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.ADD));
629         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
630         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
631         assertEquals("Error, did not add the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() -1);
632         assertEquals("Error, did not add the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() -1);
633         assertEquals("Error, did not add the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() -1);
634
635         networkIdToRouterMacCache.put(UUID, MAC_ADDRESS);
636         networkIdToRouterIpListCache.put(UUID, ips);
637         subnetIdToRouterInterfaceCache.put(UUID, intf);
638         networkIdToRouterMacCacheSize = networkIdToRouterMacCache.size();
639         networkIdToRouterIpListCacheSize = networkIdToRouterIpListCache.size();
640         subnetIdToRouterInterfaceCacheSize = subnetIdToRouterInterfaceCache.size();
641
642         Whitebox.invokeMethod(neutronL3Adapter, "handleNeutronRouterInterfaceEvent", neutronRouter, intf, Action.DELETE);
643
644         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowsForNeutronRouterInterface", intf, true);
645         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("getDpidForIntegrationBridge", any(Node.class));
646         PowerMockito.verifyPrivate(neutronL3Adapter, times(2)).invoke("programFlowsForNeutronRouterInterfacePair", any(Node.class), anyLong(), any(NeutronRouter_Interface.class), any(NeutronRouter_Interface.class), any(NeutronNetwork.class), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE), anyBoolean());
647         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programFlowForNetworkFromExternal", any(Node.class), anyLong(), anyString(), anyString(), anyString(), anyInt(), eq(Action.DELETE));
648         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage1", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
649         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage1", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
650         assertEquals("Error, did not remove the RouterMac", networkIdToRouterMacCacheSize, networkIdToRouterMacCache.size() +1);
651         assertEquals("Error, did not remove the RouterIP", networkIdToRouterIpListCacheSize, networkIdToRouterIpListCache.size() +1);
652         assertEquals("Error, did not remove the RouterInterface", subnetIdToRouterInterfaceCacheSize, subnetIdToRouterInterfaceCache.size() +1);
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 testProgramStaticArpStage1() throws Exception {
750
751         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programStaticArpStage2", Long.class, String.class, String.class, String.class, Action.class));
752
753         PowerMockito.when(neutronL3Adapter, "programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
754
755
756         Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
757
758         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
759
760         Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage1", Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
761
762         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programStaticArpStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
763     }
764
765     @Test
766     public void testProgramStaticArpStage2() throws Exception {
767         assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programStaticArpStage2", Long.valueOf(45), PORT_INT, MAC_ADDRESS, MALFORM_IP, Action.ADD));
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, "programStaticArpStage2", Long.valueOf(45), PORT_INT, MAC_ADDRESS, IP, Action.ADD));
774     }
775
776     @Test
777     public void testProgramInboundIpRewriteStage1() throws Exception {
778
779         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programInboundIpRewriteStage2", Long.class, Long.class, String.class, String.class, String.class, Action.class));
780
781         PowerMockito.when(neutronL3Adapter, "programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
782
783
784         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12), PORT_INT, MAC_ADDRESS, IP, Action.ADD);
785
786         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
787
788         Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage1", Long.valueOf(12), Long.valueOf(12),PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
789
790         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programInboundIpRewriteStage2", anyLong(), anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
791     }
792
793     @Test
794     public void testProgramInboundIpRewriteStage2() throws Exception {
795         assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage2", Long.valueOf(45), Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
796
797         PowerMockito.mockStatic(InetAddress.class);
798         InetAddress inetAddress = mock(InetAddress.class);
799         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
800
801         assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programInboundIpRewriteStage2", Long.valueOf(45), Long.valueOf(45), SEG_ID, MAC_ADDRESS, IP, Action.ADD));
802     }
803
804     @Test
805     public void testProgramIpRewriteExclusionStage1() throws Exception {
806
807         NodeId nodeId = mock(NodeId.class);
808         when(nodeId.getValue()).thenReturn(ID);
809         Node node = mock(Node.class);
810         when(node.getNodeId()).thenReturn(nodeId);
811
812         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programIpRewriteExclusionStage2", Node.class, Long.class, String.class, String.class, Action.class));
813
814         PowerMockito.when(neutronL3Adapter, "programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
815
816
817         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.ADD);
818
819         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.ADD));
820
821         Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage1", node, Long.valueOf(12), SEG_ID, CIDR, Action.DELETE);
822
823         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programIpRewriteExclusionStage2", any(Node.class), anyLong(), anyString(), anyString(), eq(Action.DELETE));
824     }
825
826     @Test
827     public void testProgramIpRewriteExclusionStage2() throws Exception {
828         NodeId nodeId = mock(NodeId.class);
829         when(nodeId.getValue()).thenReturn(ID);
830         Node node = mock(Node.class);
831         when(node.getNodeId()).thenReturn(nodeId);
832
833         assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programIpRewriteExclusionStage2", node, Long.valueOf(45), SEG_ID, CIDR, Action.ADD));
834     }
835
836     @SuppressWarnings("unchecked")
837     @Test
838     public void testProgramOutboundIpRewriteStage1() throws Exception{
839
840         MemberModifier.suppress(MemberMatcher.method(NeutronL3Adapter.class, "programOutboundIpRewriteStage2", floatingIpClass, Action.class));
841
842         PowerMockito.when(neutronL3Adapter, "programOutboundIpRewriteStage2", any(floatingIpClass), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
843
844
845         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.ADD);
846
847         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.ADD));
848
849         Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage1", floatingIpObject, Action.DELETE);
850
851         PowerMockito.verifyPrivate(neutronL3Adapter, times(1)).invoke("programOutboundIpRewriteStage2", any(floatingIpClass), eq(Action.DELETE));
852     }
853
854     /*@Test
855     public void testPrepareProgramOutboundIpRewriteStage2() throws Exception {
856         assertEquals("Error, did not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
857
858         PowerMockito.mockStatic(InetAddress.class);
859         InetAddress inetAddress = mock(InetAddress.class);
860         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
861
862         assertEquals("Error, did not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(neutronL3Adapter, "programOutboundIpRewriteStage2", floatingIpObject, Action.ADD));
863     }*/
864
865     @Test
866     public void testGetMaskLenFromCidr() throws Exception {
867         assertEquals("Error, did not return the correct mask", 32, Whitebox.invokeMethod(neutronL3Adapter, "getMaskLenFromCidr", IP_MASK));
868     }
869
870     @Test
871     public void testGetDpidForIntegrationBridge() throws Exception {
872         Southbound southbound = mock(Southbound.class);
873         ConfigurationService configurationService = mock(ConfigurationService.class);
874
875         MemberModifier.field(NeutronL3Adapter.class, "southbound").set(neutronL3Adapter , southbound);
876         MemberModifier.field(NeutronL3Adapter.class, "configurationService").set(neutronL3Adapter , configurationService);
877
878         PowerMockito.when(southbound.getBridge(any(Node.class), anyString())).thenReturn(mock(OvsdbBridgeAugmentation.class));
879         PowerMockito.when(configurationService.getIntegrationBridgeName()).thenReturn("");
880         PowerMockito.when(southbound.getDataPathId(any(Node.class))).thenReturn(45L);
881
882         assertEquals("Error, did not return the correct Dpid", 45L, Whitebox.invokeMethod(neutronL3Adapter, "getDpidForIntegrationBridge", mock(Node.class)));
883     }
884
885     @Test
886     public void testencodeExcplicitOFPort() throws Exception {
887         assertEquals("Error, did not correctly encode the port", OFPort, NeutronL3Adapter.encodeExcplicitOFPort(45L));
888     }
889
890     @Test
891     public void testSetDependencies() throws Exception {
892         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
893         ConfigurationService configurationService = mock(ConfigurationService.class);
894         ArpProvider arpProvider = mock(ArpProvider.class);
895         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
896         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
897         RoutingProvider routingProvider = mock(RoutingProvider.class);
898         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
899         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
900         Southbound southbound = mock(Southbound.class);
901
902         PowerMockito.mockStatic(ServiceHelper.class);
903         PowerMockito.when(ServiceHelper.getGlobalInstance(TenantNetworkManager.class, neutronL3Adapter)).thenReturn(tenantNetworkManager);
904         PowerMockito.when(ServiceHelper.getGlobalInstance(ConfigurationService.class, neutronL3Adapter)).thenReturn(configurationService);
905         PowerMockito.when(ServiceHelper.getGlobalInstance(ArpProvider.class, neutronL3Adapter)).thenReturn(arpProvider);
906         PowerMockito.when(ServiceHelper.getGlobalInstance(InboundNatProvider.class, neutronL3Adapter)).thenReturn(inboundNatProvider);
907         PowerMockito.when(ServiceHelper.getGlobalInstance(OutboundNatProvider.class, neutronL3Adapter)).thenReturn(outboundNatProvider);
908         PowerMockito.when(ServiceHelper.getGlobalInstance(RoutingProvider.class, neutronL3Adapter)).thenReturn(routingProvider);
909         PowerMockito.when(ServiceHelper.getGlobalInstance(L3ForwardingProvider.class, neutronL3Adapter)).thenReturn(l3ForwardingProvider);
910         PowerMockito.when(ServiceHelper.getGlobalInstance(NodeCacheManager.class, neutronL3Adapter)).thenReturn(nodeCacheManager);
911         PowerMockito.when(ServiceHelper.getGlobalInstance(Southbound.class, neutronL3Adapter)).thenReturn(southbound);
912
913         neutronL3Adapter.setDependencies(mock(ServiceReference.class));
914
915         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
916         assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
917         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
918         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
919         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
920         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
921         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
922         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
923         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
924     }
925
926     @Test
927     public void testSetDependenciesObject() throws Exception{
928         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , false);
929
930         INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
931         neutronL3Adapter.setDependencies(iNeutronNetworkCRUD);
932         assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
933
934         INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
935         neutronL3Adapter.setDependencies(iNeutronPortCRUD);
936         assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
937
938         INeutronSubnetCRUD iNeutronSubnetCRUD = mock(INeutronSubnetCRUD.class);
939         neutronL3Adapter.setDependencies(iNeutronSubnetCRUD);
940         assertEquals("Error, did not return the correct object", getField("neutronSubnetCache"), iNeutronSubnetCRUD);
941
942         ArpProvider arpProvider = mock(ArpProvider.class);
943         neutronL3Adapter.setDependencies(arpProvider);
944         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
945
946         InboundNatProvider inboundNatProvider = mock(InboundNatProvider.class);
947         neutronL3Adapter.setDependencies(inboundNatProvider);
948         assertEquals("Error, did not return the correct object", getField("inboundNatProvider"), inboundNatProvider);
949
950         OutboundNatProvider outboundNatProvider = mock(OutboundNatProvider.class);
951         neutronL3Adapter.setDependencies(outboundNatProvider);
952         assertEquals("Error, did not return the correct object", getField("outboundNatProvider"), outboundNatProvider);
953
954         RoutingProvider routingProvider = mock(RoutingProvider.class);
955         neutronL3Adapter.setDependencies(routingProvider);
956         assertEquals("Error, did not return the correct object", getField("routingProvider"), routingProvider);
957
958         L3ForwardingProvider l3ForwardingProvider = mock(L3ForwardingProvider.class);
959         neutronL3Adapter.setDependencies(l3ForwardingProvider);
960         assertEquals("Error, did not return the correct object", getField("l3ForwardingProvider"), l3ForwardingProvider);
961
962         MemberModifier.field(NeutronL3Adapter.class, "enabled").set(neutronL3Adapter , true);
963     }
964
965     private Object getField(String fieldName) throws Exception {
966         Field field = NeutronL3Adapter.class.getDeclaredField(fieldName);
967         field.setAccessible(true);
968         return field.get(neutronL3Adapter);
969     }
970
971     @SuppressWarnings("rawtypes")
972     private Object createFloatingIpObject() throws Exception{
973         Class clazz = Whitebox.getInnerClassType(NeutronL3Adapter.class, "FloatIpData");
974         Constructor [] constructors = clazz.getConstructors();
975         Constructor c  = constructors[0];
976         return c.newInstance(neutronL3Adapter, 415L, 415L, "a", "b", "c", "d", "e");
977     }
978 }