From 4ad3612cb95e3aa811ddd5d1aa76bc93052bf7cd Mon Sep 17 00:00:00 2001 From: Peter Kajsa Date: Tue, 1 Mar 2016 10:52:16 +0100 Subject: [PATCH] Bug 5437: Issue accessing mounted device supporting OpenConfig BGP. 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 Change-Id: I24821d1b12d4456058e0ec1049542972fe29b903 --- .../yang/model/util/SchemaContextUtil.java | 30 ++++----- .../yangtools/yang/stmt/test/Bug5437.java | 63 +++++++++++++++++++ .../src/test/resources/bugs/bug5437/foo.yang | 47 ++++++++++++++ 3 files changed, 126 insertions(+), 14 deletions(-) create mode 100644 yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5437.java create mode 100644 yang/yang-parser-impl/src/test/resources/bugs/bug5437/foo.yang diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java index 7ad5070f19..2352eedc8a 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/SchemaContextUtil.java @@ -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 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 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 index 0000000000..d912aa8696 --- /dev/null +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/test/Bug5437.java @@ -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 index 0000000000..9d30edc78a --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/bugs/bug5437/foo.yang @@ -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"; + } + } +} -- 2.36.6