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;
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";
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);
}
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());
}
+
}
@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);
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));
}
}