From c0709676882d853cb21ddd017856227ab88fdb79 Mon Sep 17 00:00:00 2001 From: Anil Vishnoi Date: Wed, 3 Jun 2015 19:03:47 +0530 Subject: [PATCH] Bug 3563 - Fix/clean methods that determine the OpenFlow Controller ip address Change-Id: I420a10e769be016efa0a4e6c71d863fd39c12e23 Signed-off-by: Anil Vishnoi (cherry picked from commit 94dbb902c1f020c87407ba0fb87ff770dea01cbc) --- .../openstack/netvirt/api/Constants.java | 4 + .../impl/BridgeConfigurationManagerImpl.java | 92 ++++++++++--------- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/api/Constants.java b/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/api/Constants.java index 321ba32bd..89221884f 100644 --- a/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/api/Constants.java +++ b/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/api/Constants.java @@ -92,4 +92,8 @@ public final class Constants { public static final short OUTBOUND_SNAT = 110; // Ingress ACL table drains traffic to this table private static Long groupId = 1L; + + //6653 is official openflow port. + public static short OPENFLOW_PORT = 6653; + public static String OPENFLOW_CONNECTION_PROTOCOL = "tcp"; } diff --git a/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/impl/BridgeConfigurationManagerImpl.java b/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/impl/BridgeConfigurationManagerImpl.java index 0484075c0..9ecd88602 100644 --- a/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/impl/BridgeConfigurationManagerImpl.java +++ b/openstack/net-virt/src/main/java/org/opendaylight/ovsdb/openstack/netvirt/impl/BridgeConfigurationManagerImpl.java @@ -12,6 +12,7 @@ import org.opendaylight.ovsdb.openstack.netvirt.ConfigInterface; import org.opendaylight.ovsdb.openstack.netvirt.NetworkHandler; import org.opendaylight.ovsdb.openstack.netvirt.api.BridgeConfigurationManager; import org.opendaylight.ovsdb.openstack.netvirt.api.ConfigurationService; +import org.opendaylight.ovsdb.openstack.netvirt.api.Constants; import org.opendaylight.ovsdb.openstack.netvirt.api.NetworkingProviderManager; import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables; import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound; @@ -24,11 +25,13 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology. import com.google.common.base.Preconditions; import com.google.common.collect.Lists; + import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; import java.util.Enumeration; import java.util.List; + import org.apache.commons.lang3.tuple.ImmutablePair; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; @@ -350,7 +353,7 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage return rv; } - private InetAddress getControllerIPAddress() { + private String getControllerIPAddress() { InetAddress controllerIP = null; String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address"); @@ -358,7 +361,7 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage try { controllerIP = InetAddress.getByName(addressString); if (controllerIP != null) { - return controllerIP; + return addressString; } } catch (UnknownHostException e) { LOGGER.error("Host {} is invalid", addressString); @@ -370,38 +373,18 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage try { controllerIP = InetAddress.getByName(addressString); if (controllerIP != null) { - return controllerIP; + return addressString; } } catch (UnknownHostException e) { LOGGER.error("Host {} is invalid", addressString); } } - /* - try { - controllerIP = connection.getClient().getConnectionInfo().getLocalAddress(); - return controllerIP; - } catch (Exception e) { - LOGGER.debug("Invalid connection provided to getControllerIPAddresses", e); - } - */ - - if (addressString != null) { - try { - controllerIP = InetAddress.getByName(addressString); - if (controllerIP != null) { - return controllerIP; - } - } catch (UnknownHostException e) { - LOGGER.error("Host {} is invalid", addressString); - } - } - - return controllerIP; + return null; } private short getControllerOFPort() { - Short defaultOpenFlowPort = 6633; + Short defaultOpenFlowPort = Constants.OPENFLOW_PORT; Short openFlowPort = defaultOpenFlowPort; String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort"); if (portString != null) { @@ -416,30 +399,51 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage } private String getControllerTarget(Node node) { - String target = null; - OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node); - if (ovsdbNodeAugmentation != null) { - ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo(); - String addressStr = new String(connectionInfo.getLocalIp().getValue()); - target = "tcp:" + addressStr + ":6633"; - } else{ - target = getControllerTarget(); + String setControllerStr = null; + String controllerIpStr = null; + short openflowPort = Constants.OPENFLOW_PORT; + //Look at user configuration. + //TODO: In case we move to config subsystem to expose these user facing parameter, + // we will have to modify this code. + + controllerIpStr = getControllerIPAddress(); + + if(controllerIpStr == null){ + // Check if ovsdb node has connection info + OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node); + if (ovsdbNodeAugmentation != null) { + ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo(); + if(connectionInfo != null && connectionInfo.getLocalIp() != null) { + controllerIpStr = new String(connectionInfo.getLocalIp().getValue()); + }else{ + LOGGER.warn("Ovsdb Node does not contains connection info : {}",node); + } + } + }else { + openflowPort = getControllerOFPort(); + } + + if(controllerIpStr == null) { + // Neither user provided ip nor ovsdb node has controller ip, Lets use local machine ip address + LOGGER.debug("Use local machine ip address as a OpenFlow Controller ip address"); + controllerIpStr = getLocalControllerHostIpAddress(); + } + if(controllerIpStr != null){ + LOGGER.debug("Targe OpenFlow Controller found : {}",controllerIpStr); + setControllerStr = Constants.OPENFLOW_CONNECTION_PROTOCOL + ":" + controllerIpStr + ":" + openflowPort; + }else { + LOGGER.warn("Failed to determine OpenFlow controller ip address"); } - return target; + return setControllerStr; } - private String getControllerTarget() { - /* TODO SB_MIGRATION - * hardcoding value, need to find better way to get local ip - */ - //String target = "tcp:" + getControllerIPAddress() + ":" + getControllerOFPort(); - //TODO: dirty fix, need to remove it once we have proper solution + private String getLocalControllerHostIpAddress() { String ipaddress = null; try{ - for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){ + for (Enumeration ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){ NetworkInterface iface = (NetworkInterface) ifaces.nextElement(); - for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) { + for (Enumeration inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) { InetAddress inetAddr = (InetAddress) inetAddrs.nextElement(); if (!inetAddr.isLoopbackAddress()) { if (inetAddr.isSiteLocalAddress()) { @@ -450,9 +454,9 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage } } }catch (Exception e){ - LOGGER.warn("ROYALLY SCREWED : Exception while fetching local host ip address ",e); + LOGGER.warn("Exception while fetching local host ip address ",e); } - return "tcp:"+ipaddress+":6633"; + return ipaddress; } @Override -- 2.36.6