Bug 9005 - scope of model import prefix should be module/submodule 60/62260/1
authorPeter Kajsa <pkajsa@cisco.com>
Fri, 18 Aug 2017 10:24:32 +0000 (12:24 +0200)
committerRobert Varga <nite@hq.sk>
Thu, 24 Aug 2017 10:31:33 +0000 (10:31 +0000)
Yang parser includes all substatements of a submodule into a parent
module including all import statements of the submodule, what causes
mismatch of parent module imports.

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

yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/AbstractEffectiveModule.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug9005Test.java [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-1@2000-01-01.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-2@2000-01-02.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug9005/foo@2017-07-07.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug9005/sub-foo@2017-07-07.yang [new file with mode: 0644]

index d80ea60cbf2676af109d0b900f8e047f031704ff..5944f98cc149f0328a104768ce85533242b41ed0 100644 (file)
@@ -22,11 +22,13 @@ import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.concepts.SemVer;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.Deviation;
 import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
@@ -38,6 +40,7 @@ import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.UsesNode;
@@ -142,7 +145,9 @@ abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> exte
                 final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
                         .buildEffective();
                 submodulesInit.add(submodule);
-                substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
+                substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
+                        .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
+                        .collect(Collectors.toList()));
             }
 
             this.submodules = ImmutableSet.copyOf(submodulesInit);
diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug9005Test.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug9005Test.java
new file mode 100644 (file)
index 0000000..d381d02
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2017 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Set;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.api.ModuleImport;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class Bug9005Test {
+    @Test
+    public void test() throws Exception {
+        final SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug9005");
+        assertNotNull(context);
+
+        final Module foo = context.findModuleByName("foo",
+                SimpleDateFormatUtil.getRevisionFormat().parse("2017-07-07"));
+
+        final Set<ModuleImport> imports = foo.getImports();
+        assertEquals(1, imports.size());
+        final ModuleImport imp1 = imports.iterator().next();
+        assertEquals("bar-2", imp1.getModuleName());
+        assertEquals("bar", imp1.getPrefix());
+        assertEquals(SimpleDateFormatUtil.getRevisionFormat().parse("2000-01-02"), imp1.getRevision());
+
+        final Set<Module> submodules = foo.getSubmodules();
+        assertEquals(1, submodules.size());
+        final Module submodule = submodules.iterator().next();
+        final Set<ModuleImport> subImports = submodule.getImports();
+
+        assertEquals(1, subImports.size());
+        final ModuleImport subImp1 = subImports.iterator().next();
+        assertEquals("bar-1", subImp1.getModuleName());
+        assertEquals("bar", subImp1.getPrefix());
+        assertEquals(SimpleDateFormatUtil.getRevisionFormat().parse("2000-01-01"), subImp1.getRevision());
+    }
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-1@2000-01-01.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-1@2000-01-01.yang
new file mode 100644 (file)
index 0000000..79e7d8b
--- /dev/null
@@ -0,0 +1,6 @@
+module bar-1 {
+    namespace bar-1;
+    prefix bar;
+
+    revision 2000-01-01;
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-2@2000-01-02.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug9005/bar-2@2000-01-02.yang
new file mode 100644 (file)
index 0000000..cd3ff10
--- /dev/null
@@ -0,0 +1,6 @@
+module bar-2 {
+    namespace bar-2;
+    prefix bar;
+
+    revision 2000-01-02;
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug9005/foo@2017-07-07.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug9005/foo@2017-07-07.yang
new file mode 100644 (file)
index 0000000..6ab063a
--- /dev/null
@@ -0,0 +1,10 @@
+module foo {
+    namespace foo;
+    prefix foo;
+
+    import bar-2 { prefix bar; }
+
+    include sub-foo;
+
+    revision 2017-07-07;
+}
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug9005/sub-foo@2017-07-07.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug9005/sub-foo@2017-07-07.yang
new file mode 100644 (file)
index 0000000..5214745
--- /dev/null
@@ -0,0 +1,9 @@
+submodule sub-foo {
+    belongs-to foo {
+        prefix foo;
+    }
+
+    import bar-1 { prefix bar; }
+
+    revision 2017-07-07;
+}