Squashed commit of the following:
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / transactions / md / OvsdbBridgeUpdateCommand.java
index 41079fc7f3d784059d51a97e499cf87d5bcb7ba1..d8d618455cc46e078433bfa858345cddeaedad6f 100644 (file)
@@ -1,22 +1,29 @@
 package org.opendaylight.ovsdb.southbound.transactions.md;
 
+import java.net.InetAddress;
+import java.net.NetworkInterface;
 import java.util.ArrayList;
+import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+import org.apache.commons.lang3.math.NumberUtils;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.ovsdb.lib.error.SchemaVersionMismatchException;
 import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.Bridge;
-import org.opendaylight.ovsdb.southbound.OvsdbClientKey;
+import org.opendaylight.ovsdb.schema.openvswitch.Controller;
 import org.opendaylight.ovsdb.southbound.SouthboundConstants;
 import org.opendaylight.ovsdb.southbound.SouthboundMapper;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.DatapathId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeAugmentation;
@@ -33,8 +40,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigs;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.BridgeOtherConfigsKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ControllerEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.bridge.attributes.ProtocolEntryKey;
+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.ManagedNodeEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntryBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
@@ -47,13 +56,14 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.net.InetAddresses;
 
 public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbBridgeUpdateCommand.class);
     private Map<UUID,Bridge> updatedBridgeRows;
     private Map<UUID, Bridge> oldBridgeRows;
 
