Make sure SmtmtNamespaceContext uses belongs-to prefix 58/94158/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Dec 2020 11:48:16 +0000 (12:48 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 10 Dec 2020 13:00:48 +0000 (14:00 +0100)
For xpath parsing we nee to make sure we take into account the
prefix under which 'belongs-to' module is known to the submodule.

JIRA: YANGTOOLS-1201
Change-Id: Id38d76443d23eecef09f1f927c44218363a3a1d1
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit bb9c8c8751e338d2e683063144d1d5810b22eee7)

yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/namespace/StmtNamespaceContext.java
yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1201Test.java [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/bar.yang [new file with mode: 0644]
yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/foo.yang [new file with mode: 0644]

index 38bd6c144a01cd4342b90e6076c4fa3f58ee6d70..bbf8043be15cbcd5b01d390784581314ff36f656 100644 (file)
@@ -39,9 +39,9 @@ final class StmtNamespaceContext implements YangNamespaceContext {
         this.moduleToPrefix = qnameToPrefix == null ? ImmutableBiMap.of() : ImmutableBiMap.copyOf(qnameToPrefix);
 
         // Additional mappings
+        final Map<String, QNameModule> additional = new HashMap<>();
         final Map<String, StmtContext<?, ?, ?>> imports = ctx.getAllFromNamespace(ImportPrefixToModuleCtx.class);
         if (imports != null) {
-            final Map<String, QNameModule> additional = new HashMap<>();
             for (Entry<String, StmtContext<?, ?, ?>> entry : imports.entrySet()) {
                 if (!moduleToPrefix.containsValue(entry.getKey())) {
                     QNameModule qnameModule = ctx.getFromNamespace(ModuleCtxToModuleQName.class, entry.getValue());
@@ -56,10 +56,20 @@ final class StmtNamespaceContext implements YangNamespaceContext {
                     }
                 }
             }
-            this.prefixToModule = ImmutableMap.copyOf(additional);
-        } else {
-            this.prefixToModule = ImmutableMap.of();
         }
+        if (ctx.producesDeclared(SubmoduleStatement.class)) {
+            final Map<String, String> belongsTo = ctx.getAllFromNamespace(BelongsToPrefixToModuleName.class);
+            if (belongsTo != null) {
+                for (Entry<String, String> entry : belongsTo.entrySet()) {
+                    final QNameModule module = ctx.getFromNamespace(ModuleNameToModuleQName.class, entry.getValue());
+                    if (module != null && !additional.containsKey(entry.getKey())) {
+                        additional.put(entry.getKey(), module);
+                    }
+                }
+            }
+        }
+
+        this.prefixToModule = ImmutableMap.copyOf(additional);
     }
 
     @Override
diff --git a/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1201Test.java b/yang/yang-parser-rfc7950/src/test/java/org/opendaylight/yangtools/yang/stmt/YT1201Test.java
new file mode 100644 (file)
index 0000000..08f9938
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.xpath.api.YangExpr;
+import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.Relative;
+import org.opendaylight.yangtools.yang.xpath.api.YangXPathAxis;
+
+public class YT1201Test {
+    private static final QName FOO = QName.create("foo", "foo");
+    private static final QName BAR = QName.create("foo", "bar");
+
+    @Test
+    public void testWhenPrefixes() throws Exception {
+        final DataSchemaNode bar = StmtTestUtils.parseYangSources("/bugs/YT1201/").getDataChildByName(BAR);
+        assertThat(bar, instanceOf(ContainerSchemaNode.class));
+        final YangExpr when = ((ContainerSchemaNode) bar).getWhenCondition().get().getRootExpr();
+        assertThat(when, instanceOf(Relative.class));
+        assertEquals(List.of(YangXPathAxis.CHILD.asStep(FOO)), ((Relative) when).getSteps());
+    }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/bar.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/bar.yang
new file mode 100644 (file)
index 0000000..9212343
--- /dev/null
@@ -0,0 +1,9 @@
+submodule bar {
+  belongs-to foo {
+    prefix foo;
+  }
+
+  container bar {
+    when foo:foo;
+  }
+}
diff --git a/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/foo.yang b/yang/yang-parser-rfc7950/src/test/resources/bugs/YT1201/foo.yang
new file mode 100644 (file)
index 0000000..f1702af
--- /dev/null
@@ -0,0 +1,11 @@
+module foo {
+  namespace foo;
+  prefix foo;
+
+  include bar;
+
+  leaf foo {
+    type string;
+  }
+}
+