Merge remote-tracking branch 'origin/master' into merge-branch
[netvirt.git] / neutron / src / test / java / org / opendaylight / ovsdb / neutron / AdminConfigManagerTest.java
index 7ef27dec559a6e2055ff5ee11b60d4dd3934faf6..6e3f6774fd07e6bd1cfe27d63b19302790445be1 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013 Hewlett-Packard Development Company, L.P.
+ * Copyright (c) 2013 Hewlett-Packard Development Company, L.P. and others
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7,11 +7,13 @@
  *
  * Contributors:
  *    Dave Tucker (HP) - Added unit tests for the AdminConfigManager class.
+ *    Sam Hague - Added unit tests for getPhysicalInterfaceName.
  *******************************************************************************/
 
 package org.opendaylight.ovsdb.neutron;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
 import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.anyObject;
 import static org.mockito.Matchers.anyString;
@@ -23,6 +25,7 @@ import java.net.InetAddress;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.opendaylight.controller.sal.core.Node;
@@ -40,6 +43,38 @@ import org.powermock.modules.junit4.PowerMockRunner;
 @PrepareForTest(ServiceHelper.class)
 public class AdminConfigManagerTest {
 
+    AdminConfigManager adminConfigManager;
+    private OVSDBConfigService ovsdbConfig;
+    private Node node;
+    private Open_vSwitch ovsTable;
+    private ConcurrentMap<String, Table<?>> ovsMap;
+    private OvsDBMap map;
+
+    private static String OPENVSWITCH = "Open_vSwitch";
+    private static String PROVIDER_MAPPINGS = "provider_mappings";
+    private static String PHYSNET1 = "physnet1";
+    private static String ETH1 = "eth1";
+    private static String PHYSNET2 = "physnet2";
+    private static String ETH2 = "eth2";
+    private static String PHYSNET3 = "physnet3";
+    private static String ETH3 = "eth3";
+    private static String LOCAL_IP = "local_ip";
+    private static String IPADDR = "10.10.10.10";
+
+    @Before
+    public void setUp(){
+        adminConfigManager = new AdminConfigManager();
+
+        node = mock(Node.class);
+        ovsdbConfig = mock(ConfigurationService.class);
+        PowerMockito.mockStatic(ServiceHelper.class);
+        when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
+
+        ovsTable = new Open_vSwitch();
+        ovsMap = new ConcurrentHashMap<>();
+        map = new OvsDBMap();
+    }
+
     @Test
     public void testGetTunnelEndpoint() throws Exception {
         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
@@ -62,10 +97,10 @@ public class AdminConfigManagerTest {
         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
 
         // OVSDBConfigService is null
-        assertEquals(null, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
+        assertEquals(null, adminConfigManager.getTunnelEndPoint(mockNode));
 
         // Success...
-        assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
+        assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
     }
 
     @Test
@@ -98,6 +133,74 @@ public class AdminConfigManagerTest {
         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
 
         // Success...
-        assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
+        assertEquals(testAddress, adminConfigManager.getTunnelEndPoint(mockNode));
+    }
+
+    // Add key:value pairs to the map.
+    // Calling again with the same key will overwrite the current pair.
+    private void initMap (String key, String value) {
+        map.put(key, value);
+        ovsTable.setOther_config(map);
+        ovsMap.put(OPENVSWITCH, ovsTable);
+    }
+
+    @Test
+    public void testGetPhysicalInterfaceName () throws Exception {
+        when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
+
+        // Check if match can be found with a single pair
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1);
+        assertEquals("Failed to find " + ETH1 + " in " + map.toString(),
+                ETH1, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
+
+        // Check if match can be found with different pairs
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2);
+        assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
+                ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
+
+        // Check if match can be found with duplicate pairs
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2 + "," + PHYSNET2 + ":" + ETH2);
+        assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
+                ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
+
+        // Check if match can be found with multiple pairs and extra other_config
+        initMap(LOCAL_IP, IPADDR);
+        assertEquals("Failed to find " + ETH2 + " in " + map.toString(),
+                ETH2, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET2));
+    }
+
+    @Test
+    public void testGetPhysicalInterfaceNameNegative () throws Exception {
+        when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
+                .thenReturn(ovsMap);
+
+        // Add a null row, an empty row and a good row to the table
+        Open_vSwitch nullRow = new Open_vSwitch();
+        Open_vSwitch emptyRow = new Open_vSwitch();
+        OvsDBMap emptyProviderMap = new OvsDBMap();
+        emptyRow.setOther_config(emptyProviderMap);
+        ovsMap.put("0", nullRow);
+        ovsMap.put("1", emptyRow);
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1);
+
+        // Check if no rows/no table is handled
+        assertEquals("Failed to return null when ovsdb table is null",
+                null, adminConfigManager.getTunnelEndPoint(node));
+
+        // Check if the null and empty rows are ignored
+        System.out.println("map = " + map.toString());
+        System.out.println("ovsMap = " + ovsMap.toString());
+        assertEquals("Failed to find " + ETH1 + " in " + map.toString(),
+                ETH1, adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
+
+        // Should not be able to find match
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + ":" + ETH1 + "," + PHYSNET2 + ":" + ETH2);
+        assertNull("Found " + ETH3 + " in " + map.toString(),
+                adminConfigManager.getPhysicalInterfaceName(node, PHYSNET3));
+
+        // Should not be able to find match with mal-formed values
+        initMap(PROVIDER_MAPPINGS, PHYSNET1 + "-" + ETH1);
+        assertNull("Found " + ETH1 + " in " + map.toString(),
+                adminConfigManager.getPhysicalInterfaceName(node, PHYSNET1));
     }
 }