Merge "Avoid calling the String constructor explicitly"
[netvirt.git] / openstack / net-virt / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / impl / BridgeConfigurationManagerImpl.java
index aa131325bef3362450de6d929452823321052001..d59f53601734280d497669f225369496a3d40f84 100644 (file)
 /*
- * Copyright (C) 2013 Red Hat, Inc.
+ * Copyright (c) 2013, 2015 Red Hat, Inc. 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 http://www.eclipse.org/legal/epl-v10.html
- *
- * Authors : Madhu Venugopal, Brent Salisbury, Sam Hague
  */
+
 package org.opendaylight.ovsdb.openstack.netvirt.impl;
 
-import org.opendaylight.controller.networkconfig.neutron.NeutronNetwork;
-import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
-import org.opendaylight.ovsdb.lib.notation.Row;
-import org.opendaylight.ovsdb.lib.notation.UUID;
+import org.opendaylight.ovsdb.openstack.netvirt.translator.NeutronNetwork;
+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.plugin.api.OvsdbConfigurationService;
-import org.opendaylight.ovsdb.plugin.api.Status;
-import org.opendaylight.ovsdb.plugin.api.StatusCode;
-import org.opendaylight.ovsdb.plugin.api.StatusWithUuid;
-import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
-import org.opendaylight.ovsdb.schema.openvswitch.Interface;
-import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
-import org.opendaylight.ovsdb.schema.openvswitch.Port;
+import org.opendaylight.ovsdb.openstack.netvirt.api.OvsdbTables;
+import org.opendaylight.ovsdb.openstack.netvirt.api.Southbound;
+import org.opendaylight.ovsdb.utils.config.ConfigProperties;
+import org.opendaylight.ovsdb.utils.servicehelper.ServiceHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeNetdev;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathTypeSystem;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ConnectionInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagerEntry;
+import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
+
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
 import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager {
-    static final Logger LOGGER = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
+/**
+ * @author Madhu Venugopal
+ * @author Brent Salisbury
+ * @author Sam Hague (shague@redhat.com)
+ */
+public class BridgeConfigurationManagerImpl implements BridgeConfigurationManager, ConfigInterface {
+    private static final Logger LOG = LoggerFactory.getLogger(BridgeConfigurationManagerImpl.class);
 
     // The implementation for each of these services is resolved by the OSGi Service Manager
     private volatile ConfigurationService configurationService;
     private volatile NetworkingProviderManager networkingProviderManager;
-    private volatile OvsdbConfigurationService ovsdbConfigurationService;
+    private volatile Southbound southbound;
+
+    public void setConfigurationService(ConfigurationService configurationService) {
+        this.configurationService = configurationService;
+    }
 
-    public BridgeConfigurationManagerImpl() {
+    public void setSouthbound(Southbound southbound) {
+        this.southbound = southbound;
     }
 
     @Override
     public String getBridgeUuid(Node node, String bridgeName) {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        try {
-             Map<String, Row> bridgeTable =
-                     ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
-            if (bridgeTable == null) {
-                return null;
-            }
-            for (String key : bridgeTable.keySet()) {
-                Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
-                if (bridge.getName().equals(bridgeName)) {
-                    return key;
-                }
-            }
-        } catch (Exception e) {
-            LOGGER.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
-        }
-        return null;
+        return southbound.getBridgeUuid(node, bridgeName);
     }
 
     @Override
     public boolean isNodeNeutronReady(Node node) {
         Preconditions.checkNotNull(configurationService);
-        return this.getBridgeUuid(node, configurationService.getIntegrationBridgeName()) != null;
+        return southbound.getBridge(node, configurationService.getIntegrationBridgeName()) != null;
     }
 
     @Override
     public boolean isNodeOverlayReady(Node node) {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        return this.isNodeNeutronReady(node)
-               && this.getBridgeUuid(node, configurationService.getNetworkBridgeName()) != null;
+        Preconditions.checkNotNull(configurationService);
+        return isNodeNeutronReady(node)
+                && southbound.getBridge(node, configurationService.getNetworkBridgeName()) != null;
     }
 
     @Override
-    public boolean isPortOnBridge (Node node, Bridge bridge, String portName) {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        for (UUID portsUUID : bridge.getPortsColumn().getData()) {
-            try {
-                Row portRow = ovsdbConfigurationService.getRow(node,
-                                                        ovsdbConfigurationService.getTableName(node, Port.class),
-                                                        portsUUID.toString());
-
-                Port port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
-                if ((port != null) && port.getName().equalsIgnoreCase(portName)) {
-                    return true;
-                }
-            } catch (Exception e) {
-                LOGGER.error("Error getting port {} for bridge domain {}/{}", portsUUID, node, bridge.getName(), e);
-            }
-        }
-
-        return false;
+    public boolean isPortOnBridge (Node bridgeNode, String portName) {
+        return southbound.extractTerminationPointAugmentation(bridgeNode, portName) != null;
     }
 
     @Override
-    public boolean isNodeTunnelReady(Node node) {
+    public boolean isNodeTunnelReady(Node bridgeNode, Node ovsdbNode) {
         Preconditions.checkNotNull(configurationService);
-        Preconditions.checkNotNull(networkingProviderManager);
-
-        /* Is br-int created? */
-        Bridge intBridge = this.getBridge(node, configurationService.getIntegrationBridgeName());
-        if (intBridge == null) {
+        if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, configurationService.getIntegrationBridgeName())) {
+            LOG.trace("isNodeTunnelReady: node: {}, {} missing",
+                    bridgeNode, configurationService.getIntegrationBridgeName());
             return false;
         }
 
-        if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
-            /* Is br-net created? */
-            Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
-            if (netBridge == null) {
-                return false;
-            }
-
-            if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
-                return false;
-            }
-        }
-        return true;
+        return isNodeL3Ready(bridgeNode, ovsdbNode);
     }
 
     @Override
-    public boolean isNodeVlanReady(Node node, NeutronNetwork network) {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        Preconditions.checkNotNull(networkingProviderManager);
+    public boolean isNodeVlanReady(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
+        Preconditions.checkNotNull(configurationService);
 
-        /* is br-int created */
-        Bridge intBridge = this.getBridge(node, configurationService.getIntegrationBridgeName());
-        if (intBridge == null) {
-            LOGGER.trace("isNodeVlanReady: node: {}, br-int missing", node);
+        final String brInt = configurationService.getIntegrationBridgeName();
+        if (!southbound.isBridgeOnOvsdbNode(ovsdbNode, brInt)) {
+            LOG.trace("isNodeVlanReady: node: {}, {} missing", bridgeNode, brInt);
             return false;
         }
 
-        if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
-            /* is br-net created? */
-            Bridge netBridge = this.getBridge(node, configurationService.getNetworkBridgeName());
-
-            if (netBridge == null) {
-                LOGGER.trace("isNodeVlanReady: node: {}, br-net missing", node);
-                return false;
-            }
+        /* Check if physical device is added to br-int. */
+        String phyNetName = getPhysicalInterfaceName(ovsdbNode, network.getProviderPhysicalNetwork());
+        if (!isPortOnBridge(bridgeNode, phyNetName)) {
+            LOG.trace("isNodeVlanReady: node: {}, eth missing", bridgeNode);
+            return false;
+        }
 
-            if (!isNetworkPatchCreated(node, intBridge, netBridge)) {
-                LOGGER.trace("isNodeVlanReady: node: {}, patch missing", node);
-                return false;
-            }
+        return isNodeL3Ready(bridgeNode, ovsdbNode);
+    }
 
-            /* Check if physical device is added to br-net. */
-            String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
-            if (isPortOnBridge(node, netBridge, phyNetName)) {
-                return true;
+    public boolean isNodeL3Ready(Node bridgeNode, Node ovsdbNode) {
+        Preconditions.checkNotNull(configurationService);
+        boolean ready = false;
+        if (configurationService.isL3ForwardingEnabled()) {
+            final String brInt = configurationService.getIntegrationBridgeName();
+            final String brExt = configurationService.getExternalBridgeName();
+            final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
+            final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
+            Preconditions.checkNotNull(portNameInt);
+            Preconditions.checkNotNull(portNameExt);
+
+            if (southbound.isBridgeOnOvsdbNode(ovsdbNode, brExt)) {
+                ready = isNetworkPatchCreated(bridgeNode, southbound.readBridgeNode(ovsdbNode, brExt));
+            } else {
+                LOG.trace("isNodeL3Ready: node: {}, {} missing",
+                        bridgeNode, brExt);
             }
         } else {
-            /* Check if physical device is added to br-int. */
-            String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
-            if (isPortOnBridge(node, intBridge, phyNetName)) {
-                return true;
-            }
+            ready = true;
         }
-
-        LOGGER.trace("isNodeVlanReady: node: {}, eth missing", node);
-        return false;
+        return ready;
     }
 
     @Override
-    public void prepareNode(Node node) {
-        Preconditions.checkNotNull(networkingProviderManager);
+    public void prepareNode(Node ovsdbNode) {
+        Preconditions.checkNotNull(configurationService);
 
         try {
-            this.createIntegrationBridge(node);
+            createIntegrationBridge(ovsdbNode);
         } catch (Exception e) {
-            LOGGER.error("Error creating Integration Bridge on " + node.toString(), e);
+            LOG.error("Error creating Integration Bridge on {}", ovsdbNode, e);
             return;
         }
-        if (networkingProviderManager == null) {
-            LOGGER.error("Error creating internal network. Provider Network Manager unavailable");
+
+        try {
+            if (configurationService.isL3ForwardingEnabled()) {
+                createExternalBridge(ovsdbNode);
+            }
+        } catch (Exception e) {
+            LOG.error("Error creating External Bridge on {}", ovsdbNode, e);
             return;
         }
-        networkingProviderManager.getProvider(node).initializeFlowRules(node);
+        // this node is an ovsdb node so it doesn't have a bridge
+        // so either look up the bridges or just wait for the bridge update to come in
+        // and add the flows there.
+        //networkingProviderManager.getProvider(node).initializeFlowRules(node);
     }
 
-    /*
+    /**
      * Check if the full network setup is available. If not, create it.
      */
     @Override
-    public boolean createLocalNetwork (Node node, NeutronNetwork network) {
+    public boolean createLocalNetwork (Node bridgeNode, NeutronNetwork network) {
         boolean isCreated = false;
+        Node ovsdbNode = southbound.readOvsdbNode(bridgeNode);
+        if (ovsdbNode == null) {
+            //this should never happen
+            LOG.error("createLocalNetwork could not find ovsdbNode from bridge node " + bridgeNode);
+            return false;
+        }
         if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
-            if (!this.isNodeVlanReady(node, network)) {
+            if (!isNodeVlanReady(bridgeNode, ovsdbNode, network)) {
                 try {
-                    isCreated = this.createBridges(node, network);
+                    isCreated = createBridges(bridgeNode, ovsdbNode, network);
                 } catch (Exception e) {
-                    LOGGER.error("Error creating internal net network " + node, e);
+                    LOG.error("Error creating internal vlan net network " + bridgeNode, e);
                 }
             } else {
                 isCreated = true;
             }
         } else if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VXLAN) ||
                    network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_GRE)) {
-            if (!this.isNodeTunnelReady(node)) {
+            if (!isNodeTunnelReady(bridgeNode, ovsdbNode)) {
                 try {
-                    isCreated = this.createBridges(node, network);
+                    isCreated = createBridges(bridgeNode, ovsdbNode, network);
                 } catch (Exception e) {
-                    LOGGER.error("Error creating internal net network " + node, e);
+                    LOG.error("Error creating internal vxlan/gre net network " + bridgeNode, e);
                 }
             } else {
                 isCreated = true;
@@ -222,57 +203,56 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage
         return isCreated;
     }
 
+
+
     @Override
-    public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
+    public String getExternalInterfaceName (Node node, String extNetwork) {
         String phyIf = null;
-        try {
-            Map<String, Row> ovsTable =
-                    ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
-
-            if (ovsTable == null) {
-                LOGGER.error("OpenVSwitch table is null for Node {} ", node);
-                return null;
+        String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
+                configurationService.getProviderMappingsKey());
+        if (providerMaps != null) {
+            for (String map : providerMaps.split(",")) {
+                String[] pair = map.split(":");
+                if (pair[0].equals(extNetwork)) {
+                    phyIf = pair[1];
+                    break;
+                }
             }
+        }
+        if (phyIf == null) {
+            LOG.error("External interface not found for Node: {}, Network {}",
+                    node, extNetwork);
+        }
+        else {
+            LOG.info("External interface found for Node: {}, Network {} is {}",node,extNetwork,phyIf);
+        }
+        return phyIf;
+    }
 
-            // Loop through all the Open_vSwitch rows looking for the first occurrence of other_config.
-            // The specification does not restrict the number of rows so we choose the first we find.
-            for (Row row : ovsTable.values()) {
-                String providerMaps;
-                OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
-                Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
-
-                if (configs == null) {
-                    LOGGER.debug("OpenVSwitch table is null for Node {} ", node);
-                    continue;
-                }
 
-                providerMaps = configs.get(configurationService.getProviderMappingsKey());
-                if (providerMaps == null) {
-                    providerMaps = configurationService.getDefaultProviderMapping();
-                }
 
-                if (providerMaps != null) {
-                    for (String map : providerMaps.split(",")) {
-                        String[] pair = map.split(":");
-                        if (pair[0].equals(physicalNetwork)) {
-                            phyIf = pair[1];
-                            break;
-                        }
-                    }
-                }
+    @Override
+    public String getPhysicalInterfaceName (Node node, String physicalNetwork) {
+        String phyIf = null;
+        String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
+                configurationService.getProviderMappingsKey());
+        if (providerMaps == null) {
+            providerMaps = configurationService.getDefaultProviderMapping();
+        }
 
-                if (phyIf != null) {
+        if (providerMaps != null) {
+            for (String map : providerMaps.split(",")) {
+                String[] pair = map.split(":");
+                if (pair[0].equals(physicalNetwork)) {
+                    phyIf = pair[1];
                     break;
                 }
             }
-        } catch (Exception e) {
-            LOGGER.error("Unable to find physical interface for Node: {}, Network {}",
-                         node, physicalNetwork, e);
         }
 
         if (phyIf == null) {
-            LOGGER.error("Physical interface not found for Node: {}, Network {}",
-                         node, physicalNetwork);
+            LOG.error("Physical interface not found for Node: {}, Network {}",
+                    node, physicalNetwork);
         }
 
         return phyIf;
@@ -281,83 +261,34 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage
     @Override
     public List<String> getAllPhysicalInterfaceNames(Node node) {
         List<String> phyIfName = Lists.newArrayList();
+        String providerMaps = southbound.getOtherConfig(node, OvsdbTables.OPENVSWITCH,
+                configurationService.getProviderMappingsKey());
+        if (providerMaps == null) {
+            providerMaps = configurationService.getDefaultProviderMapping();
+        }
 
-        try {
-            Map<String, Row> ovsTable =
-                    ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, OpenVSwitch.class));
-
-            if (ovsTable == null) {
-                LOGGER.error("OpenVSwitch table is null for Node {} ", node);
-                return null;
-            }
-
-            // While there is only one entry in the HashMap, we can't access it by index...
-            for (Row row : ovsTable.values()) {
-                String bridgeMaps;
-                OpenVSwitch ovsRow = ovsdbConfigurationService.getTypedRow(node, OpenVSwitch.class, row);
-                Map<String, String> configs = ovsRow.getOtherConfigColumn().getData();
-
-                if (configs == null) {
-                    LOGGER.debug("OpenVSwitch table is null for Node {} ", node);
-                    continue;
-                }
-
-                bridgeMaps = configs.get(configurationService.getProviderMappingsKey());
-                if (bridgeMaps == null) {
-                    bridgeMaps = configurationService.getDefaultProviderMapping();
-                }
-
-                if (bridgeMaps != null) {
-                    for (String map : bridgeMaps.split(",")) {
-                        String[] pair = map.split(":");
-                        phyIfName.add(pair[1]);
-                    }
-                }
+        if (providerMaps != null) {
+            for (String map : providerMaps.split(",")) {
+                String[] pair = map.split(":");
+                phyIfName.add(pair[1]);
             }
-        } catch (Exception e) {
-            LOGGER.error("Unable to find physical interface for Node: " + node, e);
         }
 
-        LOGGER.debug("Physical interface for Node: {}, If: {}",
-                     node, phyIfName);
-
         return phyIfName;
     }
 
-    /**
-     * Returns the Bridge for a given node and bridgeName
-     */
-    public Bridge getBridge (Node node, String bridgeName) {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        try {
-            Map<String, Row> bridgeTable =
-                    ovsdbConfigurationService.getRows(node, ovsdbConfigurationService.getTableName(node, Bridge.class));
-            if (bridgeTable != null) {
-                for (String key : bridgeTable.keySet()) {
-                    Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeTable.get(key));
-                    if (bridge.getName().equals(bridgeName)) {
-                        return bridge;
-                    }
-                }
-            }
-        } catch (Exception e) {
-            LOGGER.error("Error getting Bridge Identifier for {} / {}", node, bridgeName, e);
-        }
-        return null;
-    }
-
     /**
      * Returns true if a patch port exists between the Integration Bridge and Network Bridge
      */
-    private boolean isNetworkPatchCreated (Node node, Bridge intBridge, Bridge netBridge) {
+    private boolean isNetworkPatchCreated(Node intBridge, Node netBridge) {
         Preconditions.checkNotNull(configurationService);
 
         boolean isPatchCreated = false;
 
         String portName = configurationService.getPatchPortName(new ImmutablePair<>(intBridge, netBridge));
-        if (isPortOnBridge(node, intBridge, portName)) {
+        if (isPortOnBridge(intBridge, portName)) {
             portName = configurationService.getPatchPortName(new ImmutablePair<>(netBridge, intBridge));
-            if (isPortOnBridge(node, netBridge, portName)) {
+            if (isPortOnBridge(netBridge, portName)) {
                 isPatchCreated = true;
             }
         }
@@ -368,15 +299,24 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage
     /**
      * Creates the Integration Bridge
      */
-    private void createIntegrationBridge (Node node) throws Exception {
+    private boolean createIntegrationBridge(Node ovsdbNode) {
         Preconditions.checkNotNull(configurationService);
 
-        String brInt = configurationService.getIntegrationBridgeName();
+        if (!addBridge(ovsdbNode, configurationService.getIntegrationBridgeName())) {
+            LOG.debug("Integration Bridge Creation failed");
+            return false;
+        }
+        return true;
+    }
 
-        Status status = this.addBridge(node, brInt, null, null);
-        if (!status.isSuccess()) {
-            LOGGER.debug("Integration Bridge Creation Status: {}", status);
+    private boolean createExternalBridge(Node ovsdbNode) {
+        Preconditions.checkNotNull(configurationService);
+
+        if (!addBridge(ovsdbNode, configurationService.getExternalBridgeName())) {
+            LOG.debug("External Bridge Creation failed");
+            return false;
         }
+        return true;
     }
 
     /**
@@ -434,255 +374,264 @@ public class BridgeConfigurationManagerImpl implements BridgeConfigurationManage
                 Interface br-int
                     type: internal
      */
-    private boolean createBridges(Node node, NeutronNetwork network) throws Exception {
+    private boolean createBridges(Node bridgeNode, Node ovsdbNode, NeutronNetwork network) {
         Preconditions.checkNotNull(configurationService);
         Preconditions.checkNotNull(networkingProviderManager);
-        Status status;
 
-        LOGGER.debug("createBridges: node: {}, network type: {}", node, network.getProviderNetworkType());
+        LOG.debug("createBridges: node: {}, network type: {}", bridgeNode, network.getProviderNetworkType());
 
-        if (networkingProviderManager.getProvider(node).hasPerTenantTunneling()) { /* indicates OF 1.0 */
-            String brInt = configurationService.getIntegrationBridgeName();
-            String brNet = configurationService.getNetworkBridgeName();
-            String patchNet = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brNet));
-            String patchInt = configurationService.getPatchPortName(new ImmutablePair<>(brNet, brInt));
+        final String brInt = configurationService.getIntegrationBridgeName();
+        if (! createIntegrationBridge(ovsdbNode)) {
+            LOG.debug("{} Bridge creation failed", brInt);
+            return false;
+        }
 
-            status = this.addBridge(node, brInt, patchNet, patchInt);
-            if (!status.isSuccess()) {
-                LOGGER.debug("{} Bridge Creation Status: {}", brInt, status);
-                return false;
-            }
-            status = this.addBridge(node, brNet, patchInt, patchNet);
-            if (!status.isSuccess()) {
-                LOGGER.debug("{} Bridge Creation Status: {}", brNet, status);
+        if (configurationService.isL3ForwardingEnabled()) {
+            final String brExt = configurationService.getExternalBridgeName();
+            if (! createExternalBridge(ovsdbNode)) {
+                LOG.error("{} Bridge creation failed", brExt);
                 return false;
             }
 
-            /* For vlan network types add physical port to br-net. */
-            if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
-                String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
-                status = addPortToBridge(node, brNet, phyNetName);
-                if (!status.isSuccess()) {
-                    LOGGER.debug("Add Port {} to Bridge {} Status: {}", phyNetName, brNet, status);
-                    return false;
-                }
+            //get two patch port names
+            final String portNameInt = configurationService.getPatchPortName(new ImmutablePair<>(brInt, brExt));
+            final String portNameExt = configurationService.getPatchPortName(new ImmutablePair<>(brExt, brInt));
+            Preconditions.checkNotNull(portNameInt);
+            Preconditions.checkNotNull(portNameExt);
+
+            if (!addPatchPort(bridgeNode, brInt, portNameInt, portNameExt)) {
+                LOG.error("Add Port {} to Bridge {} failed", portNameInt, brInt);
+                return false;
             }
-        } else {
-            String brInt = configurationService.getIntegrationBridgeName();
-            status = this.addBridge(node, brInt, null, null);
-            if (!status.isSuccess()) {
-                LOGGER.debug("{} Bridge Creation Status: {}", brInt, status);
+            Node extBridgeNode = southbound.readBridgeNode(ovsdbNode, brExt);
+            Preconditions.checkNotNull(extBridgeNode);
+            if (!addPatchPort(extBridgeNode, brExt, portNameExt, portNameInt)) {
+                LOG.error("Add Port {} to Bridge {} failed", portNameExt, brExt);
                 return false;
             }
-
-            /* For vlan network types add physical port to br-int. */
-            if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
-                String phyNetName = this.getPhysicalInterfaceName(node, network.getProviderPhysicalNetwork());
-                status = addPortToBridge(node, brInt, phyNetName);
-                if (!status.isSuccess()) {
-                    LOGGER.debug("Add Port {} to Bridge {} Status: {}", phyNetName, brInt, status);
+            String extNetName = getExternalInterfaceName(extBridgeNode, brExt);
+            if ( extNetName != null) {
+                if (!addPortToBridge(extBridgeNode, brExt, extNetName)) {
+                    LOG.error("Add External Port {} to Bridge {} failed", extNetName, brExt);
                     return false;
                 }
+            LOG.info("Add External Port {} to Ext Bridge {} success", extNetName, brExt);
+            }
+        }
+        /* For vlan network types add physical port to br-int. */
+        if (network.getProviderNetworkType().equalsIgnoreCase(NetworkHandler.NETWORK_TYPE_VLAN)) {
+            String phyNetName = this.getPhysicalInterfaceName(bridgeNode, network.getProviderPhysicalNetwork());
+            if (!addPortToBridge(bridgeNode, brInt, phyNetName)) {
+                LOG.debug("Add Port {} to Bridge {} failed", phyNetName, brInt);
+                return false;
             }
         }
 
-        LOGGER.debug("createNetNetwork: node: {}, status: success", node);
+        LOG.info("createBridges: node: {}, status: success", bridgeNode);
         return true;
     }
 
     /**
      * Add a Port to a Bridge
      */
-    private Status addPortToBridge (Node node, String bridgeName, String portName) throws Exception {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-
-        LOGGER.debug("addPortToBridge: Adding port: {} to Bridge {}, Node {}", portName, bridgeName, node);
+    private boolean addPortToBridge (Node node, String bridgeName, String portName) {
+        boolean rv = true;
 
-        String bridgeUUID = this.getBridgeUuid(node, bridgeName);
-        if (bridgeUUID == null) {
-            LOGGER.error("addPortToBridge: Could not find Bridge {} in Node {}", bridgeName, node);
-            return new Status(StatusCode.NOTFOUND, "Could not find "+bridgeName+" in "+node);
-        }
+        if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
+            rv = southbound.addTerminationPoint(node, bridgeName, portName, null);
 
-        /* Check if the port already exists. */
-        Row row = ovsdbConfigurationService
-                .getRow(node, ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
-        Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, row);
-        if (bridge != null) {
-            if (isPortOnBridge(node, bridge, portName)) {
-                LOGGER.debug("addPortToBridge: Port {} already in Bridge {}, Node {}", portName, bridgeName, node);
-                return new Status(StatusCode.SUCCESS);
+            if (rv) {
+                LOG.info("addPortToBridge: node: {}, bridge: {}, portname: {} status: success",
+                        node.getNodeId().getValue(), bridgeName, portName);
+            } else {
+                LOG.error("addPortToBridge: node: {}, bridge: {}, portname: {} status: FAILED",
+                        node.getNodeId().getValue(), bridgeName, portName);
             }
         } else {
-            LOGGER.error("addPortToBridge: Could not find Port {} in Bridge {}, Node {}", portName, bridgeName, node);
-            return new Status(StatusCode.NOTFOUND, "Could not find "+portName+" in "+bridgeName);
-        }
-
-        Port port = ovsdbConfigurationService.createTypedRow(node, Port.class);
-        port.setName(portName);
-        StatusWithUuid statusWithUuid =
-                ovsdbConfigurationService.insertRow(node, port.getSchema().getName(), bridgeUUID, port.getRow());
-        if (!statusWithUuid.isSuccess()) {
-            LOGGER.error("addPortToBridge: Failed to add Port {} in Bridge {}, Node {}", portName, bridgeName, node);
-            return statusWithUuid;
-        }
-
-        String portUUID = statusWithUuid.getUuid().toString();
-        String interfaceUUID = null;
-        int timeout = 6;
-        while ((interfaceUUID == null) && (timeout > 0)) {
-            Row portRow = ovsdbConfigurationService.getRow(node, port.getSchema().getName(), portUUID);
-            port = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
-            Set<UUID> interfaces = port.getInterfacesColumn().getData();
-            if (interfaces == null || interfaces.size() == 0) {
-                // Wait for the OVSDB update to sync up the Local cache.
-                Thread.sleep(500);
-                timeout--;
-                continue;
-            }
-            interfaceUUID = interfaces.toArray()[0].toString();
-            Row intf = ovsdbConfigurationService.getRow(node,
-                                                ovsdbConfigurationService.getTableName(node, Interface.class), interfaceUUID);
-            if (intf == null) {
-                interfaceUUID = null;
-            }
-        }
-
-        if (interfaceUUID == null) {
-            LOGGER.error("addPortToBridge: Cannot identify Interface for port {}/{}", portName, portUUID);
-            return new Status(StatusCode.INTERNALERROR);
+            LOG.trace("addPortToBridge: node: {}, bridge: {}, portname: {} status: not_needed",
+                    node.getNodeId().getValue(), bridgeName, portName);
         }
 
-        return new Status(StatusCode.SUCCESS);
+        return rv;
     }
 
     /**
      * Add a Patch Port to a Bridge
      */
-    private Status addPatchPort (Node node, String bridgeUUID, String portName, String peerPortName) throws Exception {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-
-        LOGGER.debug("addPatchPort: node: {}, bridgeUUID: {}, port: {}, peer: {}",
-                     node, bridgeUUID, portName, peerPortName);
-
-        /* Check if the port already exists. */
-        Row bridgeRow = ovsdbConfigurationService.getRow(node,
-                                                  ovsdbConfigurationService.getTableName(node, Bridge.class), bridgeUUID);
-        Bridge bridge = ovsdbConfigurationService.getTypedRow(node, Bridge.class, bridgeRow);
-        if (bridge != null) {
-            if (isPortOnBridge(node, bridge, portName)) {
-                LOGGER.debug("addPatchPort: Port {} already in Bridge, Node {}", portName, node);
-                return new Status(StatusCode.SUCCESS);
+    private boolean addPatchPort (Node node, String bridgeName, String portName, String peerPortName) {
+        boolean rv = true;
+
+        if (southbound.extractTerminationPointAugmentation(node, portName) == null) {
+            rv = southbound.addPatchTerminationPoint(node, bridgeName, portName, peerPortName);
+
+            if (rv) {
+                LOG.info("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: success",
+                        node.getNodeId().getValue(), bridgeName, portName, peerPortName);
+            } else {
+                LOG.error("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: FAILED",
+                        node.getNodeId().getValue(), bridgeName, portName, peerPortName);
             }
         } else {
-            LOGGER.error("addPatchPort: Could not find Port {} in Bridge, Node {}", portName, node);
-            return new Status(StatusCode.NOTFOUND, "Could not find "+portName+" in Bridge");
-        }
-
-        Port patchPort = ovsdbConfigurationService.createTypedRow(node, Port.class);
-        patchPort.setName(portName);
-        // Create patch port and interface
-        StatusWithUuid statusWithUuid =
-                ovsdbConfigurationService.insertRow(node, patchPort.getSchema().getName(), bridgeUUID, patchPort.getRow());
-        if (!statusWithUuid.isSuccess()) {
-            return statusWithUuid;
-        }
-
-        String patchPortUUID = statusWithUuid.getUuid().toString();
-
-        String interfaceUUID = null;
-        int timeout = 6;
-        while ((interfaceUUID == null) && (timeout > 0)) {
-            Row portRow = ovsdbConfigurationService.getRow(node, patchPort.getSchema().getName(), patchPortUUID);
-            patchPort = ovsdbConfigurationService.getTypedRow(node, Port.class, portRow);
-            Set<UUID> interfaces = patchPort.getInterfacesColumn().getData();
-            if (interfaces == null || interfaces.size() == 0) {
-                // Wait for the OVSDB update to sync up the Local cache.
-                Thread.sleep(500);
-                timeout--;
-                continue;
-            }
-            interfaceUUID = interfaces.toArray()[0].toString();
-        }
-
-        if (interfaceUUID == null) {
-            return new Status(StatusCode.INTERNALERROR);
+            LOG.trace("addPatchPort: node: {}, bridge: {}, portname: {} peer: {} status: not_needed",
+                    node.getNodeId().getValue(), bridgeName, portName, peerPortName);
         }
 
-        Interface intf = ovsdbConfigurationService.createTypedRow(node, Interface.class);
-        intf.setType("patch");
-        Map<String, String> options = Maps.newHashMap();
-        options.put("peer", peerPortName);
-        intf.setOptions(options);
-        return ovsdbConfigurationService.updateRow(node,
-                                            intf.getSchema().getName(),
-                                            patchPortUUID,
-                                            interfaceUUID,
-                                            intf.getRow());
+        return rv;
     }
 
     /**
      * Add Bridge to a Node
      */
-    private Status addBridge(Node node, String bridgeName,
-                             String localPatchName, String remotePatchName) throws Exception {
-        Preconditions.checkNotNull(ovsdbConfigurationService);
-        Preconditions.checkNotNull(networkingProviderManager);
+    private boolean addBridge(Node ovsdbNode, String bridgeName) {
+        boolean rv = true;
+        if ((!southbound.isBridgeOnOvsdbNode(ovsdbNode, bridgeName)) ||
+                (southbound.getBridgeFromConfig(ovsdbNode, bridgeName) == null)) {
+            Class<? extends DatapathTypeBase> dpType = null;
+            if (configurationService.isUserSpaceEnabled()) {
+                dpType = DatapathTypeNetdev.class;
+            }
+            rv = southbound.addBridge(ovsdbNode, bridgeName, getControllersFromOvsdbNode(ovsdbNode), dpType);
+        }
+        return rv;
+    }
 
-        String bridgeUUID = this.getBridgeUuid(node, bridgeName);
-        Bridge bridge = ovsdbConfigurationService.createTypedRow(node, Bridge.class);
-        Set<String> failMode = new HashSet<>();
-        failMode.add("secure");
-        bridge.setFailMode(failMode);
+    private String getControllerIPAddress() {
+        String addressString = ConfigProperties.getProperty(this.getClass(), "ovsdb.controller.address");
+        if (addressString != null) {
+            try {
+                if (InetAddress.getByName(addressString) != null) {
+                    return addressString;
+                }
+            } catch (UnknownHostException e) {
+                LOG.error("Host {} is invalid", addressString);
+            }
+        }
 
-        Set<String> protocols = new HashSet<>();
+        addressString = ConfigProperties.getProperty(this.getClass(), "of.address");
+        if (addressString != null) {
+            try {
+                if (InetAddress.getByName(addressString) != null) {
+                    return addressString;
+                }
+            } catch (UnknownHostException e) {
+                LOG.error("Host {} is invalid", addressString);
+            }
+        }
 
-        /* ToDo: Plugin should expose an easy way to get the OVS Version or Schema Version
-         * or, alternatively it should not attempt to add set unsupported fields
-         */
+        return null;
+    }
 
-        try {
-           protocols.add(Constants.OPENFLOW13);
-            bridge.setProtocols(protocols);
-        } catch (SchemaVersionMismatchException e) {
-            LOGGER.info("Failed to add protocol.", e);
+    private short getControllerOFPort() {
+        short openFlowPort = Constants.OPENFLOW_PORT;
+        String portString = ConfigProperties.getProperty(this.getClass(), "of.listenPort");
+        if (portString != null) {
+            try {
+                openFlowPort = Short.parseShort(portString);
+            } catch (NumberFormatException e) {
+                LOG.warn("Invalid port:{}, use default({})", portString,
+                        openFlowPort);
+            }
         }
+        return openFlowPort;
+    }
 
-        if (bridgeUUID == null) {
-            bridge.setName(bridgeName);
+    private List<String> getControllersFromOvsdbNode(Node node) {
+        List<String> controllersStr = new ArrayList<>();
 
-            StatusWithUuid statusWithUuid = ovsdbConfigurationService.insertRow(node,
-                                                                         bridge.getSchema().getName(),
-                                                                         null,
-                                                                         bridge.getRow());
-            if (!statusWithUuid.isSuccess()) {
-                return statusWithUuid;
-            }
-            bridgeUUID = statusWithUuid.getUuid().toString();
-            Port port = ovsdbConfigurationService.createTypedRow(node, Port.class);
-            port.setName(bridgeName);
-            Status status = ovsdbConfigurationService.insertRow(node, port.getSchema().getName(), bridgeUUID, port.getRow());
-            LOGGER.debug("addBridge: Inserting Bridge {} {} with protocols {} and status {}",
-                         bridgeName, bridgeUUID, protocols, status);
+        String controllerIpStr = getControllerIPAddress();
+        if (controllerIpStr != null) {
+            // If codepath makes it here, the ip address to be used was explicitly provided.
+            // Being so, also fetch openflowPort provided via ConfigProperties.
+            controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
+                    + ":" + controllerIpStr + ":" + getControllerOFPort());
         } else {
-            Status status = ovsdbConfigurationService.updateRow(node,
-                                                         bridge.getSchema().getName(),
-                                                         null,
-                                                         bridgeUUID,
-                                                         bridge.getRow());
-            LOGGER.debug("addBridge: Updating Bridge {} {} with protocols {} and status {}",
-                         bridgeName, bridgeUUID, protocols, status);
+            // Check if ovsdb node has manager entries
+            OvsdbNodeAugmentation ovsdbNodeAugmentation = southbound.extractOvsdbNode(node);
+            if (ovsdbNodeAugmentation != null) {
+                List<ManagerEntry> managerEntries = ovsdbNodeAugmentation.getManagerEntry();
+                if (managerEntries != null && !managerEntries.isEmpty()) {
+                    for (ManagerEntry managerEntry : managerEntries) {
+                        if (managerEntry == null || managerEntry.getTarget() == null) {
+                            continue;
+                        }
+                        String[] tokens = managerEntry.getTarget().getValue().split(":");
+                        if (tokens.length == 3 && tokens[0].equalsIgnoreCase("tcp")) {
+                            controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
+                                    + ":" + tokens[1] + ":" + getControllerOFPort());
+                        } else if (tokens[0].equalsIgnoreCase("ptcp")) {
+                            ConnectionInfo connectionInfo = ovsdbNodeAugmentation.getConnectionInfo();
+                            if (connectionInfo != null && connectionInfo.getLocalIp() != null) {
+                                controllerIpStr = String.valueOf(connectionInfo.getLocalIp().getValue());
+                                controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
+                                        + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
+                            } else {
+                                LOG.warn("Ovsdb Node does not contain connection info: {}", node);
+                            }
+                        } else {
+                            LOG.trace("Skipping manager entry {} for node {}",
+                                    managerEntry.getTarget(), node.getNodeId().getValue());
+                        }
+                    }
+                } else {
+                    LOG.warn("Ovsdb Node does not contain manager entries : {}", node);
+                }
+            }
+        }
+
+        if (controllersStr.isEmpty()) {
+            // Neither user provided ip nor ovsdb node has manager entries. Lets use local machine ip address.
+            LOG.debug("Use local machine ip address as a OpenFlow Controller ip address");
+            controllerIpStr = getLocalControllerHostIpAddress();
+            if (controllerIpStr != null) {
+                controllersStr.add(Constants.OPENFLOW_CONNECTION_PROTOCOL
+                        + ":" + controllerIpStr + ":" + Constants.OPENFLOW_PORT);
+            }
         }
 
-        ovsdbConfigurationService.setOFController(node, bridgeUUID);
+        if (controllersStr.isEmpty()) {
+            LOG.warn("Failed to determine OpenFlow controller ip address");
+        } else if (LOG.isDebugEnabled()) {
+            controllerIpStr = "";
+            for (String currControllerIpStr : controllersStr) {
+                controllerIpStr += " " + currControllerIpStr;
+            }
+            LOG.debug("Found {} OpenFlow Controller(s) :{}", controllersStr.size(), controllerIpStr);
+        }
+
+        return controllersStr;
+    }
 
-        if (localPatchName != null &&
-            remotePatchName != null &&
-            networkingProviderManager.getProvider(node).hasPerTenantTunneling()) {
-            return addPatchPort(node, bridgeUUID, localPatchName, remotePatchName);
+    private String getLocalControllerHostIpAddress() {
+        String ipaddress = null;
+        try{
+            for (Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();ifaces.hasMoreElements();){
+                NetworkInterface iface = ifaces.nextElement();
+
+                for (Enumeration<InetAddress> inetAddrs = iface.getInetAddresses(); inetAddrs.hasMoreElements();) {
+                    InetAddress inetAddr = inetAddrs.nextElement();
+                    if (!inetAddr.isLoopbackAddress() && inetAddr.isSiteLocalAddress()) {
+                        ipaddress = inetAddr.getHostAddress();
+                        break;
+                    }
+                }
+            }
+        }catch (Exception e){
+            LOG.warn("Exception while fetching local host ip address ", e);
         }
-        return new Status(StatusCode.SUCCESS);
+        return ipaddress;
     }
 
+    @Override
+    public void setDependencies(ServiceReference serviceReference) {
+        configurationService =
+                (ConfigurationService) ServiceHelper.getGlobalInstance(ConfigurationService.class, this);
+        networkingProviderManager =
+                (NetworkingProviderManager) ServiceHelper.getGlobalInstance(NetworkingProviderManager.class, this);
+        southbound =
+                (Southbound) ServiceHelper.getGlobalInstance(Southbound.class, this);
+    }
 
+    @Override
+    public void setDependencies(Object impl) {
+    }
 }