Clarify NamespaceStorageSupport.getParentNamespaceStorage()
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / BuildGlobalContext.java
index 2d4dcf926a562714e059ee73000aae61300c57fa..fdec5deea86894fbce4ff061a61ced8b9455638d 100644 (file)
@@ -12,7 +12,6 @@ import static java.util.Objects.requireNonNull;
 
 import com.google.common.base.Verify;
 import com.google.common.collect.HashBasedTable;
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.ImmutableSetMultimap;
@@ -26,7 +25,6 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Set;
@@ -40,6 +38,7 @@ import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
@@ -54,7 +53,9 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules;
 import org.opendaylight.yangtools.yang.parser.spi.source.ModulesDeviatedByModules.SupportedModules;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
@@ -67,13 +68,16 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.SourceSpecificContext
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
+final class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
     private static final Logger LOG = LoggerFactory.getLogger(BuildGlobalContext.class);
 
-    private static final List<ModelProcessingPhase> PHASE_EXECUTION_ORDER =
-            ImmutableList.<ModelProcessingPhase>builder().add(ModelProcessingPhase.SOURCE_PRE_LINKAGE)
-            .add(ModelProcessingPhase.SOURCE_LINKAGE).add(ModelProcessingPhase.STATEMENT_DEFINITION)
-            .add(ModelProcessingPhase.FULL_DECLARATION).add(ModelProcessingPhase.EFFECTIVE_MODEL).build();
+    private static final ModelProcessingPhase[] PHASE_EXECUTION_ORDER = {
+        ModelProcessingPhase.SOURCE_PRE_LINKAGE,
+        ModelProcessingPhase.SOURCE_LINKAGE,
+        ModelProcessingPhase.STATEMENT_DEFINITION,
+        ModelProcessingPhase.FULL_DECLARATION,
+        ModelProcessingPhase.EFFECTIVE_MODEL
+    };
 
     private final Table<YangVersion, QName, StatementDefinitionContext<?, ?, ?>> definitions = HashBasedTable.create();
     private final Map<QName, StatementDefinitionContext<?, ?, ?>> modelDefinedStmtDefs = new HashMap<>();
@@ -104,9 +108,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
                 throw new IllegalArgumentException("Unhandled parser mode " + statementParserMode);
         }
 
-        for (final Entry<ValidationBundleType, Collection<?>> validationBundle : supportedValidation.entrySet()) {
-            addToNs(ValidationBundlesNamespace.class, validationBundle.getKey(), validationBundle.getValue());
-        }
+        addToNamespace(ValidationBundlesNamespace.class, supportedValidation);
 
         this.supportedVersions = ImmutableSet.copyOf(supports.get(ModelProcessingPhase.INIT).getSupportedVersions());
     }
@@ -132,12 +134,12 @@ class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
     }
 
     void setSupportedFeatures(final Set<QName> supportedFeatures) {
-        addToNs(SupportedFeaturesNamespace.class, SupportedFeatures.SUPPORTED_FEATURES,
+        addToNamespace(SupportedFeaturesNamespace.class, SupportedFeatures.SUPPORTED_FEATURES,
                     ImmutableSet.copyOf(supportedFeatures));
     }
 
     void setModulesDeviatedByModules(final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
-        addToNs(ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES,
+        addToNamespace(ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES,
                     ImmutableSetMultimap.copyOf(modulesDeviatedByModules));
     }
 
@@ -243,7 +245,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
 
     private SomeModifiersUnresolvedException propagateException(final SourceSpecificContext source,
             final RuntimeException cause) throws SomeModifiersUnresolvedException {
-        final SourceIdentifier sourceId = StmtContextUtils.createSourceIdentifier(source.getRoot());
+        final SourceIdentifier sourceId = createSourceIdentifier(source.getRoot());
         if (!(cause instanceof SourceException)) {
             /*
              * This should not be happening as all our processing should provide SourceExceptions.
@@ -257,6 +259,18 @@ class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
         throw new SomeModifiersUnresolvedException(currentPhase, sourceId, cause);
     }
 
+    private static SourceIdentifier createSourceIdentifier(final StmtContext<?, ?, ?> root) {
+        final QNameModule qNameModule = root.getFromNamespace(ModuleCtxToModuleQName.class, root);
+        final String arg = root.coerceRawStatementArgument();
+        if (qNameModule != null) {
+            // creates SourceIdentifier for a module
+            return RevisionSourceIdentifier.create(arg, qNameModule.getRevision());
+        }
+
+        // creates SourceIdentifier for a submodule
+        return RevisionSourceIdentifier.create(arg, StmtContextUtils.getLatestRevision(root.declaredSubstatements()));
+    }
+
     @SuppressWarnings("checkstyle:illegalCatch")
     private EffectiveSchemaContext transformEffective() throws ReactorException {
         checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
@@ -345,7 +359,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements Registry {
 
             if (!addedCause) {
                 addedCause = true;
-                final SourceIdentifier sourceId = StmtContextUtils.createSourceIdentifier(failedSource.getRoot());
+                final SourceIdentifier sourceId = createSourceIdentifier(failedSource.getRoot());
                 buildFailure = new SomeModifiersUnresolvedException(currentPhase, sourceId, sourceEx);
             } else {
                 buildFailure.addSuppressed(sourceEx);