Bug 499: Fixed normalization of InstanceIdentifier for backwards compatibility 49/5849/4
authorRobert Varga <rovarga@cisco.com>
Wed, 26 Mar 2014 18:29:13 +0000 (19:29 +0100)
committerRobert Varga <rovarga@cisco.com>
Fri, 4 Apr 2014 13:29:02 +0000 (15:29 +0200)
Change-Id: I633eb004729c6013df49c3f82420adf6d2417ea9
Signed-off-by: Tony Tkacik <ttkacik@cisco.com>
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/md-sal/sal-common-impl/src/main/java/org/opendaylight/controller/md/sal/common/impl/util/compat/DataNormalizationOperation.java
opendaylight/md-sal/sal-common-impl/src/main/java/org/opendaylight/controller/md/sal/common/impl/util/compat/DataNormalizer.java

index de90f48..941f2fd 100644 (file)
@@ -61,6 +61,11 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
         return false;
     }
 
+
+    public boolean isKeyedEntry() {
+        return false;
+    }
+
     protected Set<QName> getQNameIdentifiers() {
         return Collections.singleton(identifier.getNodeType());
     }
@@ -129,6 +134,11 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             return Builders.leafSetEntryBuilder().withNodeIdentifier(nodeId).withValue(node.getValue()).build();
         }
 
+
+        @Override
+        public boolean isKeyedEntry() {
+            return true;
+        }
     }
 
     private static abstract class CompositeNodeNormalizationOpertation<T extends PathArgument> extends
@@ -243,7 +253,9 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
         protected NormalizedNodeContainerBuilder createBuilder(final CompositeNode compositeNode) {
             ImmutableMap.Builder<QName, Object> keys = ImmutableMap.builder();
             for (QName key : keyDefinition) {
-                SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),"List node %s MUST contain leaf %s with value.",getIdentifier().getNodeType(),key);
+
+                SimpleNode<?> valueNode = checkNotNull(compositeNode.getFirstSimpleByName(key),
+                        "List node %s MUST contain leaf %s with value.", getIdentifier().getNodeType(), key);
                 keys.put(key, valueNode.getValue());
             }
 
@@ -263,6 +275,12 @@ public abstract class DataNormalizationOperation<T extends PathArgument> impleme
             }
             return builder.build();
         }
+
+
+        @Override
+        public boolean isKeyedEntry() {
+            return true;
+        }
     }
 
     private static final class ContainerNormalization extends DataContainerNormalizationOperation<NodeIdentifier> {
index 9487f21..28b2bde 100644 (file)
@@ -4,6 +4,7 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import java.util.AbstractMap;
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
@@ -11,6 +12,7 @@ import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeWithValue;
 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.Node;
 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
@@ -31,12 +33,9 @@ import com.google.common.collect.Iterables;
 
 public class DataNormalizer {
 
-    private final SchemaContext schemaContext;
-
     private final DataNormalizationOperation<?> operation;
 
     public DataNormalizer(final SchemaContext ctx) {
-        schemaContext = ctx;
         operation = DataNormalizationOperation.from(ctx);
     }
 
@@ -44,14 +43,18 @@ public class DataNormalizer {
         ImmutableList.Builder<PathArgument> normalizedArgs = ImmutableList.builder();
 
         DataNormalizationOperation<?> currentOp = operation;
-        for (PathArgument legacyArg : legacy.getPath()) {
+        Iterator<PathArgument> arguments = legacy.getPath().iterator();
+        while ( arguments.hasNext() ) {
+            PathArgument legacyArg = arguments.next();
             currentOp = currentOp.getChild(legacyArg);
             checkArgument(currentOp != null, "Legacy Instance Identifier %s is not correct. Normalized Instance Identifier so far %s",legacy,normalizedArgs.build());
             while (currentOp.isMixin()) {
                 normalizedArgs.add(currentOp.getIdentifier());
                 currentOp = currentOp.getChild(legacyArg.getNodeType());
             }
-            normalizedArgs.add(legacyArg);
+            if(arguments.hasNext() || (!currentOp.isKeyedEntry() || legacyArg instanceof NodeIdentifierWithPredicates || legacyArg instanceof NodeWithValue)) {
+                normalizedArgs.add(legacyArg);
+            }
         }
         return new InstanceIdentifier(normalizedArgs.build());
     }