2 * Copyright (c) 2015 Inocybe and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.impl;
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;
18 import java.lang.reflect.Field;
19 import java.util.ArrayList;
20 import java.util.HashSet;
21 import java.util.List;
24 import java.util.concurrent.ConcurrentMap;
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.openstack.netvirt.api.Action;
43 import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService;
44 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
45 import org.opendaylight.ovsdb.openstack.netvirt.api.TenantNetworkManager;
46 import org.opendaylight.ovsdb.utils.config.ConfigProperties;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
49 import org.powermock.api.mockito.PowerMockito;
50 import org.powermock.core.classloader.annotations.PrepareForTest;
51 import org.powermock.modules.junit4.PowerMockRunner;
54 * Unit test for {@link NeutronL3Adapter}
56 /* TODO SB_MIGRATION */ @Ignore
57 @PrepareForTest(ConfigProperties.class)
58 @RunWith(PowerMockRunner.class)
59 public class NeutronL3AdapterTest {
61 @InjectMocks NeutronL3Adapter neutronL3Adapter;
63 @Mock private ConfigurationService configurationService;
64 @Mock private TenantNetworkManager tenantNetworkManager;
65 @Mock private INeutronNetworkCRUD neutronNetworkCache;
66 @Mock private INeutronSubnetCRUD neutronSubnetCache;
67 @Mock private INeutronPortCRUD neutronPortCache;
68 @Mock NeutronPort neutronPort;
70 private Set<String> inboundIpRewriteCache;
71 private Set<String> outboundIpRewriteCache;
72 private Set<String> inboundIpRewriteExclusionCache;
73 private Set<String> outboundIpRewriteExclusionCache;
74 private Set<String> routerInterfacesCache;
75 private Set<String> staticArpEntryCache;
76 private Set<String> l3ForwardingCache;
77 private Set<String> defaultRouteCache;
78 private Map<String, String> networkIdToRouterMacCache;
79 private Map<String, NeutronRouter_Interface> subnetIdToRouterInterfaceCache;
81 private static final String HOST_ADDRESS = "127.0.0.1";
84 public void setUp() throws Exception{
85 PowerMockito.mockStatic(ConfigProperties.class);
86 PowerMockito.when(ConfigProperties.getProperty(neutronL3Adapter.getClass(), "ovsdb.l3.fwd.enabled")).thenReturn("yes");
88 neutronL3Adapter.init();
90 this.getNeutronL3AdapterFields();
94 private void getNeutronL3AdapterFields() throws Exception{
95 inboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteCache");
96 outboundIpRewriteCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteCache");
97 inboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("inboundIpRewriteExclusionCache");
98 outboundIpRewriteExclusionCache = (Set<String>) getNeutronL3AdapterField("outboundIpRewriteExclusionCache");
99 routerInterfacesCache = (Set<String>) getNeutronL3AdapterField("routerInterfacesCache");
100 staticArpEntryCache = (Set<String>) getNeutronL3AdapterField("staticArpEntryCache");
101 l3ForwardingCache = (Set<String>) getNeutronL3AdapterField("l3ForwardingCache");
102 defaultRouteCache = (Set<String>) getNeutronL3AdapterField("defaultRouteCache");
103 networkIdToRouterMacCache = (Map<String, String>) getNeutronL3AdapterField("networkIdToRouterMacCache");
104 subnetIdToRouterInterfaceCache = (Map<String, NeutronRouter_Interface>) getNeutronL3AdapterField("subnetIdToRouterInterfaceCache");
107 private Object getNeutronL3AdapterField(String fieldName) throws Exception {
108 Field fieldObject = NeutronL3Adapter.class.getDeclaredField(fieldName);
109 fieldObject.setAccessible(true);
110 return fieldObject.get(neutronL3Adapter);
113 private void setUpVar(){
114 Neutron_IPs neutronIP = mock(Neutron_IPs.class);
115 NeutronRouter neutronRouter = mock(NeutronRouter.class);
117 NeutronSubnet neutronSubnet = mock(NeutronSubnet.class);
118 NeutronNetwork neutronNetwork = mock(NeutronNetwork.class);
119 Node node = mock(Node.class);
120 NodeId nodeID = mock(NodeId.class);
122 //Row row = mock(Row.class);
123 //Bridge bridge = mock(Bridge.class);
124 Status status = mock(Status.class);
126 List<Neutron_IPs> list_neutronIP = new ArrayList<Neutron_IPs>();
127 list_neutronIP.add(neutronIP);
129 List<NeutronPort> list_neutronPort = new ArrayList<>();
130 list_neutronPort.add(neutronPort);
132 List<Node> list_nodes = new ArrayList<Node>();
133 list_nodes.add(node);
135 //ConcurrentMap<String, Row> rowMap = mock(ConcurrentMap.class);
136 //rowMap.put("key", row);
138 //Column<GenericTableSchema, Set<String>> bridgeColumnIds = mock(Column.class);
139 Set<String> dpids = new HashSet();
142 when(neutronPort.getFixedIPs()).thenReturn(list_neutronIP);
143 when(neutronPort.getPortUUID()).thenReturn("portUUID");
144 when(neutronPort.getTenantID()).thenReturn("tenantID");
145 when(neutronPort.getNetworkUUID()).thenReturn("networkUUID");
146 when(neutronPort.getMacAddress()).thenReturn("macAddress1").thenReturn("macAddress2");
148 when(neutronIP.getSubnetUUID()).thenReturn("subnetUUID");
149 when(neutronIP.getIpAddress()).thenReturn(HOST_ADDRESS);
151 when(neutronPortCache.getAllPorts()).thenReturn(list_neutronPort);
152 when(neutronPortCache.getPort(anyString())).thenReturn(neutronPort);
154 when(neutronSubnetCache.getSubnet(anyString())).thenReturn(neutronSubnet);
156 when(neutronSubnet.getNetworkUUID()).thenReturn("networkUUID");
158 when(neutronNetworkCache.getNetwork(anyString())).thenReturn(neutronNetwork);
160 when(neutronNetwork.getRouterExternal()).thenReturn(false); // default true
161 when(neutronNetwork.getProviderSegmentationID()).thenReturn("providerSegmentationId1","providerSegmentationId2", "providerSegmentationId3");
162 when(neutronNetwork.getTenantID()).thenReturn("tenantId");
163 when(neutronNetwork.getNetworkUUID()).thenReturn("networkUUID");
165 when(neutronSubnet.getGatewayIP()).thenReturn("gatewayIp");
166 when(neutronSubnet.getCidr()).thenReturn(HOST_ADDRESS + "/32");
167 when(neutronSubnet.getSubnetUUID()).thenReturn("subnetUUID");
169 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(false);
170 when(tenantNetworkManager.isTenantNetworkPresentInNode(any(Node.class), anyString())).thenReturn(true);
172 when(node.getNodeId()).thenReturn(nodeID);
174 when(nodeID.getValue()).thenReturn("nodeId");
176 when(status.isSuccess()).thenReturn(true);
178 /* TODO SB_MIGRATION */
179 //when(connectionService.getBridgeNodes()).thenReturn(list_nodes);
181 when(configurationService.getDefaultGatewayMacAddress(any(Node.class))).thenReturn("defaultGatewayMacAddress");
182 when(configurationService.getIntegrationBridgeName()).thenReturn("brName");
184 //when(ovsdbConfigurationService.getRows(any(Node.class), anyString())).thenReturn(rowMap);
185 //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Bridge.class), any(Row.class))).thenReturn(bridge);
186 //when(ovsdbConfigurationService.getRow(any(Node.class), anyString(), anyString())).thenReturn(row);
188 //when(bridge.getName()).thenReturn("brName");
189 //when(bridge.getDatapathIdColumn()).thenReturn(bridgeColumnIds);
191 //when(bridgeColumnIds.getData()).thenReturn(dpids);
196 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
197 * Device owner = network:router_interface
200 public void testHandleNeutronPortEvent1() {
201 when(neutronPort.getDeviceOwner()).thenReturn("network:router_interface");
203 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
204 // Affected by the add
205 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
206 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
207 /* TODO SB_MIGRATION */
208 //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
209 assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
210 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
211 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
212 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
214 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
215 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
216 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
218 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
219 // Affected by the delete
220 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 0, networkIdToRouterMacCache.size());
221 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
222 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
224 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
225 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
226 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
227 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
228 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
229 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
230 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
234 * Test method {@link NeutronL3Adapter#handleNeutronPortEvent(NeutronPort, Action)}
238 public void testHandleNeutronPortEvent2() {
239 when(neutronPort.getDeviceOwner()).thenReturn("");
241 // populate subnetIdToRouterInterfaceCache to pass the
242 // if (neutronRouterInterface != null)
243 NeutronRouter_Interface neutronRouterInterface = mock(NeutronRouter_Interface.class);
244 when(neutronRouterInterface.getPortUUID()).thenReturn("portUUID");
245 when(neutronRouterInterface.getSubnetUUID()).thenReturn("subnetUUID");
246 subnetIdToRouterInterfaceCache.put("subnetUUID", neutronRouterInterface);
248 /* device owner = "" */
249 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.ADD);
250 // Affected by the add
251 /* TODO SB_MIGRATION */
252 //assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
253 assertEquals("Error, did not return the correct staticArpEntryCache size", 2, staticArpEntryCache.size());
254 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
255 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
256 assertEquals("Error, did not return the correct l3ForwardingCache size", 1, l3ForwardingCache.size());
257 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
259 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
261 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
262 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
263 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
265 neutronL3Adapter.handleNeutronPortEvent(neutronPort, Action.DELETE);
266 // Affected by the delete
267 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
268 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
270 assertEquals("Error, did not return the correct routerInterfacesCache size", 2, routerInterfacesCache.size());
271 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 1, inboundIpRewriteExclusionCache.size());
272 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 1, outboundIpRewriteExclusionCache.size());
273 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
274 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 0, inboundIpRewriteCache.size());
275 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 0, outboundIpRewriteCache.size());
276 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
278 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 1, subnetIdToRouterInterfaceCache.size());
282 * Test method {@link NeutronL3Adapter#handleNeutronFloatingIPEvent(NeutronFloatingIP, Action)}
285 public void testandleNeutronFloatingIPEvent() throws Exception{
286 NeutronFloatingIP neutronFloatingIP = mock(NeutronFloatingIP.class);
287 when(neutronFloatingIP.getFixedIPAddress()).thenReturn(HOST_ADDRESS);
288 when(neutronFloatingIP.getFloatingIPAddress()).thenReturn(HOST_ADDRESS);
289 when(neutronFloatingIP.getFloatingNetworkUUID()).thenReturn("floatingNetworkUUID");
291 networkIdToRouterMacCache.put("floatingNetworkUUID", "routerMacAddress");
293 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.ADD);
295 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
296 // Affected by the add
297 /* TODO SB_MIGRATION */
298 //assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
299 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
300 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
302 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
303 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
304 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
305 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
306 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
307 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());
309 neutronL3Adapter.handleNeutronFloatingIPEvent(neutronFloatingIP, Action.DELETE);
311 assertEquals("Error, did not return the correct networkIdToRouterMacCache size", 1, networkIdToRouterMacCache.size());
312 assertEquals("Error, did not return the correct inboundIpRewriteCache size", 1, inboundIpRewriteCache.size());
313 assertEquals("Error, did not return the correct outboundIpRewriteCache size", 1, outboundIpRewriteCache.size());
314 assertEquals("Error, did not return the correct staticArpEntryCache size", 1, staticArpEntryCache.size());
315 assertEquals("Error, did not return the correct routerInterfacesCache size", 0, routerInterfacesCache.size());
316 assertEquals("Error, did not return the correct inboundIpRewriteExclusionCache size", 0, inboundIpRewriteExclusionCache.size());
317 assertEquals("Error, did not return the correct outboundIpRewriteExclusionCache size", 0, outboundIpRewriteExclusionCache.size());
318 assertEquals("Error, did not return the correct subnetIdToRouterInterfaceCache size", 0, subnetIdToRouterInterfaceCache.size());
319 assertEquals("Error, did not return the correct l3ForwardingCache size", 0, l3ForwardingCache.size());
320 assertEquals("Error, did not return the correct defaultRouteCache size", 0, defaultRouteCache.size());