Use targeted node type for bridge name search 21/77721/2
authorStephen Kitt <skitt@redhat.com>
Tue, 13 Nov 2018 13:31:20 +0000 (14:31 +0100)
committerStephen Kitt <skitt@redhat.com>
Mon, 10 Dec 2018 15:39:19 +0000 (16:39 +0100)
The IdentifiableItem we're looking for when checking for a bridge name
is Node; this changes isBridgeOnOvsdbNode() to look for that
directly. If also changes toString() to a match-specific method to
avoid the temptation to use it for other purposes.

Change-Id: Ib20e2c909098af9e0643f12805d98c5f2a66b651
JIRA: OVSDB-470
Signed-off-by: Stephen Kitt <skitt@redhat.com>
utils/southbound-utils/src/main/java/org/opendaylight/ovsdb/utils/southbound/utils/SouthboundUtils.java

index 2eb797127b764746d27b66e62d7db434de050969..5d86fed567acd0b9368d1dbd0fef7e1ad957557b 100644 (file)
@@ -102,6 +102,7 @@ import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointBuilder;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
+import org.opendaylight.yangtools.yang.binding.Identifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
@@ -962,37 +963,46 @@ public class SouthboundUtils {
     }
 
     public boolean isBridgeOnOvsdbNode(Node ovsdbNode, String bridgeName) {
-        boolean found = false;
         OvsdbNodeAugmentation ovsdbNodeAugmentation = extractNodeAugmentation(ovsdbNode);
         if (ovsdbNodeAugmentation != null) {
             List<ManagedNodeEntry> managedNodes = ovsdbNodeAugmentation.getManagedNodeEntry();
             if (managedNodes != null) {
                 for (ManagedNodeEntry managedNode : managedNodes) {
-                    InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
-                    for (PathArgument bridgeIidPathArg : bridgeIid.getPathArguments()) {
-                        if (toString(bridgeIidPathArg).contains(bridgeName)) {
-                            found = true;
-                            break;
-                        }
+                    if (matchesBridgeName(managedNode, bridgeName)) {
+                        return true;
                     }
                 }
             }
         }
-        return found;
+        return false;
     }
 
     // see OVSDB-470 for background
-    // TODO This is a lot better than the original, but still has a toString, which ideally should be avoided..
-    private String toString(PathArgument pathArgument) {
-        if (pathArgument instanceof IdentifiableItem<?, ?>) {
-            IdentifiableItem<?, ?> identifiableItem = (IdentifiableItem<?, ?>) pathArgument;
-            return identifiableItem.getKey().toString();
-        } else if (pathArgument instanceof Item<?>) {
-            Item<?> item = (Item<?>) pathArgument;
-            return item.getType().getName();
-        } else {
-            throw new IllegalArgumentException("Unknown kind of PathArgument: " + pathArgument);
+    private boolean matchesBridgeName(ManagedNodeEntry managedNode, String bridgeName) {
+        InstanceIdentifier<?> bridgeIid = managedNode.getBridgeRef().getValue();
+        for (PathArgument bridgeIidPathArg : bridgeIid.getPathArguments()) {
+            if (bridgeIidPathArg instanceof IdentifiableItem<?, ?>) {
+                IdentifiableItem<?, ?> identifiableItem = (IdentifiableItem<?, ?>) bridgeIidPathArg;
+                Identifier<?> key = identifiableItem.getKey();
+                if (key instanceof NodeKey) {
+                    // Do not combine the above if with that below, we want to
+                    // avoid the toString() call in the else if this is a NodeKey
+                    NodeKey nodeKey = (NodeKey) key;
+                    if (nodeKey.getNodeId().getValue().contains(bridgeName)) {
+                        return true;
+                    }
+                } else if (key.toString().contains(bridgeName)) {
+                    return true;
+                }
+            } else if (bridgeIidPathArg instanceof Item<?>) {
+                if (((Item<?>) bridgeIidPathArg).getType().getName().contains(bridgeName)) {
+                    return true;
+                }
+            } else {
+                throw new IllegalArgumentException("Unknown kind of PathArgument: " + bridgeIidPathArg);
+            }
         }
+        return false;
     }
 
     public OvsdbBridgeAugmentation getBridgeFromConfig(Node node, String bridge) {