Use Topology Node in place of Inventory Node
[ovsdb.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / OF13ProviderTest.java
1 /*
2  * Copyright (c) 2015 Inocybe Technologies.  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.providers.openflow13;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Matchers.same;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.when;
21
22 import java.lang.reflect.Field;
23 import java.net.InetAddress;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Random;
31 import java.util.Set;
32 import java.util.concurrent.BlockingQueue;
33 import java.util.concurrent.ConcurrentHashMap;
34 import java.util.concurrent.ConcurrentMap;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.Mockito;
42 import org.mockito.runners.MockitoJUnitRunner;
43 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
44 import org.opendaylight.neutron.spi.INeutronNetworkCRUD;
45 import org.opendaylight.neutron.spi.INeutronPortCRUD;
46 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
47 import org.opendaylight.neutron.spi.NeutronLoadBalancerPool;
48 import org.opendaylight.neutron.spi.NeutronNetwork;
49 import org.opendaylight.ovsdb.lib.notation.Column;
50 import org.opendaylight.ovsdb.lib.notation.Row;
51 import org.opendaylight.ovsdb.lib.notation.UUID;
52 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
53 import org.opendaylight.ovsdb.openstack.netvirt.AbstractEvent;
54 import org.opendaylight.ovsdb.openstack.netvirt.AbstractHandler;
55 import org.opendaylight.ovsdb.openstack.netvirt.LBaaSHandler;
56 import org.opendaylight.ovsdb.openstack.netvirt.LBaaSPoolHandler;
57 import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler;
58 import org.opendaylight.ovsdb.openstack.netvirt.NeutronCacheUtils;
59 import org.opendaylight.ovsdb.openstack.netvirt.api.*;
60 import org.opendaylight.ovsdb.openstack.netvirt.impl.EventDispatcherImpl;
61 //import org.opendaylight.ovsdb.plugin.api.OvsdbConfigurationService;
62 //import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
63 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
64 import org.opendaylight.ovsdb.schema.openvswitch.Interface;
65 import org.opendaylight.ovsdb.schema.openvswitch.Port;
66 import org.opendaylight.ovsdb.utils.mdsal.node.StringConvertor;
67 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
68 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
69 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
70 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
72 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
74 import org.osgi.framework.ServiceReference;
75 import org.powermock.api.mockito.PowerMockito;
76 import org.powermock.core.classloader.annotations.PrepareForTest;
77 import org.powermock.modules.junit4.PowerMockRunner;
78
79 /**
80  * Unit test for {@link OF13Provider}
81  */
82 @PrepareForTest(OF13Provider.class)
83 @RunWith(PowerMockRunner.class)
84 public class OF13ProviderTest {
85
86     @InjectMocks private OF13Provider of13Provider;
87     @Mock private NeutronNetwork network;
88     @Mock private org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node openflowNode;
89     @Mock private Node node;
90     @Mock private Node node2;
91     @Mock private Node node3;
92     @Mock private Interface intf;
93
94     @Mock private ConfigurationService configurationService;
95     @Mock private BridgeConfigurationManager bridgeConfigurationManager;
96     @Mock private TenantNetworkManager tenantNetworkManager;
97     /* TODO SB_MIGRATION */
98     //@Mock private OvsdbConfigurationService ovsdbConfigurationService;
99     //@Mock private OvsdbConnectionService connectionService;
100     @Mock private MdsalConsumer mdsalConsumer;
101     @Mock private SecurityServicesManager securityServicesManager;
102     @Mock private IngressAclProvider ingressAclProvider;
103     @Mock private EgressAclProvider egressAclProvider;
104     @Mock private ClassifierProvider classifierProvider;
105     @Mock private L2ForwardingProvider l2ForwardingProvider;
106     @Mock private DataBroker dataBroker;
107
108
109     @Before
110     public void setUp() throws Exception{
111         of13Provider = new OF13Provider();
112
113         //Setup mock dependency services.
114         configurationService = Mockito.mock(ConfigurationService.class);
115         bridgeConfigurationManager = Mockito.mock(BridgeConfigurationManager.class);
116         tenantNetworkManager = Mockito.mock(TenantNetworkManager.class);
117         /* TODO SB_MIGRATION */
118         //ovsdbConfigurationService = Mockito.mock(OvsdbConfigurationService.class);
119         //connectionService = Mockito.mock(OvsdbConnectionService.class);
120         mdsalConsumer = Mockito.mock(MdsalConsumer.class);
121         securityServicesManager = Mockito.mock(SecurityServicesManager.class);
122         ingressAclProvider = Mockito.mock(IngressAclProvider.class);
123         egressAclProvider = Mockito.mock(EgressAclProvider.class);
124         classifierProvider = Mockito.mock(ClassifierProvider.class);
125         l2ForwardingProvider = Mockito.mock(L2ForwardingProvider.class);
126         dataBroker = Mockito.mock(DataBroker.class);
127
128         this.SeedMockDependencies();
129
130         List<Node> nodeList = new ArrayList();
131         NodeId nodeId = new NodeId("Node1");
132         NodeKey nodeKey = new NodeKey(nodeId);
133
134         node = Mockito.mock(Node.class);
135         when(node.getNodeId()).thenReturn(nodeId);
136         when(node.getKey()).thenReturn(new NodeKey(nodeId));
137         when(configurationService.getTunnelEndPoint(node)).thenReturn(InetAddress.getByName("192.168.0.1"));
138         nodeList.add(node);
139
140         nodeId = new NodeId("Node2");
141         node2 = Mockito.mock(Node.class);
142         when(node2.getNodeId()).thenReturn(nodeId);
143         when(node2.getKey()).thenReturn(new NodeKey(nodeId));
144         when(configurationService.getTunnelEndPoint(node2)).thenReturn(InetAddress.getByName("192.168.0.2"));
145         nodeList.add(node2);
146
147         nodeId = new NodeId("Node3");
148         node3 = Mockito.mock(Node.class);
149         when(node3.getNodeId()).thenReturn(nodeId);
150         when(node3.getKey()).thenReturn(new NodeKey(nodeId));
151         when(configurationService.getTunnelEndPoint(node3)).thenReturn(InetAddress.getByName("192.168.0.3"));
152         nodeList.add(node3);
153
154         /* TODO SB_MIGRATION */
155         //when(connectionService.getNodes()).thenReturn(nodeList);
156
157         final String key = "key";
158         ConcurrentHashMap<String, Row> bridgeTable = new ConcurrentHashMap();
159         bridgeTable.put(key, new Row());
160
161         Row bridgeRow = Mockito.mock(Row.class);
162         Bridge bridge = Mockito.mock(Bridge.class);
163
164
165         Set<String> paths = new HashSet<String>(Arrays.asList(new String[] { "100"}));
166         Column<GenericTableSchema, Set<String>> dataPathIdColumns = Mockito.mock(Column.class);
167
168         when(dataPathIdColumns.getData()).thenReturn(paths);
169         when(bridge.getDatapathIdColumn()).thenReturn(dataPathIdColumns);
170
171         /* TODO SB_MIGRATION */
172         when(configurationService.getIntegrationBridgeName()).thenReturn(key);
173         //when(ovsdbConfigurationService.getTableName(node, Bridge.class)).thenReturn(key);
174         //when(ovsdbConfigurationService.getRows(node, key)).thenReturn(bridgeTable);
175         //when(ovsdbConfigurationService.getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), key)).thenReturn(bridgeRow);
176         //when(ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow)).thenReturn(bridge);
177
178         Bridge bridge1 = Mockito.mock(Bridge.class);
179         when(bridge1.getName()).thenReturn(key);
180         //when(ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key))).thenReturn(bridge1);
181
182         Port port = mock(Port.class);
183         Column<GenericTableSchema, Set<UUID>> itfaceColumns = mock(Column.class);
184         when(port.getInterfacesColumn()).thenReturn(itfaceColumns);
185         Set<UUID> ifaceUUIDs = new HashSet();
186         ifaceUUIDs.add(mock(UUID.class));
187         when(itfaceColumns.getData()).thenReturn(ifaceUUIDs );
188         //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Port.class), any(Row.class))).thenReturn(port);
189
190         intf = mock(Interface.class);
191
192         Set<Long> ports = new HashSet<Long>(Arrays.asList(new Long[] { 21L, 23L ,80L}));
193         Column<GenericTableSchema, Set<Long>> openFlowPortColumns = Mockito.mock(Column.class);
194         when(openFlowPortColumns.getData()).thenReturn(ports);
195
196         when(intf.getName()).thenReturn("intf1");
197         when(intf.getOpenFlowPortColumn()).thenReturn(openFlowPortColumns);
198
199         Column<GenericTableSchema, Map<String, String>> externalIdColumns = mock(Column.class);
200         Map<String, String> externalIds = new HashMap();
201         externalIds.put(Constants.EXTERNAL_ID_INTERFACE_ID, "portUUID");
202         externalIds.put(Constants.EXTERNAL_ID_VM_MAC, "extMac");
203         when(externalIdColumns.getData()).thenReturn(externalIds);
204
205         when(intf.getExternalIdsColumn()).thenReturn(externalIdColumns);
206         //when(ovsdbConfigurationService.getTypedRow(any(Node.class), same(Interface.class), any(Row.class))).thenReturn(intf);
207
208     }
209
210
211     /**
212      * Tests for defaults
213      *      getName()
214      *      supportsServices()
215      *      hasPerTenantTunneling()
216      */
217     @Test
218     public void verifyObjectDefaultSettings(){
219         assertEquals("Error, getName() - Default provider name is invalid","OF13Provider",of13Provider.getName());
220         assertEquals("Error, supportsServices() - Support services is disabled", true, of13Provider.supportsServices());
221         assertEquals("Error, hasPerTenantTunneling() - Support for per tenant tunnelling is enabled", false, of13Provider.hasPerTenantTunneling());
222     }
223
224     /**
225      * Test method
226      * {@link OF13Provider#notifyFlowCapableNodeEventTest(Long, Action)}
227      */
228     @Test
229     public void notifyFlowCapableNodeEventTest(){
230
231         long flowId = 100;
232         Action action = Action.ADD;
233
234         of13Provider.notifyFlowCapableNodeEvent(flowId, action);
235         verify(mdsalConsumer, times(1)).notifyFlowCapableNodeCreateEvent(Constants.OPENFLOW_NODE_PREFIX + flowId, action);
236     }
237
238     /**
239      * Test method
240      * {@link OF13Provider#initializeFlowRules(Node)}
241      */
242     @Test
243     public void initializeFlowRulesTest(){
244
245         Row row = Mockito.mock(Row.class);
246         //when(ovsdbConfigurationService.getTypedRow(node, Interface.class, row)).thenReturn(intf);
247
248         ConcurrentHashMap<String, Row> intfs = new ConcurrentHashMap();
249         intfs.put("intf1", row);
250
251         NeutronNetwork network = Mockito.mock(NeutronNetwork.class);
252         when(network.getProviderNetworkType()).thenReturn(NetworkHandler.NETWORK_TYPE_VLAN);
253         when(tenantNetworkManager.getTenantNetwork(intf)).thenReturn(network);
254         //when(ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class))).thenReturn(intfs);
255
256         of13Provider.initializeFlowRules(node);
257
258         /**
259          * The final phase of the initialization process is to call triggerInterfaceUpdates(node)
260          * This must call tenantNetworkManager.getTenantNetwork(Interface) for each interface.
261          * Verify that this is called once since we are initializing flow rules for only one interface.
262          */
263         /* TODO SB_MIGRATION */
264         //verify(tenantNetworkManager, times(1)).getTenantNetwork(intf);
265     }
266
267     /**
268      * Test method
269      * {@link OF13Provider#initializeOFFlowRulesTest(Node)}
270      */
271     @Test
272     public void initializeOFFlowRulesTest(){
273
274         of13Provider.initializeOFFlowRules(openflowNode);
275         //verify(connectionService, times(1)).getNodes();
276     }
277
278     /**
279      * Test method
280      * {@link OF13Provider#handleInterfaceUpdateTest(NeutronNetwork, Node, Interface)}
281      */
282     @Test
283     public void handleInterfaceUpdateTest(){
284         NeutronNetwork network = Mockito.mock(NeutronNetwork.class);
285
286         /**
287          * For Vlan network type, test ensures that all parameter validations
288          * passed by ensuring that ovsdbConfigurationService.getRows(node,"interface_table_name))
289          * is called at least once.
290          */
291
292         when(network.getProviderNetworkType()).thenReturn(NetworkHandler.NETWORK_TYPE_VLAN);
293         this.of13Provider.handleInterfaceUpdate(network, node, intf);
294         //verify(ovsdbConfigurationService, times(1)).getRows(node, ovsdbConfigurationService.getTableName(node, Interface.class));
295
296         /**
297          * Ideally we want to verify that the right rule tables are constructed for
298          * each type of network (vlan, gre, vxlan). However, to simplify things, we just
299          * verify that configurationService.getTunnelEndPoint() is called twice for the appropriate
300          * network types and for each of the two remaining nodes.
301          */
302
303         when(network.getProviderNetworkType()).thenReturn(NetworkHandler.NETWORK_TYPE_GRE);
304         this.of13Provider.handleInterfaceUpdate(network, node, intf);this.of13Provider.handleInterfaceUpdate(network, node, intf);
305         /* TODO SB_MIGRATION */
306         //verify(configurationService, times(4)).getTunnelEndPoint(node);
307
308         when(network.getProviderNetworkType()).thenReturn(NetworkHandler.NETWORK_TYPE_VXLAN);
309         this.of13Provider.handleInterfaceUpdate(network, node, intf);this.of13Provider.handleInterfaceUpdate(network, node, intf);
310         //verify(configurationService, times(8)).getTunnelEndPoint(node);
311
312         assertEquals("Error, handleInterfaceUpdate(String, String) - is returning a non NULL value.", null, this.of13Provider.handleInterfaceUpdate("",""));
313     }
314
315     /**
316      * Test method
317      * {@link OF13Provider#handleInterfaceDelete(String, NeutronNetwork, Node, Interface, boolean)}
318      */
319     @Test
320     public void handleInterfaceDeleteTest(){
321         NeutronNetwork network = Mockito.mock(NeutronNetwork.class);
322
323
324         when(network.getProviderNetworkType()).thenReturn(NetworkHandler.NETWORK_TYPE_VLAN);
325         when(bridgeConfigurationManager.getAllPhysicalInterfaceNames(node)).thenReturn(Arrays.asList(new String[] { "eth0", "eth1" ,"eth2"}));
326
327         Column<GenericTableSchema, String> typeColumn = Mockito.mock(Column.class);
328         when(typeColumn.getData()).thenReturn(NetworkHandler.NETWORK_TYPE_VXLAN);
329         when(intf.getTypeColumn()).thenReturn(typeColumn);
330
331         Map<String, String> options = new HashMap();
332         options.put("local_ip", "192.168.0.1");
333         options.put("remote_ip", "10.0.12.0");
334
335         Column<GenericTableSchema, Map<String, String>> optionColumns = Mockito.mock(Column.class);
336         when(intf.getOptionsColumn()).thenReturn(optionColumns);
337
338         Status status = this.of13Provider.handleInterfaceDelete("tunnel1", network, node, intf, true);
339
340         assertEquals("Error, handleInterfaceDelete(String, NeutronNetwor, Node, Interface, boolean) - returned the wrong status.", new Status(StatusCode.SUCCESS), status);
341     }
342
343
344     /**
345      * Test method
346      * {@link OF13Provider#createNodeBuilderTest(String)}
347      */
348     @Test
349     public void createNodeBuilderTest(){
350         final String nodeId="node1";
351
352         NodeBuilder builder = new NodeBuilder();
353         builder.setId(new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId(nodeId));
354         builder.setKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey(builder.getId()));
355
356         NodeBuilder builderStatic = OF13Provider.createNodeBuilder(nodeId);
357
358         assertEquals("Error, createNodeBuilder() returned an invalid Node Builder Id", builderStatic.getId(), builder.getId());
359         assertEquals("Error, createNodeBuilder() returned an invalid Node Builder key", builderStatic.getKey(), builder.getKey());
360     }
361
362     /**
363      * Seeds mock dependencies into the of13Provider object
364      * @throws Exception
365      */
366     private void SeedMockDependencies() throws Exception{
367
368         SeedClassFieldValue(of13Provider, "configurationService", configurationService);
369         SeedClassFieldValue(of13Provider, "bridgeConfigurationManager", bridgeConfigurationManager);
370         SeedClassFieldValue(of13Provider, "tenantNetworkManager", tenantNetworkManager);
371         /* TODO SB_MIGRATION */
372         //SeedClassFieldValue(of13Provider, "ovsdbConfigurationService", ovsdbConfigurationService);
373         //SeedClassFieldValue(of13Provider, "connectionService", connectionService);
374         SeedClassFieldValue(of13Provider, "mdsalConsumer", mdsalConsumer);
375         SeedClassFieldValue(of13Provider, "securityServicesManager", securityServicesManager);
376         SeedClassFieldValue(of13Provider, "ingressAclProvider", ingressAclProvider);
377         SeedClassFieldValue(of13Provider, "egressAclProvider", egressAclProvider);
378         SeedClassFieldValue(of13Provider, "classifierProvider", classifierProvider);
379         SeedClassFieldValue(of13Provider, "l2ForwardingProvider", l2ForwardingProvider);
380         SeedClassFieldValue(of13Provider, "dataBroker", dataBroker);
381     }
382
383     /**
384      * Get the specified field from OF13Provider using reflection
385      * @param instance - the class instance
386      * @param fieldName - the field to retrieve
387      *
388      * @return the desired field
389      */
390     private Object getClassField(OF13Provider instance, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
391         Field field = OF13Provider.class.getDeclaredField(fieldName);
392         field.setAccessible(true);
393         return field.get(instance);
394     }
395
396     /**
397      * Sets the internal value of a field from OF13Provider using reflection
398      * @param instance
399      * @param fieldName
400      * @param value
401      * @throws NoSuchFieldException
402      * @throws SecurityException
403      * @throws IllegalArgumentException
404      * @throws IllegalAccessException
405      */
406     private void SeedClassFieldValue(OF13Provider instance, String fieldName, Object value)throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
407
408         Field field = OF13Provider.class.getDeclaredField(fieldName);
409         field.setAccessible(true);
410         field.set(instance, value);
411     }
412 }