Avoid module namespace/submodule name conflict when using lib sources
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / BuildGlobalContext.java
index 3e77b2cd4603935e460b46227fac5d65f1c9e4a6..156fbe8c7cedd5a181584083bfd03a9eb1b78466 100644 (file)
@@ -427,6 +427,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         final Set<SourceSpecificContext> requiredLibs = new HashSet<>();
         for (final SourceSpecificContext source : sources) {
             collectRequiredSourcesFromLib(libSourcesTable, requiredLibs, source);
+            removeConflictingLibSources(source, requiredLibs);
         }
         return requiredLibs;
     }
@@ -455,6 +456,22 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         return sourceMap != null && !sourceMap.isEmpty() ? sourceMap.get(sourceMap.lastKey()) : null;
     }
 
+    // removes required library sources which would cause namespace/name conflict with one of the main sources
+    // later in the parsing process. this can happen if we add a parent module or a submodule as a main source
+    // and the same parent module or submodule is added as one of the library sources.
+    // such situation may occur when using the yang-system-test artifact - if a parent module/submodule is specified
+    // as its argument and the same dir is specified as one of the library dirs through -p option).
+    private static void removeConflictingLibSources(final SourceSpecificContext source,
+            final Set<SourceSpecificContext> requiredLibs) {
+        final Iterator<SourceSpecificContext> requiredLibsIter = requiredLibs.iterator();
+        while (requiredLibsIter.hasNext()) {
+            final SourceSpecificContext currentReqSource = requiredLibsIter.next();
+            if (source.getRootIdentifier().equals(currentReqSource.getRootIdentifier())) {
+                requiredLibsIter.remove();
+            }
+        }
+    }
+
     private void endPhase(final ModelProcessingPhase phase) {
         Preconditions.checkState(currentPhase == phase);
         finishedPhase = currentPhase;