-    public OvsdbBridgeUpdateCommand(OvsdbClientKey key, TableUpdates updates,
+    public OvsdbBridgeUpdateCommand(ConnectionInfo key, TableUpdates updates,
             DatabaseSchema dbSchema) {
         super(key,updates,dbSchema);
         updatedBridgeRows = TyperUtils.extractRowsUpdated(Bridge.class, getUpdates(), getDbSchema());
@@ -69,8 +79,8 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
 
     private void updateBridge(ReadWriteTransaction transaction,
             Bridge bridge) {
-        final InstanceIdentifier<Node> connectionIId = getKey().toInstanceIndentifier();
-        Optional<Node> connection = readNode(transaction, getKey().toInstanceIndentifier());
+        final InstanceIdentifier<Node> connectionIId = SouthboundMapper.createInstanceIdentifier(getConnectionInfo());
+        Optional<Node> connection = readNode(transaction, connectionIId);
         if (connection.isPresent()) {
             LOG.debug("Connection {} is present",connection);
 
@@ -79,7 +89,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
             transaction.merge(LogicalDatastoreType.OPERATIONAL, connectionIId, connectionNode);
 
             // Update the bridge node with whatever data we are getting
-            InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
+            InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
             Node bridgeNode = buildBridgeNode(bridge);
             transaction.merge(LogicalDatastoreType.OPERATIONAL, bridgeIid, bridgeNode);
             deleteEntries(transaction, protocolEntriesToRemove(bridgeIid,bridge));
@@ -153,19 +163,23 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
                 new ArrayList<InstanceIdentifier<ProtocolEntry>>();
         Bridge oldBridge = oldBridgeRows.get(bridge.getUuid());
 
-        if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
-            for (String protocol: oldBridge.getProtocolsColumn().getData()) {
-                if (bridge.getProtocolsColumn() == null
-                        || !bridge.getProtocolsColumn().getData().contains(protocol)) {
-                    Class<? extends OvsdbBridgeProtocolBase> proto =
-                            SouthboundConstants.OVSDB_PROTOCOL_MAP.inverse().get(protocol);
-                    InstanceIdentifier<ProtocolEntry> iid = bridgeIid
-                            .augmentation(OvsdbBridgeAugmentation.class)
-                            .child(ProtocolEntry.class,
-                                    new ProtocolEntryKey(proto));
-                    result.add(iid);
+        try {
+            if (oldBridge != null && oldBridge.getProtocolsColumn() != null) {
+                for (String protocol : oldBridge.getProtocolsColumn().getData()) {
+                    if (bridge.getProtocolsColumn() == null || !bridge.getProtocolsColumn().getData()
+                                .contains(protocol)) {
+                        Class<? extends OvsdbBridgeProtocolBase> proto = SouthboundConstants.OVSDB_PROTOCOL_MAP
+                                .inverse().get(protocol);
+                        InstanceIdentifier<ProtocolEntry> iid = bridgeIid
+                                .augmentation(OvsdbBridgeAugmentation.class)
+                                .child(ProtocolEntry.class,
+                                        new ProtocolEntryKey(proto));
+                        result.add(iid);
+                    }
                 }
             }
+        } catch (SchemaVersionMismatchException e) {
+            LOG.warn("protocol not supported by this version of ovsdb", e);
         }
         return result;
     }
@@ -176,7 +190,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         try {
             node = transaction.read(LogicalDatastoreType.OPERATIONAL, connectionIid).checkedGet();
         } catch (final ReadFailedException e) {
-            LOG.debug("Read Operational/DS for Node fail! {}", connectionIid, e);
+            LOG.warn("Read Operational/DS for Node fail! {}", connectionIid, e);
         }
         return node;
     }
@@ -185,11 +199,12 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
             Bridge bridge) {
         //Update node with managed node reference
         NodeBuilder connectionNode = new NodeBuilder();
-        connectionNode.setNodeId(SouthboundMapper.createNodeId(getKey().getIp(),getKey().getPort()));
+        connectionNode.setNodeId(SouthboundMapper.createNodeId(getConnectionInfo().getRemoteIp(),
+                getConnectionInfo().getRemotePort()));
 
         OvsdbNodeAugmentationBuilder ovsdbConnectionAugmentationBuilder = new OvsdbNodeAugmentationBuilder();
         List<ManagedNodeEntry> managedBridges = new ArrayList<ManagedNodeEntry>();
-        InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
+        InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
         ManagedNodeEntry managedBridge = new ManagedNodeEntryBuilder().setBridgeRef(
                 new OvsdbBridgeRef(bridgeIid)).build();
         managedBridges.add(managedBridge);
@@ -203,7 +218,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
 
     private Node buildBridgeNode(Bridge bridge) {
         NodeBuilder bridgeNodeBuilder = new NodeBuilder();
-        InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getKey(),bridge);
+        InstanceIdentifier<Node> bridgeIid = SouthboundMapper.createInstanceIdentifier(getConnectionInfo(),bridge);
         NodeId bridgeNodeId = SouthboundMapper.createManagedNodeId(bridgeIid);
         bridgeNodeBuilder.setNodeId(bridgeNodeId);
         OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder = new OvsdbBridgeAugmentationBuilder();
@@ -215,6 +230,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
         setExternalIds(ovsdbBridgeAugmentationBuilder, bridge);
         setOtherConfig(ovsdbBridgeAugmentationBuilder, bridge);
         setFailMode(ovsdbBridgeAugmentationBuilder, bridge);
+        setOpenFlowNodeRef(ovsdbBridgeAugmentationBuilder, bridge);
         setManagedBy(ovsdbBridgeAugmentationBuilder);
         bridgeNodeBuilder.addAugmentation(OvsdbBridgeAugmentation.class, ovsdbBridgeAugmentationBuilder.build());
 
@@ -224,7 +240,7 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
     }
 
     private void setManagedBy(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder) {
-        InstanceIdentifier<Node> connectionNodePath = getKey().toInstanceIndentifier();
+        InstanceIdentifier<Node> connectionNodePath = SouthboundMapper.createInstanceIdentifier(getConnectionInfo());
         ovsdbBridgeAugmentationBuilder.setManagedBy(new OvsdbNodeRef(connectionNodePath));
     }
 
@@ -303,4 +319,50 @@ public class OvsdbBridgeUpdateCommand extends AbstractTransactionCommand {
             ovsdbBridgeAugmentationBuilder.setDatapathId(dpid);
         }
     }
+
+    private void setOpenFlowNodeRef(OvsdbBridgeAugmentationBuilder ovsdbBridgeAugmentationBuilder,
+            Bridge bridge) {
+        Map<UUID, Controller> updatedControllerRows =
+                TyperUtils.extractRowsUpdated(Controller.class, getUpdates(), getDbSchema());
+        LOG.debug("setOpenFlowNodeRef: updatedControllerRows: {}", updatedControllerRows);
+        for (ControllerEntry controllerEntry: SouthboundMapper.createControllerEntries(bridge, updatedControllerRows)) {
+            if (controllerEntry != null
+                && controllerEntry.isIsConnected() != null && controllerEntry.isIsConnected()) {
+                String [] controllerTarget = controllerEntry.getTarget().getValue().split(":");
+                IpAddress bridgeControllerIpAddress = null;
+                PortNumber bridgeControllerPortNumber = null;
+                for (String targetElement : controllerTarget) {
+                    if (InetAddresses.isInetAddress(targetElement)) {
+                        bridgeControllerIpAddress = new IpAddress(targetElement.toCharArray());
+                        continue;
+                    }
+                    if (NumberUtils.isNumber(targetElement)) {
+                        bridgeControllerPortNumber = new PortNumber(
+                                Integer.valueOf(String.valueOf(targetElement)));
+                        continue;
+                    }
+                }
+                try {
+                    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
+                networkInterfacesLoop:
+                    while (networkInterfaces.hasMoreElements()) {
+                        NetworkInterface networkInterface = networkInterfaces.nextElement();
+                        Enumeration<InetAddress> networkInterfaceAddresses = networkInterface.getInetAddresses();
+                        while (networkInterfaceAddresses.hasMoreElements()) {
+                            InetAddress networkInterfaceAddress = networkInterfaceAddresses.nextElement();
+                            if (bridgeControllerIpAddress.getIpv4Address().getValue()
+                                    .equals(networkInterfaceAddress.getHostAddress())) {
+                                ovsdbBridgeAugmentationBuilder.setBridgeOpenflowNodeRef(
+                                        SouthboundMapper.createInstanceIdentifier(bridgeControllerIpAddress,
+                                                bridgeControllerPortNumber));
+                                break networkInterfacesLoop;
+                            }
+                        }
+                    }
+                } catch (Exception e) {
+                    LOG.warn("Error getting local ip address {}", e);
+                }
+            }
+        }
+    }
 }