BUG-868: do not use InstanceIdentifier.getPath() 13/7213/1
authorRobert Varga <rovarga@cisco.com>
Mon, 19 May 2014 06:45:47 +0000 (08:45 +0200)
committerRobert Varga <rovarga@cisco.com>
Mon, 19 May 2014 06:45:47 +0000 (08:45 +0200)
This simplifies the NodeMapping class significantly by using the
utility methods shipped in InstanceIdentifier itself.

Change-Id: I1bb79bd4a16da9d78c600d21f67e3ca1926a0445
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/compatibility/sal-compatibility/src/main/java/org/opendaylight/controller/sal/compatibility/NodeMapping.java
opendaylight/md-sal/sal-common-util/src/main/java/org/opendaylight/controller/sal/common/util/Arguments.java

index c4c1ee9..9276238 100644 (file)
@@ -54,10 +54,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.No
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
-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.PathArgument;
 
 import com.google.common.base.Objects;
 import com.google.common.base.Preconditions;
@@ -83,32 +80,20 @@ public final class NodeMapping {
       return  new org.opendaylight.controller.sal.core.Node(NodeMapping.MD_SAL_TYPE, aDNodeId);
   }
 
-  public static NodeId toNodeId(final InstanceIdentifier<? extends Object> node) {
-    Preconditions.<InstanceIdentifier<? extends Object>>checkNotNull(node);
-    List<PathArgument> path = node.getPath();
-    Preconditions.<List<PathArgument>>checkNotNull(path);
-    int size = path.size();
-    Preconditions.checkArgument(size >= 2);
-    final PathArgument arg = path.get(1);
-    final IdentifiableItem item = Arguments.<IdentifiableItem>checkInstanceOf(arg, IdentifiableItem.class);
-    Identifier<?> key = item.getKey();
-    final NodeKey nodeKey = Arguments.<NodeKey>checkInstanceOf(key, NodeKey.class);
-    return nodeKey.getId();
+  public static NodeId toNodeId(final InstanceIdentifier<?> id) {
+    final NodeKey key = id.firstKeyOf(Node.class, NodeKey.class);
+    Preconditions.checkArgument(key != null, "No node identifier found in %s", id);
+    return key.getId();
   }
 
   public static String toADNodeId(final NodeId nodeId) {
-    Preconditions.<NodeId>checkNotNull(nodeId);
     return nodeId.getValue();
   }
 
   public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorRef source) throws ConstructionException {
-    Preconditions.<NodeConnectorRef>checkNotNull(source);
-    final InstanceIdentifier<?> path = Preconditions.<InstanceIdentifier<? extends Object>>checkNotNull(source.getValue());
-    Preconditions.checkArgument(path.getPath().size() >= 3);
-    final PathArgument arg = path.getPath().get(2);
-    final IdentifiableItem item = Arguments.<IdentifiableItem>checkInstanceOf(arg,IdentifiableItem.class);
-    final NodeConnectorKey connectorKey = Arguments.<NodeConnectorKey>checkInstanceOf(item.getKey(), NodeConnectorKey.class);
-    return NodeMapping.toADNodeConnector(connectorKey.getId(), NodeMapping.toNodeId(path));
+    final InstanceIdentifier<?> id = Preconditions.checkNotNull(source.getValue());
+    final NodeConnectorKey key = id.firstKeyOf(NodeConnector.class, NodeConnectorKey.class);
+    return NodeMapping.toADNodeConnector(key.getId(), NodeMapping.toNodeId(id));
   }
 
   public static org.opendaylight.controller.sal.core.NodeConnector toADNodeConnector(final NodeConnectorId ncid, final NodeId nid) throws ConstructionException {
@@ -161,6 +146,7 @@ public final class NodeMapping {
   public static NodeConnectorRef toNodeConnectorRef(final org.opendaylight.controller.sal.core.NodeConnector nodeConnector) {
 
     final NodeRef node = NodeMapping.toNodeRef(nodeConnector.getNode());
+    @SuppressWarnings("unchecked")
     final InstanceIdentifier<Node> nodePath = ((InstanceIdentifier<Node>) node.getValue());
     NodeConnectorId nodeConnectorId = null;
 
index 902665d..a4017c2 100644 (file)
@@ -7,16 +7,16 @@
  */
 package org.opendaylight.controller.sal.common.util;
 
-public class Arguments {
+public final class Arguments {
 
     private Arguments() {
         throw new UnsupportedOperationException("Utility class");
     }
-    
+
     /**
      * Checks if value is instance of provided class
-     * 
-     * 
+     *
+     *
      * @param value Value to check
      * @param type Type to check
      * @return Reference which was checked
@@ -24,7 +24,7 @@ public class Arguments {
     @SuppressWarnings("unchecked")
     public static <T> T checkInstanceOf(Object value, Class<T> type) {
         if(!type.isInstance(value))
-            throw new IllegalArgumentException();
+            throw new IllegalArgumentException(String.format("Value %s is not of type %s", value, type));
         return (T) value;
     }
 }