added getConnectionInstance by Node 99/29599/1
authorVishal Thapar <vishal.thapar@ericsson.com>
Thu, 12 Nov 2015 15:13:23 +0000 (20:43 +0530)
committerVishal Thapar <vishal.thapar@ericsson.com>
Thu, 12 Nov 2015 15:13:23 +0000 (20:43 +0530)
This is required for https://git.opendaylight.org/gerrit/#/c/29575/

Change-Id: Iceab17399b973dab7c1fbf47d21af6989aa2f49e
Signed-off-by: Vishal Thapar <vishal.thapar@ericsson.com>
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepConnectionManager.java
hwvtepsouthbound/hwvtepsouthbound-impl/src/main/java/org/opendaylight/ovsdb/hwvtepsouthbound/HwvtepSouthboundUtil.java

index bac5a71b763b33f4f6fc890bd08aac89dd146c4c..2d4f49b7d0b5115d331cbbcd19967f1cdf827874 100644 (file)
@@ -21,6 +21,7 @@ import java.util.concurrent.ExecutionException;
 import javax.annotation.Nonnull;
 
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
@@ -30,6 +31,8 @@ import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipL
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.HwvtepGlobalRemoveCommand;
 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.PhysicalSwitchRemoveCommand;
 import org.opendaylight.ovsdb.hwvtepsouthbound.transactions.md.TransactionInvoker;
@@ -44,6 +47,8 @@ import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.hardwarevtep.Global;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.PhysicalSwitchAugmentation;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -53,6 +58,7 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.CheckedFuture;
 
 public class HwvtepConnectionManager implements OvsdbConnectionListener, AutoCloseable{
     private Map<ConnectionInfo, HwvtepConnectionInstance> clients =
@@ -181,18 +187,50 @@ public class HwvtepConnectionManager implements OvsdbConnectionListener, AutoClo
         ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
         return clients.get(connectionInfo);
     }
