Bug 5437: Issue accessing mounted device supporting OpenConfig BGP. 01/35601/1
authorPeter Kajsa <pkajsa@cisco.com>
Tue, 1 Mar 2016 09:52:16 +0000 (10:52 +0100)
committerPeter Kajsa <pkajsa@cisco.com>
Wed, 2 Mar 2016 07:28:27 +0000 (08:28 +0100)
Yangtools evaluated a relative path of leafref in the incorrect context.
Relative path of leafref should be evaluated in the final context of leafref node.

Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
Change-Id: I24821d1b12d4456058e0ec1049542972fe29b903

yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5437.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug5437/foo.yang [new file with mode: 0644]

index 7ad5070f195a118faf8590d75e3671208029c578..2352eedc8a32fe076d9e345e6ddc028d12c9ea44 100644 (file)
@@ -630,23 +630,25 @@ public final class SchemaContextUtil {
         RevisionAwareXPath pathStatement = typeDefinition.getPathStatement();
         pathStatement = new RevisionAwareXPathImpl(stripConditionsFromXPathString(pathStatement), pathStatement.isAbsolute());
 
-        SchemaNode baseSchema = schema;
-        while (baseSchema instanceof DerivableSchemaNode) {
-            final Optional<? extends SchemaNode> basePotential = ((DerivableSchemaNode) baseSchema).getOriginal();
-            if (basePotential.isPresent()) {
-                baseSchema = basePotential.get();
-            } else {
-                break;
+        final DataSchemaNode dataSchemaNode;
+        if (pathStatement.isAbsolute()) {
+            SchemaNode baseSchema = schema;
+            while (baseSchema instanceof DerivableSchemaNode) {
+                final Optional<? extends SchemaNode> basePotential = ((DerivableSchemaNode) baseSchema).getOriginal();
+                if (basePotential.isPresent()) {
+                    baseSchema = basePotential.get();
+                } else {
+                    break;
+                }
             }
-        }
 
-        Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema);
-
-        final DataSchemaNode dataSchemaNode;
-        if(pathStatement.isAbsolute()) {
-            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule, pathStatement);
+            Module parentModule = findParentModuleOfReferencingType(schemaContext, baseSchema);
+            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNode(schemaContext, parentModule,
+                    pathStatement);
         } else {
-            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext, parentModule, baseSchema, pathStatement);
+            Module parentModule = findParentModule(schemaContext, schema);
+            dataSchemaNode = (DataSchemaNode) SchemaContextUtil.findDataSchemaNodeForRelativeXPath(schemaContext,
+                    parentModule, schema, pathStatement);
         }
 
         // FIXME this is just to preserve backwards compatibility since yangtools do not mind wrong leafref xpaths
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5437.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5437.java
new file mode 100644 (file)
index 0000000..d912aa8
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.stmt.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+
+public class Bug5437 {
+    private static final String NS = "foo";
+    private static final String REV = "2016-03-01";
+
+    @Test
+    public void test() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+        SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5437");
+        assertNotNull(context);
+
+        QName root = QName.create(NS, REV, "root");
+        QName leafRef2 = QName.create(NS, REV, "leaf-ref-2");
+        QName conGrp = QName.create(NS, REV, "con-grp");
+        QName leafRef = QName.create(NS, REV, "leaf-ref");
+
+        SchemaPath leafRefPath = SchemaPath.create(true, root, conGrp, leafRef);
+        SchemaPath leafRef2Path = SchemaPath.create(true, root, leafRef2);
+        SchemaNode findDataSchemaNode = SchemaContextUtil.findDataSchemaNode(context, leafRefPath);
+        SchemaNode findDataSchemaNode2 = SchemaContextUtil.findDataSchemaNode(context, leafRef2Path);
+        assertTrue(findDataSchemaNode instanceof LeafSchemaNode);
+        assertTrue(findDataSchemaNode2 instanceof LeafSchemaNode);
+        LeafSchemaNode leafRefNode = (LeafSchemaNode) findDataSchemaNode;
+        LeafSchemaNode leafRefNode2 = (LeafSchemaNode) findDataSchemaNode2;
+
+        assertTrue(leafRefNode.getType() instanceof LeafrefTypeDefinition);
+        assertTrue(leafRefNode2.getType() instanceof LeafrefTypeDefinition);
+
+        TypeDefinition<?> baseTypeForLeafRef = SchemaContextUtil.getBaseTypeForLeafRef(
+                (LeafrefTypeDefinition) leafRefNode.getType(), context, leafRefNode);
+        TypeDefinition<?> baseTypeForLeafRef2 = SchemaContextUtil.getBaseTypeForLeafRef(
+                (LeafrefTypeDefinition) leafRefNode2.getType(), context, leafRefNode2);
+
+        assertTrue(baseTypeForLeafRef instanceof BinaryTypeDefinition);
+        assertTrue(baseTypeForLeafRef2 instanceof IntegerTypeDefinition);
+    }
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug5437/foo.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug5437/foo.yang
new file mode 100644 (file)
index 0000000..9d30edc
--- /dev/null
@@ -0,0 +1,47 @@
+module foo {
+    namespace "foo";
+    prefix foo;
+    yang-version 1;
+
+    revision "2016-03-01" {
+        description "test";
+    }
+
+    grouping grp {
+        container con-grp {
+            leaf l {
+                type int16;
+            }
+            leaf leaf-ref {
+                type leafref {
+                    path "../../con2/l2";
+                }
+            }
+        }
+    }
+
+    container root {
+        uses grp;
+        container con2 {
+            leaf l2 {
+                type binary;
+            }
+        }
+    }
+
+    augment "/root" {
+        leaf leaf-ref-2 {
+            type leaf-ref-type2;
+        }
+    }
+
+    typedef leaf-ref-type2 {
+        type leaf-ref-type;
+    }
+
+    typedef leaf-ref-type {
+        type leafref {
+            path "../con-grp/l";
+        }
+    }
+}