Cleanup: remove unnecessary boxing/unboxing
[netvirt.git] / openstack / net-virt / src / test / java / org / opendaylight / netvirt / openstack / netvirt / impl / DistributedArpServiceTest.java
1 /*
2  * Copyright (c) 2016 NEC Corporation 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.netvirt.openstack.netvirt.impl;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyLong;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.when;
19
20 import java.lang.reflect.Field;
21 import java.net.InetAddress;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
31 import org.opendaylight.netvirt.openstack.netvirt.api.ArpProvider;
32 import org.opendaylight.netvirt.openstack.netvirt.api.ConfigurationService;
33 import org.opendaylight.netvirt.openstack.netvirt.api.NodeCacheManager;
34 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
35 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
36 import org.opendaylight.netvirt.openstack.netvirt.api.TenantNetworkManager;
37 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronNetworkCRUD;
38 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronNetwork;
39 import org.opendaylight.netvirt.openstack.netvirt.translator.NeutronPort;
40 import org.opendaylight.netvirt.openstack.netvirt.translator.Neutron_IPs;
41 import org.opendaylight.netvirt.openstack.netvirt.translator.crud.INeutronPortCRUD;
42 import org.opendaylight.netvirt.openstack.netvirt.api.Southbound;
43 import org.opendaylight.netvirt.utils.servicehelper.ServiceHelper;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbTerminationPointAugmentation;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
48
49 import org.osgi.framework.ServiceReference;
50 import org.powermock.api.mockito.PowerMockito;
51 import org.powermock.api.support.membermodification.MemberMatcher;
52 import org.powermock.api.support.membermodification.MemberModifier;
53 import org.powermock.core.classloader.annotations.PrepareForTest;
54 import org.powermock.modules.junit4.PowerMockRunner;
55 import org.powermock.reflect.Whitebox;
56
57 /**
58  * Unit test for {@link DistributedArpService}
59  */
60 @PrepareForTest({ServiceHelper.class, InetAddress.class, DistributedArpService.class})
61 @RunWith(PowerMockRunner.class)
62 public class DistributedArpServiceTest {
63
64     @Mock private DistributedArpService distributedArpService;
65     /**
66      * ID used for testing different scenarios.
67      */
68     private static final String ID = "45";
69     /**
70      * IP used for testing different scenarios.
71      */
72     private static final String IP = "127.0.0.1";
73     /**
74      * MALFORM_IP used for testing different scenarios.
75      */
76     private static final String MALFORM_IP = "127.0.0.1.5";
77     /**
78      * INTF_NAME used for testing different scenarios.
79      */
80     private static final String INTF_NAME = "br-int";
81     /**
82      * UUID used for testing different scenarios.
83      */
84     private static final String UUID = "7da709ff-397f-4778-a0e8-994811272fdb";
85     /**
86      * FIXED_IP_ADDRESS used for testing different scenarios.
87      */
88     private static final String FIXED_IP_ADDRESS = "192.168.1.0";
89     /**
90      * MAC_ADDRESS used for testing different scenarios.
91      */
92     private static final String MAC_ADDRESS = "00:00:5E:00:02:01";
93     /**
94      * MAC_ADDRESS_2 used for testing different scenarios.
95      */
96     private static final String MAC_ADDRESS_2 = "00:00:5E:00:02:02";
97     /**
98      * PORT_INT used for testing different scenarios.
99      */
100     private static final String PORT_INT = "port_int";
101
102     @Before
103     public void setUp() throws Exception{
104         distributedArpService = PowerMockito.spy(new DistributedArpService());
105     }
106
107     /**
108      * Test that checks if @{DistributedArpService#handlePortEvent} is called
109      * and then checks that the port event process to write arp rules for neutron ports based on action.
110      */
111     @Test
112     public void testHandlePortEvent() throws Exception {
113         NeutronPort neutronPortOne = PowerMockito.mock(NeutronPort.class);
114         NeutronPort neutronPortTwo = PowerMockito.mock(NeutronPort.class);
115         List<NeutronPort> list_neutronPort = new ArrayList<>();
116         list_neutronPort.add(neutronPortOne);
117         list_neutronPort.add(neutronPortTwo);
118         INeutronPortCRUD neutronPortCache = PowerMockito.mock(INeutronPortCRUD.class);
119         MemberModifier.field(DistributedArpService.class, "neutronPortCache").set(distributedArpService, neutronPortCache);
120         PowerMockito.when(neutronPortCache, "getAllPorts").thenReturn(list_neutronPort);
121
122         // Suppress the called to these functions.
123         MemberModifier.suppress(MemberMatcher.method(DistributedArpService.class, "handleNeutronPortForArp", NeutronPort.class, Action.class));
124
125         //Case 1: Delete Action.
126         Whitebox.invokeMethod(distributedArpService, "handlePortEvent", neutronPortOne, Action.DELETE);
127         PowerMockito.verifyPrivate(distributedArpService, times(1)).invoke("handleNeutronPortForArp", any(NeutronPort.class), eq(Action.DELETE));
128
129         //Case 2: Add Action.
130         Whitebox.invokeMethod(distributedArpService, "handlePortEvent", neutronPortOne, Action.ADD);
131         PowerMockito.verifyPrivate(distributedArpService, times(2)).invoke("handleNeutronPortForArp", any(NeutronPort.class), eq(Action.ADD));
132     }
133
134     /**
135      * Test that checks if @{DistributedArpService#programStaticRuleStage1} is called
136      * and then checks that the arp rules are added/removed based on neutron port event.
137      */
138     @Test
139     public void testProgramStaticRuleStage1() throws Exception {
140         MemberModifier.suppress(MemberMatcher.method(DistributedArpService.class, "programStaticRuleStage2", Long.class, String.class, String.class, String.class, Action.class));
141         PowerMockito.when(distributedArpService, "programStaticRuleStage2", anyLong(), anyString(), anyString(), anyString(), any(Action.class)).thenReturn(new Status(StatusCode.SUCCESS));
142
143         //Case 1: Add Action.
144         Whitebox.invokeMethod(distributedArpService, "programStaticRuleStage1", 12L, PORT_INT, MAC_ADDRESS, IP, Action.ADD);
145         PowerMockito.verifyPrivate(distributedArpService, times(1)).invoke("programStaticRuleStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
146
147         //Case 2: Delete Action.
148         Whitebox.invokeMethod(distributedArpService, "programStaticRuleStage1", 12L, PORT_INT, MAC_ADDRESS, IP, Action.DELETE);
149         PowerMockito.verifyPrivate(distributedArpService, times(1)).invoke("programStaticRuleStage2", anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
150     }
151
152     /**
153      * Test that checks if @{DistributedArpService#programStaticRuleStage2} is called
154      * and then checks that the arp rules are programmed by invoke arpProvider.
155      */
156     @Test
157     public void testProgramStaticRuleStage2() throws Exception {
158         //Case 1: StatusCode BADREQUEST.
159         assertEquals("Error, this not return the correct status code", new Status(StatusCode.BADREQUEST), Whitebox.invokeMethod(distributedArpService, "programStaticRuleStage2",
160                 45L, PORT_INT, MAC_ADDRESS, MALFORM_IP, Action.ADD));
161         PowerMockito.mockStatic(InetAddress.class);
162         InetAddress inetAddress = mock(InetAddress.class);
163         PowerMockito.when(InetAddress.getByName(anyString())).thenReturn(inetAddress);
164
165         //Case 2: StatusCode SUCCESS.
166         assertEquals("Error, this not return the correct status code", new Status(StatusCode.SUCCESS), Whitebox.invokeMethod(distributedArpService, "programStaticRuleStage2",
167                 45L, PORT_INT, MAC_ADDRESS, IP, Action.DELETE));
168     }
169
170     /**
171      * Test that checks if @{DistributedArpService#handleNeutornPortForArp} is called
172      * and then checks that the arp rules are written based on event for neutron port.
173      */
174     @Test
175     public void testHandleNeutornPortForArp() throws Exception {
176         Neutron_IPs neutronIp = mock(Neutron_IPs.class);
177         when(neutronIp.getIpAddress()).thenReturn(FIXED_IP_ADDRESS);
178         List<Neutron_IPs> neutronIps = new ArrayList<>();
179         neutronIps.add(neutronIp);
180         NeutronPort neutronPort = mock(NeutronPort.class);
181         when(neutronPort.getNetworkUUID()).thenReturn(UUID);
182         when(neutronPort.getMacAddress()).thenReturn(MAC_ADDRESS_2);
183         when(neutronPort.getFixedIPs()).thenReturn(neutronIps);
184         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
185         when(neutronNetwork.getProviderSegmentationID()).thenReturn(ID);
186         List<Node> nodes = new ArrayList<>();
187         nodes.add(mock(Node.class));
188         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
189         MemberModifier.field(DistributedArpService.class, "tenantNetworkManager").set(distributedArpService, tenantNetworkManager);
190         when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), eq(ID))).thenReturn(true);
191         PowerMockito.doReturn(15L).when(distributedArpService, "getDatapathIdIntegrationBridge", any(Node.class));
192         INeutronNetworkCRUD neutronNetworkCache = mock(INeutronNetworkCRUD.class);
193         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
194         MemberModifier.field(DistributedArpService.class, "neutronNetworkCache").set(distributedArpService, neutronNetworkCache);
195         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
196         when(nodeCacheManager.getBridgeNodes()).thenReturn(nodes);
197         MemberModifier.field(DistributedArpService.class, "nodeCacheManager").set(distributedArpService, nodeCacheManager);
198         MemberModifier.field(DistributedArpService.class, "flgDistributedARPEnabled").set(distributedArpService, true);
199
200         // Suppress the called to these functions.
201         MemberModifier.suppress(MemberMatcher.method(DistributedArpService.class, "programStaticRuleStage1", Long.class, String.class, String.class, String.class, Action.class));
202
203         //Case 1: Add Action.
204         Whitebox.invokeMethod(distributedArpService, "handleNeutronPortForArp", neutronPort, Action.ADD);
205         PowerMockito.verifyPrivate(distributedArpService, times(1)).invoke("getDatapathIdIntegrationBridge", any(Node.class));
206         Mockito.verify(distributedArpService, times(1)).programStaticRuleStage1(anyLong(), anyString(), anyString(), anyString(), eq(Action.ADD));
207
208         //Case 2: Delete Action.
209         Whitebox.invokeMethod(distributedArpService, "handleNeutronPortForArp", neutronPort, Action.DELETE);
210         PowerMockito.verifyPrivate(distributedArpService, times(2)).invoke("getDatapathIdIntegrationBridge", any(Node.class));
211         Mockito.verify(distributedArpService, times(1)).programStaticRuleStage1(anyLong(), anyString(), anyString(), anyString(), eq(Action.DELETE));
212     }
213
214     /**
215      * Test that checks if @{DistributedArpService#getDatapathIdIntegrationBridge} is called
216      * and then checks the node integration bridge, then return its datapathID.
217      */
218     @Test
219     public void testGetDatapathIdIntegrationBridge() throws Exception {
220         Southbound southbound = mock(Southbound.class);
221         ConfigurationService configurationService = mock(ConfigurationService.class);
222
223         MemberModifier.field(DistributedArpService.class, "southbound").set(distributedArpService, southbound);
224         MemberModifier.field(DistributedArpService.class, "configurationService").set(distributedArpService, configurationService);
225
226         PowerMockito.when(southbound.getBridge(any(Node.class), anyString())).thenReturn(mock(OvsdbBridgeAugmentation.class));
227         PowerMockito.when(configurationService.getIntegrationBridgeName()).thenReturn("");
228         PowerMockito.when(southbound.getDataPathId(any(Node.class))).thenReturn(45L);
229
230         //Assert check for correct Dp Id.
231         assertEquals("Error, did not return the correct Dpid", 45, (long)Whitebox.invokeMethod(distributedArpService, "getDatapathIdIntegrationBridge", mock(Node.class)));
232     }
233
234     /**
235      * Test that checks if @{DistributedArpService#processInterfaceEvent} is called
236      * and then checks that the event is processing.
237      */
238     @Test
239     public void testProcessInterfaceEvent() throws Exception {
240         NeutronPort neutronPort = mock(NeutronPort.class);
241         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
242         PowerMockito.doNothing().when(distributedArpService).handlePortEvent(any(NeutronPort.class), any(Action.class));
243         // init instance variables.
244         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
245         MemberModifier.field(DistributedArpService.class, "tenantNetworkManager").set(distributedArpService , tenantNetworkManager);
246
247         // Mock variables
248         NodeId nodeId = mock(NodeId.class);
249         when(nodeId.getValue()).thenReturn(ID);
250         Node node = mock(Node.class);
251         when(node.getNodeId()).thenReturn(nodeId);
252
253         OvsdbTerminationPointAugmentation intf = mock(OvsdbTerminationPointAugmentation.class);
254         when(intf.getName()).thenReturn(INTF_NAME);
255
256         when(tenantNetworkManager.getTenantPort(intf)).thenReturn(neutronPort);
257
258         //Case 1: Add Action.
259         distributedArpService.processInterfaceEvent(node, intf, neutronNetwork, Action.ADD);
260         Mockito.verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.ADD);
261
262         //Case 2: Delete Action.
263         distributedArpService.processInterfaceEvent(node, intf, neutronNetwork, Action.DELETE);
264         Mockito.verify(distributedArpService, times(1)).handlePortEvent(neutronPort, Action.DELETE);
265     }
266
267     /**
268      * Test that checks if @{DistributedArpService#setDependencies} is called
269      * and then checks the object instances.
270      */
271     @Test
272     public void testSetDependencies() throws Exception {
273         TenantNetworkManager tenantNetworkManager = mock(TenantNetworkManager.class);
274         ConfigurationService configurationService = mock(ConfigurationService.class);
275         ArpProvider arpProvider = mock(ArpProvider.class);
276         NodeCacheManager nodeCacheManager = mock(NodeCacheManager.class);
277         Southbound southbound = mock(Southbound.class);
278
279         ServiceHelper.overrideGlobalInstance(TenantNetworkManager.class, tenantNetworkManager);
280         ServiceHelper.overrideGlobalInstance(ConfigurationService.class, configurationService);
281         ServiceHelper.overrideGlobalInstance(ArpProvider.class, arpProvider);
282         ServiceHelper.overrideGlobalInstance(NodeCacheManager.class, nodeCacheManager);
283         ServiceHelper.overrideGlobalInstance(Southbound.class, southbound);
284
285         distributedArpService.setDependencies(mock(ServiceReference.class));
286
287         assertEquals("Error, did not return the correct object", getField("tenantNetworkManager"), tenantNetworkManager);
288         assertEquals("Error, did not return the correct object", getField("configurationService"), configurationService);
289         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
290         assertEquals("Error, did not return the correct object", getField("nodeCacheManager"), nodeCacheManager);
291         assertEquals("Error, did not return the correct object", getField("southbound"), southbound);
292     }
293
294     /**
295      * Test that checks if @{DistributedArpService#setDependencies} is called
296      * and then checks the object instances.
297      */
298     @Test
299     public void testSetDependenciesObject() throws Exception{
300         INeutronNetworkCRUD iNeutronNetworkCRUD = mock(INeutronNetworkCRUD.class);
301         distributedArpService.setDependencies(iNeutronNetworkCRUD);
302         assertEquals("Error, did not return the correct object", getField("neutronNetworkCache"), iNeutronNetworkCRUD);
303
304         INeutronPortCRUD iNeutronPortCRUD = mock(INeutronPortCRUD.class);
305         distributedArpService.setDependencies(iNeutronPortCRUD);
306         assertEquals("Error, did not return the correct object", getField("neutronPortCache"), iNeutronPortCRUD);
307
308         ArpProvider arpProvider = mock(ArpProvider.class);
309         distributedArpService.setDependencies(arpProvider);
310         assertEquals("Error, did not return the correct object", getField("arpProvider"), arpProvider);
311     }
312
313     private Object getField(String fieldName) throws Exception {
314         Field field = DistributedArpService.class.getDeclaredField(fieldName);
315         field.setAccessible(true);
316         return field.get(distributedArpService);
317     }
318 }