Promote SchemaSourceRepresentation
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SourceSpecificContext.java
index b302b67cd7bf395473b489962d98a617edd8ba01..1e53ea4afd47f05b684057390283964fb96156cb 100644 (file)
@@ -27,55 +27,78 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.concepts.Mutable;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
+import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
 import org.opendaylight.yangtools.yang.common.YangVersion;
-import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+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.StatementSourceReference;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.ParserNamespaces;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
-import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
-import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
-import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
+import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceStorage;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementDefinitions;
 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.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.source.PrefixResolver;
 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinition;
 import org.opendaylight.yangtools.yang.parser.spi.source.QNameToStatementDefinitionMap;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
-import org.opendaylight.yangtools.yang.parser.spi.source.SourceParserNamespaces;
-import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable {
+final class SourceSpecificContext implements NamespaceStorage, Mutable {
     enum PhaseCompletionProgress {
         NO_PROGRESS,
         PROGRESS,
         FINISHED
     }
 
-    private static final class SupportedStatements extends NamespaceBehaviour<QName, StatementSupport<?, ?, ?>> {
+    private static final class SupportedStatements extends NamespaceAccess<QName, StatementSupport<?, ?, ?>> {
         private final QNameToStatementDefinitionMap statementDefinitions;
 
         SupportedStatements(final QNameToStatementDefinitionMap statementDefinitions) {
-            super(StatementSupport.NAMESPACE);
             this.statementDefinitions = requireNonNull(statementDefinitions);
         }
 
         @Override
-        public StatementSupport<?, ?, ?> getFrom(final NamespaceStorageNode storage, final QName key) {
+        ParserNamespace<QName, StatementSupport<?, ?, ?>> namespace() {
+            return StatementSupport.NAMESPACE;
+        }
+
+        @Override
+        StatementSupport<?, ?, ?> valueFrom(final NamespaceStorage storage, final QName key) {
             return statementDefinitions.getSupport(key);
         }
 
         @Override
-        public Map<QName, StatementSupport<?, ?, ?>> getAllFrom(final NamespaceStorageNode storage) {
+        void valueTo(final NamespaceStorage storage, final QName key, final StatementSupport<?, ?, ?> value) {
+            throw uoe();
+        }
+
+        @Override
+        Map<QName, StatementSupport<?, ?, ?>> allFrom(final NamespaceStorage storage) {
+            throw uoe();
+        }
+
+        @Override
+        Entry<QName, StatementSupport<?, ?, ?>> entryFrom(final NamespaceStorage storage,
+                final NamespaceKeyCriterion<QName> criterion) {
+            throw uoe();
+        }
+
+        @Override
+        void addListener(final QName key, final KeyedValueAddedListener<QName, StatementSupport<?, ?, ?>> listener) {
             throw uoe();
         }
 
         @Override
-        public void addTo(final NamespaceStorageNode storage, final QName key, final StatementSupport<?, ?, ?> value) {
+        void addListener(final PredicateValueAddedListener<QName, StatementSupport<?, ?, ?>> listener) {
             throw uoe();
         }
 
@@ -89,7 +112,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     // TODO: consider keying by Byte equivalent of ExecutionOrder
     private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
     private final QNameToStatementDefinitionMap qnameToStmtDefMap = new QNameToStatementDefinitionMap();
-    private final SupportedStatements statementSupports = new SupportedStatements(qnameToStmtDefMap);
+    private final @NonNull SupportedStatements statementSupports = new SupportedStatements(qnameToStmtDefMap);
     private final HashMapPrefixResolver prefixToModuleMap = new HashMapPrefixResolver();
     private final @NonNull BuildGlobalContext globalContext;
 
@@ -180,8 +203,28 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         return root;
     }
 
-    RootStatementContext<?, ?, ?> getRoot() {
-        return root;
+    @NonNull SourceIdentifier identifySource() {
+        final var arg = root.getArgument();
+        verify(arg instanceof Unqualified, "Unexpected argument %s", arg);
+        final var unqualified = (Unqualified) arg;
+
+        final var module = root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, root);
+        if (module != null) {
+            // creates SourceIdentifier for a module
+            return new SourceIdentifier(unqualified, module.getRevision().orElse(null));
+        }
+
+        // creates SourceIdentifier for a submodule
+        return new SourceIdentifier(unqualified,
+            StmtContextUtils.getLatestRevision(root.declaredSubstatements()).orElse(null));
+    }
+
+    @NonNull DeclaredStatement<?> declaredRoot() {
+        return root.declared();
+    }
+
+    @NonNull EffectiveStatement<?, ?> effectiveRoot() {
+        return root.buildEffective();
     }
 
     /**
@@ -189,7 +232,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
      *
      * @return version of root statement context
      */
-    YangVersion getRootVersion() {
+    private YangVersion getRootVersion() {
         return root != null ? root.yangVersion() : RootStatementContext.DEFAULT_VERSION;
     }
 
@@ -208,8 +251,8 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     private void updateImportedNamespaces(final ParserNamespace<?, ?> type, final Object value) {
-        if (SourceParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX.equals(type)
-            || SourceParserNamespaces.IMPORTED_MODULE.equals(type)) {
+        if (ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX.equals(type)
+            || ParserNamespaces.IMPORTED_MODULE.equals(type)) {
             verify(value instanceof RootStatementContext, "Unexpected imported value %s", value);
 
             if (importedNamespaces.isEmpty()) {
@@ -222,7 +265,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     @Override
     public <K, V> V putToLocalStorage(final ParserNamespace<K, V> type, final K key, final V value) {
         // RootStatementContext takes care of IncludedModuleContext and the rest...
-        final V ret = getRoot().putToLocalStorage(type, key, value);
+        final V ret = root.putToLocalStorage(type, key, value);
         // FIXME: what about duplicates?
         updateImportedNamespaces(type, value);
         return ret;
@@ -231,7 +274,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     @Override
     public <K, V> V putToLocalStorageIfAbsent(final ParserNamespace<K, V> type, final K key, final V value) {
         // RootStatementContext takes care of IncludedModuleContext and the rest...
-        final V ret = getRoot().putToLocalStorageIfAbsent(type, key, value);
+        final V ret = root.putToLocalStorageIfAbsent(type, key, value);
         if (ret == null) {
             updateImportedNamespaces(type, value);
         }
@@ -239,18 +282,18 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     @Override
-    public StorageNodeType getStorageNodeType() {
-        return StorageNodeType.SOURCE_LOCAL_SPECIAL;
+    public StorageType getStorageType() {
+        return StorageType.SOURCE_LOCAL_SPECIAL;
     }
 
     @Override
     public <K, V> V getFromLocalStorage(final ParserNamespace<K, V> type, final K key) {
-        final V potentialLocal = getRoot().getFromLocalStorage(type, key);
+        final V potentialLocal = root.getFromLocalStorage(type, key);
         if (potentialLocal != null) {
             return potentialLocal;
         }
 
-        for (final NamespaceStorageNode importedSource : importedNamespaces) {
+        for (final NamespaceStorage importedSource : importedNamespaces) {
             final V potential = importedSource.getFromLocalStorage(type, key);
             if (potential != null) {
                 return potential;
@@ -261,12 +304,12 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
 
     @Override
     public <K, V> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type) {
-        final Map<K, V> potentialLocal = getRoot().getAllFromLocalStorage(type);
+        final Map<K, V> potentialLocal = root.getAllFromLocalStorage(type);
         if (potentialLocal != null) {
             return potentialLocal;
         }
 
-        for (final NamespaceStorageNode importedSource : importedNamespaces) {
+        for (final NamespaceStorage importedSource : importedNamespaces) {
             final Map<K, V> potential = importedSource.getAllFromLocalStorage(type);
 
             if (potential != null) {
@@ -276,17 +319,17 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         return null;
     }
 
-    @Override
-    @SuppressWarnings("unchecked")
-    public <K, V> NamespaceBehaviour<K, V> getNamespaceBehaviour(final ParserNamespace<K, V> type) {
+    <K, V> NamespaceAccess<K, V> accessNamespace(final ParserNamespace<K, V> type) {
         if (StatementSupport.NAMESPACE.equals(type)) {
-            return (NamespaceBehaviour<K, V>) statementSupports;
+            @SuppressWarnings("unchecked")
+            final var ret = (NamespaceAccess<K, V>) statementSupports;
+            return ret;
         }
-        return globalContext.getNamespaceBehaviour(type);
+        return globalContext.accessNamespace(type);
     }
 
     @Override
-    public BuildGlobalContext getParentNamespaceStorage() {
+    public GlobalStorage getParentStorage() {
         return globalContext;
     }
 
@@ -416,7 +459,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
 
     private PrefixResolver preLinkagePrefixes() {
         final HashMapPrefixResolver preLinkagePrefixes = new HashMapPrefixResolver();
-        final var prefixToNamespaceMap = getAllFromLocalStorage(SourceParserNamespaces.IMP_PREFIX_TO_NAMESPACE);
+        final var prefixToNamespaceMap = getAllFromLocalStorage(ParserNamespaces.IMP_PREFIX_TO_NAMESPACE);
         if (prefixToNamespaceMap == null) {
             //:FIXME if it is a submodule without any import, the map is null. Handle also submodules and includes...
             return null;
@@ -427,16 +470,16 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     private PrefixResolver prefixes() {
-        final var allImports = root.getAllFromNamespace(SourceParserNamespaces.IMPORT_PREFIX_TO_MODULECTX);
+        final var allImports = root.namespace(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX);
         if (allImports != null) {
             allImports.forEach((key, value) ->
-                prefixToModuleMap.put(key, root.getFromNamespace(SourceParserNamespaces.MODULECTX_TO_QNAME, value)));
+                prefixToModuleMap.put(key, root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, value)));
         }
 
-        final var allBelongsTo = root.getAllFromNamespace(SourceParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
+        final var allBelongsTo = root.namespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
         if (allBelongsTo != null) {
             allBelongsTo.forEach((key, value) ->
-                prefixToModuleMap.put(key, root.getFromNamespace(SourceParserNamespaces.MODULECTX_TO_QNAME, value)));
+                prefixToModuleMap.put(key, root.namespaceItem(ParserNamespaces.MODULECTX_TO_QNAME, value)));
         }
 
         return prefixToModuleMap;