Bug 8713 - BGP models not compatible with leafref context 90/62890/2
authorPeter Kajsa <pkajsa@cisco.com>
Thu, 7 Sep 2017 11:18:22 +0000 (13:18 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 27 Sep 2017 08:22:11 +0000 (08:22 +0000)
Lookup of parent module should be performed based on the first path
argument from root not the last one.

Change-Id: Ic8e0dc901056caf93e4fc5266334434bd067d9ab
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
(cherry picked from commit f1193617e74d40724cc53df1f5a4f007dc135f2b)

yang/yang-data-impl/src/main/java/org/opendaylight/yangtools/yang/data/impl/leafref/LeafRefContextBuilder.java
yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/Bug8713Test.java [new file with mode: 0644]
yang/yang-data-impl/src/test/resources/bug8713/bar.yang [new file with mode: 0644]
yang/yang-data-impl/src/test/resources/bug8713/foo.yang [new file with mode: 0644]

index b9899f17366b92a906f8f8e38112de0b1f9290b6..ea609045c36048c24c49ccd847b6377f610a95b2 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.yangtools.yang.data.impl.leafref;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -158,10 +159,10 @@ class LeafRefContextBuilder {
     }
 
     public Module getLeafRefContextModule() {
-        final QNameModule qnameModule = currentNodeQName.getModule();
+        final Iterator<QName> it = currentNodePath.getPathFromRoot().iterator();
+        final QNameModule qnameModule = it.hasNext() ? it.next().getModule() : currentNodeQName.getModule();
 
-        return schemaContext.findModuleByNamespaceAndRevision(
-                qnameModule.getNamespace(), qnameModule.getRevision());
+        return schemaContext.findModuleByNamespaceAndRevision(qnameModule.getNamespace(), qnameModule.getRevision());
     }
 
     public void addReferencedByLeafRefCtx(final QName qname, final LeafRefContext leafRef) {
diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/Bug8713Test.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/leafref/context/Bug8713Test.java
new file mode 100644 (file)
index 0000000..e32335f
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2016 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.data.impl.leafref.context;
+
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.TipProducingDataTree;
+import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefContext;
+import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefValidatation;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Bug8713Test {
+    private static final String FOO_NS = "foo";
+    private static final String BAR_NS = "bar";
+    private static final String REV = "2017-09-06";
+
+    @Test
+    public void dataTreeCanditateValidationTest() throws Exception {
+        final SchemaContext context = YangParserTestUtils.parseYangSources("/bug8713/");
+        final LeafRefContext rootLeafRefContext = LeafRefContext.create(context);
+        final TipProducingDataTree inMemoryDataTree = InMemoryDataTreeFactory.getInstance()
+                .create(DataTreeConfiguration.DEFAULT_OPERATIONAL);
+        inMemoryDataTree.setSchemaContext(context);
+
+        final ContainerNode root = createRootContainer();
+        final YangInstanceIdentifier rootPath = YangInstanceIdentifier.of(foo("root"));
+        final DataTreeModification writeModification = inMemoryDataTree.takeSnapshot().newModification();
+        writeModification.write(rootPath, root);
+        writeModification.ready();
+
+        final DataTreeCandidate writeContributorsCandidate = inMemoryDataTree.prepare(writeModification);
+
+        LeafRefValidatation.validate(writeContributorsCandidate, rootLeafRefContext);
+        inMemoryDataTree.commit(writeContributorsCandidate);
+    }
+
+    private ContainerNode createRootContainer() {
+        return Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(foo("root")))
+                .withChild(ImmutableNodes.leafNode(bar("target"), "target value"))
+                .withChild(ImmutableNodes.leafNode(bar("ref"), "target value")).build();
+    }
+
+    private static QName foo(final String localName) {
+        return QName.create(FOO_NS, REV, localName);
+    }
+
+    private static QName bar(final String localName) {
+        return QName.create(BAR_NS, REV, localName);
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-data-impl/src/test/resources/bug8713/bar.yang b/yang/yang-data-impl/src/test/resources/bug8713/bar.yang
new file mode 100644 (file)
index 0000000..883ad63
--- /dev/null
@@ -0,0 +1,19 @@
+module bar {
+    namespace bar;
+    prefix bar;
+
+    import foo { prefix foo; revision-date 2017-09-06; }
+
+    revision 2017-09-06;
+
+    augment "/foo:root" {
+        leaf ref {
+            type leafref {
+                path "../target" ;
+            }
+        }
+        leaf target {
+            type string;
+        }
+    }
+}
diff --git a/yang/yang-data-impl/src/test/resources/bug8713/foo.yang b/yang/yang-data-impl/src/test/resources/bug8713/foo.yang
new file mode 100644 (file)
index 0000000..bb65c44
--- /dev/null
@@ -0,0 +1,9 @@
+module foo {
+    namespace foo;
+    prefix foo;
+
+    revision 2017-09-06;
+
+    container root {
+    }
+}