Add Utility methods to get a list of controller-ip-addresses and OF Port 82/2382/1
authorMadhu Venugopal <mavenugo@gmail.com>
Tue, 5 Nov 2013 11:06:09 +0000 (03:06 -0800)
committerMadhu Venugopal <mavenugo@gmail.com>
Tue, 5 Nov 2013 11:06:09 +0000 (03:06 -0800)
Change-Id: Id73a705903c2f465a1c697e9935b8bdbd27df3be
Signed-off-by: Madhu Venugopal <mavenugo@gmail.com>
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/Activator.java
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConfigurationService.java

index c52c498a917513d2aa3cd91bbfe80a6109925d4c..2268bc9dfdbfcf6b57485df44be7f102f44d29c8 100755 (executable)
@@ -4,6 +4,7 @@ import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.felix.dm.Component;
+import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService;
 import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
 import org.opendaylight.controller.sal.core.ComponentActivatorAbstractBase;
@@ -67,6 +68,10 @@ public class Activator extends ComponentActivatorAbstractBase {
                     .setService(InventoryServiceInternal.class)
                     .setCallbacks("setInventoryServiceInternal", "unsetInventoryServiceInternal")
                     .setRequired(true));
+            c.add(createServiceDependency()
+                    .setService(IClusterGlobalServices.class)
+                    .setCallbacks("setClusterServices", "unsetClusterServices")
+                    .setRequired(false));
         }
 
         if (imp.equals(ConnectionService.class)) {
index 5b573d3aeb24e06cd11c26b85975f30951be0f0f..4ddd98073769e2aeb5f65f2098af39584121a61c 100755 (executable)
@@ -1,6 +1,8 @@
 package org.opendaylight.ovsdb.plugin;
 
 import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.util.*;
 
 import org.eclipse.osgi.framework.console.CommandInterpreter;
@@ -24,11 +26,13 @@ import org.opendaylight.ovsdb.lib.table.Interface;
 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
 import org.opendaylight.ovsdb.lib.table.Port;
 import org.opendaylight.ovsdb.lib.table.internal.Table;
+import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
 import org.opendaylight.controller.sal.connection.ConnectionConstants;
 import org.opendaylight.controller.sal.core.Node;
 import org.opendaylight.controller.sal.core.NodeConnector;
 import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
 import org.opendaylight.controller.sal.networkconfig.bridgedomain.IPluginInBridgeDomainConfigService;
+import org.opendaylight.controller.sal.utils.NetUtils;
 import org.opendaylight.controller.sal.utils.Status;
 import org.opendaylight.controller.sal.utils.StatusCode;
 import org.osgi.framework.BundleContext;
@@ -45,6 +49,7 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
     IConnectionServiceInternal connectionService;
     InventoryServiceInternal inventoryServiceInternal;
+    private IClusterGlobalServices clusterServices;
     boolean forceConnect = false;
 
     void init() {
@@ -104,6 +109,16 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         }
     }
 
+    public void setClusterServices(IClusterGlobalServices i) {
+        this.clusterServices = i;
+    }
+
+    public void unsetClusterServices(IClusterGlobalServices i) {
+        if (this.clusterServices == i) {
+            this.clusterServices = null;
+        }
+    }
+
     private Connection getConnection (Node node) {
         Connection connection = connectionService.getConnection(node);
         if (connection == null || !connection.getChannel().isActive()) {
@@ -112,6 +127,7 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
         return connection;
     }
+
     /**
      * Add a new bridge
      * @param node Node serving this configuration service
@@ -460,6 +476,57 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         return null;
     }
 
+    private short getControllerOFPort() {
+        Short defaultOpenFlowPort = 6633;
+        Short openFlowPort = defaultOpenFlowPort;
+        String portString = System.getProperty("of.listenPort");
+        if (portString != null) {
+            try {
+                openFlowPort = Short.decode(portString).shortValue();
+            } catch (NumberFormatException e) {
+                logger.warn("Invalid port:{}, use default({})", portString,
+                        openFlowPort);
+            }
+        }
+        return openFlowPort;
+    }
+
+    private List<InetAddress> getControllerIPAddresses() {
+        List<InetAddress> controllers = null;
+        if (clusterServices != null) {
+            controllers = clusterServices.getClusteredControllers();
+            if (controllers != null && controllers.size() > 0) {
+                if (controllers.size() == 1) {
+                    InetAddress controller = controllers.get(0);
+                    if (!controller.equals(InetAddress.getLoopbackAddress())) {
+                        return controllers;
+                    }
+                } else {
+                    return controllers;
+                }
+            }
+        }
+
+        controllers = new ArrayList<InetAddress>();
+        InetAddress controllerIP;
+        Enumeration<NetworkInterface> nets;
+        try {
+            nets = NetworkInterface.getNetworkInterfaces();
+            for (NetworkInterface netint : Collections.list(nets)) {
+                Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
+                for (InetAddress inetAddress : Collections.list(inetAddresses)) {
+                    if (!inetAddress.isLoopbackAddress() &&
+                            NetUtils.isIPv4AddressValid(inetAddress.getHostAddress())) {
+                        controllers.add(inetAddress);
+                    }
+                }
+            }
+        } catch (SocketException e) {
+            controllers.add(InetAddress.getLoopbackAddress());
+        }
+        return controllers;
+    }
+
     public void _ovsconnect (CommandInterpreter ci) {
         String bridgeName = ci.nextArgument();
         if (bridgeName == null) {