Use Topology Node in place of Inventory Node
[ovsdb.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.anyString;
14 import static org.mockito.Matchers.same;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.concurrent.ConcurrentMap;
25
26 import org.junit.Before;
27 import org.junit.Ignore;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
33 import org.opendaylight.neutron.spi.INeutronPortCRUD;
34 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
35 import org.opendaylight.neutron.spi.NeutronFloatingIP;
36 import org.opendaylight.neutron.spi.NeutronNetwork;
37 import org.opendaylight.neutron.spi.NeutronPort;
38 import org.opendaylight.neutron.spi.NeutronRouter;
39 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
40 import org.opendaylight.neutron.spi.NeutronSubnet;
41 import org.opendaylight.neutron.spi.Neutron_IPs;
42 import org.opendaylight.ovsdb.lib.notation.Column;
43 import org.opendaylight.ovsdb.lib.notation.Row;
44 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
46 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
47 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
48 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
49 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
50 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
51 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
53 import org.powermock.api.mockito.PowerMockito;
54 import org.powermock.core.classloader.annotations.PrepareForTest;
55 import org.powermock.modules.junit4.PowerMockRunner;
56
57 /**
58  * Unit test for {@link NeutronL3Adapter}
59  */
60 /* TODO SB_MIGRATION */ @Ignore
61 @PrepareForTest(ConfigProperties.class)
62 @RunWith(PowerMockRunner.class)
63 public class NeutronL3AdapterTest {
64
65     @InjectMocks NeutronL3Adapter neutronL3Adapter;
66
67     @Mock private ConfigurationService configurationService;
68     @Mock private TenantNetworkManager tenantNetworkManager;
69     @Mock private INeutronNetworkCRUD neutronNetworkCache;
70     @Mock private INeutronSubnetCRUD neutronSubnetCache;
71     @Mock private INeutronPortCRUD neutronPortCache;
72     @Mock NeutronPort neutronPort;
73
74     private Set<String> inboundIpRewriteCache;
75     private Set<String> outboundIpRewriteCache;
76     private Set<String> inboundIpRewriteExclusionCache;
77     private Set<String> outboundIpRewriteExclusionCache;
78     private Set<String> routerInterfacesCache;
79     private Set<String> staticArpEntryCache;
80     private Set<String> l3ForwardingCache;
81     private Set<String> defaultRouteCache;
82     private Map<String, String> networkIdToRouterMacCache;
83     private Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache;
84
85     private static final String HOST_ADDRESS = "127.0.0.1";
86
87     @Before
88     public void setUp() throws Exception{
89         PowerMockito.mockStatic(ConfigProperties.class);
90         PowerMockito.when(ConfigProperties.getProperty(neutronL3Adapter.getClass(), "ovsdb.l3.fwd.enabled")).thenReturn("yes");
91
92         neutronL3Adapter.init();
93
94         this.getNeutronL3AdapterFields();
95         this.setUpVar();
96     }
97
98     private void getNeutronL3AdapterFields() throws Exception{
99         inboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteCache");
100         outboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteCache");
101         inboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteExclusionCache");
102         outboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteExclusionCache");
103         routerInterfacesCache = (Set<String>) getNeutronL3AdapterField("routerInterfacesCache");
104         staticArpEntryCache = (Set<String>) getNeutronL3AdapterField("staticArpEntryCache");
105         l3ForwardingCache = (Set<String>) getNeutronL3AdapterField("l3ForwardingCache");
106         defaultRouteCache = (Set<String>) getNeutronL3AdapterField("defaultRouteCache");
107         networkIdToRouterMacCache = (Map<String, String>) getNeutronL3AdapterField("networkIdToRouterMacCache");
108         subnetIdToRouterInterfaceCache = (Map<String, NeutronRouter_Interface>) getNeutronL3AdapterField("subnetIdToRouterInterfaceCache");
109     }
110
111     private Object getNeutronL3AdapterField(String fieldName) throws Exception {
112         Field fieldObject = NeutronL3Adapter.class.getDeclaredField(fieldName);
113         fieldObject.setAccessible(true);
114         return fieldObject.get(neutronL3Adapter);
115     }
116
117     private void setUpVar(){
118         Neutron_IPs neutronIP = mock(Neutron_IPs.class);
119         NeutronRouter neutronRouter = mock(NeutronRouter.class);
120
121         NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
122         NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
123         Node node = mock(Node.class);
124         NodeId nodeID = mock(NodeId.class);
125         Row row = mock(Row.class);
126         Bridge bridge = mock(Bridge.class);
127         Status status = mock(Status.class);
128
129         List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
130         list_neutronIP.add(neutronIP);
131
132         List<NeutronPort> list_neutronPort = new ArrayList<>();
133         list_neutronPort.add(neutronPort);
134
135         List<Node> list_nodes = new ArrayList<Node>();
136         list_nodes.add(node);
137
138         ConcurrentMap<String, Row> rowMap = mock(ConcurrentMap.class);
139         rowMap.put("key", row);
140
141         Column<GenericTableSchema, Set<String>> bridgeColumnIds = mock(Column.class);
142         Set<String> dpids = new HashSet();
143         dpids.add("11111");
144
145         when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
146         when(neutronPort.getPortUUID()).thenReturn("portUUID");
147         when(neutronPort.getTenantID()).thenReturn("tenantID");
148         when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
149         when(neutronPort.getMacAddress()).thenReturn("macAddress1").thenReturn("macAddress2");
150
151         when(neutronIP.getSubnetUUID()).thenReturn("subnetUUID");
152         when(neutronIP.getIpAddress()).thenReturn(HOST_ADDRESS);
153
154         when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
155         when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
156
157         when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
158
159         when(neutronSubnet.getNetworkUUID()).thenReturn("networkUUID");
160
161         when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
162
163         when(neutronNetwork.getRouterExternal()).thenReturn(false); // default true
164         when(neutronNetwork.getProviderSegmentationID()).thenReturn("providerSegmentationId1","providerSegmentationId2", "providerSegmentationId3");
165         when(neutronNetwork.getTenantID()).thenReturn("tenantId");
166         when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
167
168         when(neutronSubnet.getGatewayIP()).thenReturn("gatewayIp");
169         when(neutronSubnet.getCidr()).thenReturn(HOST_ADDRESS + "/32");
170         when(neutronSubnet.getSubnetUUID()).thenReturn("subnetUUID");
171
172         when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(false);
173         when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(true);
174
175         when(node.getNodeId()).thenReturn(nodeID);
176
177         when(nodeID.getValue()).thenReturn("nodeId");
178
179         when(status.isSuccess()).thenReturn(true);
180
181         /* TODO SB_MIGRATION */
182         //when(connectionService.getNodes()).thenReturn(list_nodes);
183
184         when(configurationService.getDefaultGatewayMacAddress(any(Node.class))).thenReturn("defaultGatewayMacAddress");
185         when(configurationService.getIntegrationBridgeName()).thenReturn("brName");
186
187         //when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(rowMap);
188         //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
189         //when(ovsdbConfigurationService.getRow(any(Node.class), anyString(), anyString())).thenReturn(row);
190
191         when(bridge.getName()).thenReturn("brName");
192         when(bridge.getDatapathIdColumn()).thenReturn(bridgeColumnIds);
193
194         when(bridgeColumnIds.getData()).thenReturn(dpids);
195     }
196
197
198     /**
199      * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
200      * Device owner = network:router_interface
201      */
202     @Test
203     public void testHandleNeutronPortEvent1() {
204         when(neutronPort.getDeviceOwner()).thenReturn("network:router_interface");
205
206         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
207         // Affected by the add
208         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
209         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
210         /* TODO SB_MIGRATION */
211         //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
212         assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
213         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
214         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
215         assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
216         // Unchanged
217         assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
218         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
219         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
220
221         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
222         // Affected by the delete
223         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 0, networkIdToRouterMacCache.size());
224         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
225         assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
226         // Unchanged
227         assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
228         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
229         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
230         assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
231         assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
232         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
233         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
234     }
235
236     /**
237      * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
238      * Device owner = ""
239      */
240     @Test
241     public void testHandleNeutronPortEvent2() {
242         when(neutronPort.getDeviceOwner()).thenReturn("");
243
244         // populate subnetIdToRouterInterfaceCache to pass the
245         // if (neutronRouterInterface != null)
246         NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
247         when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
248         when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
249         subnetIdToRouterInterfaceCache.put("subnetUUID", neutronRouterInterface);
250
251         /* device owner = "" */
252         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
253         // Affected by the add
254         /* TODO SB_MIGRATION */
255         //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
256         assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
257         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
258         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
259         assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
260         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
261         // Added above
262         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
263         // Unchanged
264         assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
265         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
266         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
267
268         neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
269         // Affected by the delete
270         assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
271         assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
272         // Unchanged
273         assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
274         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
275         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
276         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
277         assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
278         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
279         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
280         // Added above
281         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
282     }
283
284     /**
285      * Test method {@link NeutronL3Adapter#handleNeutronFloatingIPEvent(NeutronFloatingIP, Action)}
286      */
287     @Test
288     public void testandleNeutronFloatingIPEvent() throws Exception{
289         NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
290         when(neutronFloatingIP.getFixedIPAddress()).thenReturn(HOST_ADDRESS);
291         when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(HOST_ADDRESS);
292         when(neutronFloatingIP.getFloatingNetworkUUID()).thenReturn("floatingNetworkUUID");
293
294         networkIdToRouterMacCache.put("floatingNetworkUUID", "routerMacAddress");
295
296         neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
297         // Added above
298         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
299         // Affected by the add
300         /* TODO SB_MIGRATION */
301         //assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
302         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
303         assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
304         // Unchanged
305         assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
306         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
307         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
308         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
309         assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
310         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
311
312         neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
313         // Unchanged
314         assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
315         assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
316         assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
317         assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
318         assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
319         assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
320         assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
321         assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
322         assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
323         assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
324     }
325
326 }