Removed the tunnelEndpoint cache 33/4533/6
authorDave Tucker <dave@dtucker.co.uk>
Tue, 21 Jan 2014 22:29:52 +0000 (22:29 +0000)
committerDave Tucker <dave@dtucker.co.uk>
Wed, 22 Jan 2014 13:43:55 +0000 (13:43 +0000)
Tunnel Endpoint address is now pulled directly from the Open_vSwitch table

Change-Id: I1a7292d2e4921c3d9461c79112a4a68d5fb5f33a
Signed-off-by: Dave Tucker <dave@dtucker.co.uk>
neutron/src/main/java/org/opendaylight/ovsdb/neutron/AdminConfigManager.java
neutron/src/main/java/org/opendaylight/ovsdb/neutron/SouthboundHandler.java
neutron/src/test/java/org/opendaylight/ovsdb/neutron/AdminConfigManagerTest.java

index adfd5813cf053bcd33b7965d0a03ba52001643e5..eecaf0eccf54680fb5bc0332dc2335340741ba67 100644 (file)
@@ -10,8 +10,6 @@
 package org.opendaylight.ovsdb.neutron;
 
 import java.net.InetAddress;
-import java.net.UnknownHostException;
-import java.util.HashMap;
 import java.util.Map;
 
 import org.opendaylight.controller.sal.core.Node;
