Add the parent module of a submodule to the set of required lib sources 33/62833/2
authorIgor Foltin <igor.foltin@pantheon.tech>
Thu, 7 Sep 2017 11:41:55 +0000 (13:41 +0200)
committerRobert Varga <nite@hq.sk>
Sat, 9 Sep 2017 06:00:25 +0000 (06:00 +0000)
This issue is related to the following use case of YANG statement parser:
If we add a YANG submodule as a main source and its parent module
as a library source, YANG statement parser does not add the parent module
to the set of requires lib sources. This causes an Exception as the parser
cannot resolve belongs-to statement in the submodule.

This patch fixes the issue.

Change-Id: Icbfddbbbecec310f0f7de17456e6a12dab8167d6
Signed-off-by: Igor Foltin <igor.foltin@pantheon.tech>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/BuildGlobalContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/BelongsToStatementImpl.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/YangInferencePipeline.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/stmt/Bug7480Test.java
yang/yang-parser-impl/src/test/resources/bugs/bug7480/files/sub-mod.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/bugs/bug7480/lib/parent-mod.yang [new file with mode: 0644]

index 1220ff775787e02a27bbad73f359a55b7b4be22f..3e77b2cd4603935e460b46227fac5d65f1c9e4a6 100644 (file)
@@ -445,8 +445,9 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
 
     private static SourceSpecificContext getRequiredLibSource(final ModuleIdentifier requiredModule,
             final TreeBasedTable<String, Date, SourceSpecificContext> libSourcesTable) {
-        return requiredModule.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP ? getLatestRevision(libSourcesTable
-                .row(requiredModule.getName())) : libSourcesTable.get(requiredModule.getName(),
+        return requiredModule.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP
+                || requiredModule.getRevision() == SimpleDateFormatUtil.DEFAULT_BELONGS_TO_DATE ? getLatestRevision(
+                libSourcesTable.row(requiredModule.getName())) : libSourcesTable.get(requiredModule.getName(),
                 requiredModule.getRevision());
     }
 
index d0ec8a6c786b27dc8e119aeb0ffe0183f54661af..8a138f36be4e4cf45bab0048729aa5552f5213fa 100644 (file)
@@ -66,6 +66,12 @@ public class BelongsToStatementImpl extends AbstractDeclaredStatement<String>
             return new BelongsToEffectiveStatementImpl(ctx);
         }
 
+        @Override
+        public void onPreLinkageDeclared(final StmtContext.Mutable<String, BelongsToStatement,
+                EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
+            belongsToCtx.addRequiredModule(getModuleIdentifier(belongsToCtx));
+        }
+
         @Override
         public void onLinkageDeclared(
                 final StmtContext.Mutable<String, BelongsToStatement, EffectiveStatement<String, BelongsToStatement>> belongsToCtx) {
index eb1f73889a99e177abb60122fb27830a99081fa5..7ea5ea888532819a7766e12bf2469503c4fa4a3b 100644 (file)
@@ -108,6 +108,7 @@ public final class YangInferencePipeline {
             .addVersionSpecificSupport(VERSION_1_1, new ImportStatementRfc7950Support())
             .addVersionSpecificSupport(VERSION_1, new IncludeStatementImpl.Definition())
             .addVersionSpecificSupport(VERSION_1_1, new IncludeStatementRfc7950Support())
+            .addSupport(new BelongsToStatementImpl.Definition())
             .addSupport(new PrefixStatementImpl.Definition())
             .addSupport(new YangVersionStatementImpl.Definition())
             .addSupport(new RevisionStatementImpl.Definition())
@@ -124,7 +125,6 @@ public final class YangInferencePipeline {
             .addSupport(new ReferenceStatementImpl.Definition())
             .addSupport(new ContactStatementImpl.Definition())
             .addSupport(new OrganizationStatementImpl.Definition())
-            .addSupport(new BelongsToStatementImpl.Definition())
             .addSupport(global(ModuleNamespace.class))
             .addSupport(global(ModuleNamespaceForBelongsTo.class))
             .addSupport(global(SubmoduleNamespace.class))
index 55e9f0453298f48c995fcf18d09af16afb046fb9..1c393dba8ea75b82c81a965a5923c395c708197e 100644 (file)
@@ -27,7 +27,7 @@ public class Bug7480Test {
         assertNotNull(context);
 
         final Set<Module> modules = context.getModules();
-        assertEquals(7, modules.size());
+        assertEquals(8, modules.size());
 
         assertNotNull(context.findModuleByNamespaceAndRevision(new URI("foo-imp"), SimpleDateFormatUtil
                 .getRevisionFormat().parse("2017-01-23")));
@@ -41,6 +41,11 @@ public class Bug7480Test {
         assertEquals(1, foo.size());
         final Set<Module> subFoos = foo.iterator().next().getSubmodules();
         assertEquals(1, subFoos.size());
+
+        final Module parentMod = context.findModuleByNamespaceAndRevision(new URI("parent-mod-ns"),
+                SimpleDateFormatUtil.getRevisionFormat().parse("2017-09-07"));
+        assertNotNull(parentMod);
+        assertEquals(1, parentMod.getSubmodules().size());
     }
 
     @Test
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug7480/files/sub-mod.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug7480/files/sub-mod.yang
new file mode 100644 (file)
index 0000000..3c7cce0
--- /dev/null
@@ -0,0 +1,11 @@
+submodule sub-mod {
+    belongs-to parent-mod {
+        prefix pm;
+    }
+
+    container my-container {
+        leaf my-leaf {
+            type string;
+        }
+    }
+}
\ No newline at end of file
diff --git a/yang/yang-parser-impl/src/test/resources/bugs/bug7480/lib/parent-mod.yang b/yang/yang-parser-impl/src/test/resources/bugs/bug7480/lib/parent-mod.yang
new file mode 100644 (file)
index 0000000..ef8ad8a
--- /dev/null
@@ -0,0 +1,10 @@
+module parent-mod {
+    namespace parent-mod-ns;
+    prefix pm;
+
+    include sub-mod;
+
+    revision 2017-09-07;
+
+    container my-parent-container {}
+}
\ No newline at end of file