+
     public HwvtepConnectionInstance getConnectionInstance(Node node) {
         Preconditions.checkNotNull(node);
         HwvtepGlobalAugmentation hwvtepGlobal = node.getAugmentation(HwvtepGlobalAugmentation.class);
+        PhysicalSwitchAugmentation pSwitchNode = node.getAugmentation(PhysicalSwitchAugmentation.class);
         if (hwvtepGlobal != null) {
             return getConnectionInstance(hwvtepGlobal.getConnectionInfo());
         } //TODO: We could get it from Managers also.
-        else {
+        else if(pSwitchNode != null){
+            return getConnectionInstance(pSwitchNode);
+        } else {
             LOG.warn("This is not a node that gives any hint how to find its OVSDB Manager: {}",node);
             return null;
         }
     }
 
+    public HwvtepConnectionInstance getConnectionInstance(InstanceIdentifier<Node> nodePath) {
+        try {
+            ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
+            CheckedFuture<Optional<Node>, ReadFailedException> nodeFuture = transaction.read(
+                    LogicalDatastoreType.OPERATIONAL, nodePath);
+            transaction.close();
+            Optional<Node> optional = nodeFuture.get();
+            if (optional != null && optional.isPresent() && optional.get() instanceof Node) {
+                return this.getConnectionInstance(optional.get());
+            } else {
+                LOG.warn("Found non-topological node {} on path {}",optional);
+                return null;
+            }
+        } catch (Exception e) {
+            LOG.warn("Failed to get Hwvtep Node {}",nodePath, e);
+            return null;
+        }
+    }
+
+    private HwvtepConnectionInstance getConnectionInstance(HwvtepPhysicalSwitchAttributes pNode) {
+        Optional<HwvtepGlobalAugmentation> optional = HwvtepSouthboundUtil.getManagingNode(db, pNode);
+        if(optional.isPresent()) {
+            return getConnectionInstance(optional.get().getConnectionInfo());
+        } else {
+            return null;
+        }
+    }
+
     private void removeConnectionInstance(ConnectionInfo key) {
         ConnectionInfo connectionInfo = HwvtepSouthboundMapper.suppressLocalIpPort(key);
         clients.remove(connectionInfo);
index 19f933ab06c8fee596f7bdc39642a26748407f7c..8592e4286d1c76e01789d4318a7026aae5387f8b 100644 (file)
@@ -8,10 +8,16 @@
 
 package org.opendaylight.ovsdb.hwvtepsouthbound;
 
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
 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.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepLogicalSwitchAttributes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepPhysicalSwitchAttributes;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.codec.DeserializationException;
@@ -19,6 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.CheckedFuture;
 
 public class HwvtepSouthboundUtil {
@@ -77,4 +84,77 @@ public class HwvtepSouthboundUtil {
         }
         return result;
     }
+
+    public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
+                    HwvtepPhysicalSwitchAttributes pNode) {
+        Preconditions.checkNotNull(pNode);
+        Optional<HwvtepGlobalAugmentation> result = null;
+        HwvtepGlobalRef ref = pNode.getManagedBy();
+        if (ref != null && ref.getValue() != null) {
+            result = getManagingNode(db, ref);
+        } else {
+            LOG.warn("Cannot find client for PhysicalSwitch without a specified ManagedBy {}", pNode);
+            return Optional.absent();
+        }
+        if (!result.isPresent()) {
+            LOG.warn("Failed to find managing node for PhysicalSwitch {}", pNode);
+        }
+        return result;
+    }
+
+    public static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db,
+                    HwvtepLogicalSwitchAttributes lNode) {
+        Preconditions.checkNotNull(lNode);
+        Optional<HwvtepGlobalAugmentation> result = null;
+        // TODO: Add managed-by to hwvtep-logical-switch-attributes in
+        // hwvtep.yang
+        /*
+         * HwvtepGlobalRef ref = lNode.getManagedBy(); if (ref != null &&
+         * ref.getValue() != null) { result = getManagingNode(db, ref); } else {
+         * LOG.warn(
+         * "Cannot find client for LogicalSwitch without a specified ManagedBy {}"
+         * , pNode); return Optional.absent(); } if(!result.isPresent()) {
+         * LOG.warn("Failed to find managing node for PhysicalSwitch {}",
+         * pNode); } return result;
+         */
+        return Optional.absent(); // TODO: Delete this once yang is updated
+    }
+
+    private static Optional<HwvtepGlobalAugmentation> getManagingNode(DataBroker db, HwvtepGlobalRef ref) {
+        try {
+            ReadOnlyTransaction transaction = db.newReadOnlyTransaction();
+            @SuppressWarnings("unchecked")
+            // Note: erasure makes this safe in combination with the typecheck
+            // below
+            InstanceIdentifier<Node> path = (InstanceIdentifier<Node>) ref.getValue();
+
+            CheckedFuture<Optional<Node>, ReadFailedException> nf =
+                            transaction.read(LogicalDatastoreType.OPERATIONAL, path);
+            transaction.close();
+            Optional<Node> optional = nf.get();
+            if (optional != null && optional.isPresent()) {
+                HwvtepGlobalAugmentation hwvtepNode = null;
+                Node node = optional.get();
+                if (node instanceof HwvtepGlobalAugmentation) {
+                    hwvtepNode = (HwvtepGlobalAugmentation) node;
+                } else if (node != null) {
+                    hwvtepNode = node.getAugmentation(HwvtepGlobalAugmentation.class);
+                }
+                if (hwvtepNode != null) {
+                    return Optional.of(hwvtepNode);
+                } else {
+                    LOG.warn("Hwvtep switch claims to be managed by {} but " + "that HwvtepNode does not exist",
+                                    ref.getValue());
+                    return Optional.absent();
+                }
+            } else {
+                LOG.warn("Mysteriously got back a thing which is *not* a topology Node: {}", optional);
+                return Optional.absent();
+            }
+        } catch (Exception e) {
+            LOG.warn("Failed to get HwvtepNode {}", ref, e);
+            return Optional.absent();
+        }
+    }
+
 }