@@ -31,7 +29,6 @@ public class AdminConfigManager {
     private String tunnelEndpointConfigName;
     private String patchToIntegration;
     private String patchToTunnel;
-    private Map<Node, InetAddress> tunnelEndpoints;
 
     // Refer to /etc/quantum/plugins/openvswitch/ovs_quantum_plugin.ini
     private static String DEFAULT_TUNNEL_ENDPOINT_CONFIG_STRING = "local_ip";
@@ -50,7 +47,6 @@ public class AdminConfigManager {
     private static AdminConfigManager adminConfiguration = new AdminConfigManager();
 
     private AdminConfigManager() {
-        tunnelEndpoints = new HashMap<Node, InetAddress>();
         tunnelEndpointConfigName = System.getProperty(CONFIG_TUNNEL_ENDPOINT_CONFIG);
         integrationBridgeName = System.getProperty(CONFIG_INTEGRATION_BRIDGENAME);
         tunnelBridgeName = System.getProperty(CONFIG_TUNNEL_BRIDGENAME);
@@ -111,59 +107,46 @@ public class AdminConfigManager {
     }
 
     public InetAddress getTunnelEndPoint(Node node) {
-        return tunnelEndpoints.get(node);
-    }
+        InetAddress address = null;
+        OVSDBConfigService ovsdbConfig = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
+        try {
+            Map<String, Table<?>> ovsTable = ovsdbConfig.getRows(node, Open_vSwitch.NAME.getName());
 
-    public void addTunnelEndpoint (Node node, InetAddress address) {
-        tunnelEndpoints.put(node, address);
-    }
+            if (ovsTable == null) {
+                logger.error("Open_vSwitch table is null for Node {} ", node);
+                return null;
+            }
 
-    public boolean isInterested (String tableName) {
-        return tableName.equalsIgnoreCase(Open_vSwitch.NAME.getName());
-    }
+            // While there is only one entry in the HashMap, we can't access it by index...
+            for (Table<?> row : ovsTable.values()) {
+                Open_vSwitch ovsRow = (Open_vSwitch)row;
+                Map<String, String> configs = ovsRow.getOther_config();
 
-    private void populateTunnelEndpoint (Node node, Open_vSwitch row) {
-        Map<String, String> configs = row.getOther_config();
-        if (configs != null) {
-            String tunnelEndpoint = configs.get(tunnelEndpointConfigName);
-            if (tunnelEndpoint != null) {
-                try {
-                    InetAddress address = InetAddress.getByName(tunnelEndpoint);
-                    addTunnelEndpoint(node, address);
-                    logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
-                } catch (UnknownHostException e) {
-                    logger.error("Unable to add tunnel endpoint for node " + node, e);
+                if (configs == null) {
+                    logger.debug("Open_vSwitch table is null for Node {} ", node);
+                    continue;
                 }
-            }
-        }
-    }
 
-    public void populateTunnelEndpoint (Node node) {
-        OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
-        try {
-            Map<String, Table<?>> openvswitchTable = ovsdbTable.getRows(node, Open_vSwitch.NAME.getName());
-            if (openvswitchTable == null) {
-                logger.debug("Open_vSwitch table is null for Node {} ", node);
-                return;
-            }
+                String tunnelEndpoint = configs.get(tunnelEndpointConfigName);
 
-            for (Table<?> row : openvswitchTable.values()) {
-                populateTunnelEndpoint(node, (Open_vSwitch)row);
+                if (tunnelEndpoint == null) {
+                    continue;
+                }
+
+                address = InetAddress.getByName(tunnelEndpoint);
+                logger.debug("Tunnel Endpoint for Node {} {}", node, address.getHostAddress());
+                break;
             }
-        } catch (Exception e) {
+        }
+        catch (Exception e) {
             logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
         }
+
+        return address;
     }
 
-    // Use this later if there is a need to update the tunnel-endpoint dynamically
-    public void populateTunnelEndpoint (Node node, String tableName, Table<?> row) {
-        OVSDBConfigService ovsdbTable = (OVSDBConfigService)ServiceHelper.getGlobalInstance(OVSDBConfigService.class, this);
-        try {
-            if (isInterested(tableName)) {
-                populateTunnelEndpoint(node, (Open_vSwitch)row);
-            }
-        } catch (Exception e) {
-            logger.error("Error populating Tunnel Endpoint for Node {} ", node, e);
-        }
+    public boolean isInterested (String tableName) {
+        return tableName.equalsIgnoreCase(Open_vSwitch.NAME.getName());
     }
+
 }
index d9b20c6f897173c63265471bb1a0acf66fd4fec0..3e4c4187977a31461fe411120636b71bb84af25f 100644 (file)
@@ -193,7 +193,6 @@ public class SouthboundHandler extends BaseHandler implements OVSDBInventoryList
             }
         } else if (Open_vSwitch.NAME.getName().equalsIgnoreCase(tableName)) {
             logger.debug("{} Added / Updated {} , {}, {}", tableName, node, uuid, row);
-            AdminConfigManager.getManager().populateTunnelEndpoint(node);
             try {
                 Map<String, Table<?>> interfaces = this.ovsdbConfigService.getRows(node, Interface.NAME.getName());
                 if (interfaces != null) {
index d121018cf5ae4176dda89c9efcafd4f3194a12b9..255caffc2fa226baec727c390c26a635bd4a6c39 100644 (file)
@@ -39,8 +39,9 @@ import org.powermock.modules.junit4.PowerMockRunner;
 @RunWith(PowerMockRunner.class)
 @PrepareForTest(ServiceHelper.class)
 public class AdminConfigManagerTest {
+
     @Test
-    public void testPopulateTunnelEndpoint() throws Exception {
+    public void testGetTunnelEndpoint() throws Exception {
         InetAddress testAddress = InetAddress.getByName("10.10.10.10");
 
         Node mockNode = mock(Node.class);
@@ -54,13 +55,49 @@ public class AdminConfigManagerTest {
         ovsMap.put("Open_vSwitch", ovsTable);
 
         OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
-        when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
+        when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(null)
+                                                               .thenReturn(ovsMap);
 
         PowerMockito.mockStatic(ServiceHelper.class);
         when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
 
-        AdminConfigManager.getManager().populateTunnelEndpoint(mockNode);
+        // OVSDBConfigService is null
+        assertEquals(null, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
+
+        // Success...
+        assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
+    }
+
+    @Test
+    public void testGetTunnelEndpointWithNullRows() throws Exception {
+        InetAddress testAddress = InetAddress.getByName("10.10.10.10");
+
+        Node mockNode = mock(Node.class);
+
+        Map<String, Table<?>> ovsMap = new HashMap<String, Table<?>>();
+
+        Open_vSwitch nullRow = new Open_vSwitch();
+        Open_vSwitch ovsRow1 = new Open_vSwitch();
+        Open_vSwitch ovsRow2 = new Open_vSwitch();
+        OvsDBMap invalidLocalIp = new OvsDBMap();
+        OvsDBMap localIp = new OvsDBMap();
+
+        ovsRow1.setOther_config(invalidLocalIp);
+
+        localIp.put("local_ip","10.10.10.10");
+        ovsRow2.setOther_config(localIp);
+
+        ovsMap.put("0", nullRow);
+        ovsMap.put("1", ovsRow1);
+        ovsMap.put("2", ovsRow2);
+
+        OVSDBConfigService ovsdbConfig = mock(ConfigurationService.class);
+        when(ovsdbConfig.getRows(any(Node.class), anyString())).thenReturn(ovsMap);
+
+        PowerMockito.mockStatic(ServiceHelper.class);
+        when(ServiceHelper.getGlobalInstance(eq(OVSDBConfigService.class), anyObject())).thenReturn(ovsdbConfig);
 
+        // Success...
         assertEquals(testAddress, AdminConfigManager.getManager().getTunnelEndPoint(mockNode));
     }
 }