Seal PathArgumentList
[yangtools.git] / parser / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SourceSpecificContext.java
index d34b4e19af1e6e313f630a9aa25da07f97ebeb97..33e371cfe157d113eb4f222f0761b92a96622638 100644 (file)
@@ -27,9 +27,9 @@ 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.XMLNamespace;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.repo.api.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;
@@ -37,19 +37,10 @@ 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.ParserNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StatementDefinitionNamespace;
+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.StatementSupportNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToModuleContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleCtx;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx;
-import org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext;
-import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleQName;
-import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule;
-import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModuleMap;
+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;
@@ -65,28 +56,31 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         FINISHED
     }
 
-    private static final class SupportedStatements
-            extends NamespaceBehaviour<QName, StatementSupport<?, ?, ?>, StatementSupportNamespace> {
+    private static final class SupportedStatements extends NamespaceBehaviour<QName, StatementSupport<?, ?, ?>> {
         private final QNameToStatementDefinitionMap statementDefinitions;
 
         SupportedStatements(final QNameToStatementDefinitionMap statementDefinitions) {
-            super(StatementSupportNamespace.class);
+            super(StatementSupport.NAMESPACE);
             this.statementDefinitions = requireNonNull(statementDefinitions);
         }
 
         @Override
         public StatementSupport<?, ?, ?> getFrom(final NamespaceStorageNode storage, final QName key) {
-            return statementDefinitions.get(key);
+            return statementDefinitions.getSupport(key);
         }
 
         @Override
         public Map<QName, StatementSupport<?, ?, ?>> getAllFrom(final NamespaceStorageNode storage) {
-            throw new UnsupportedOperationException("StatementSupportNamespace is immutable");
+            throw uoe();
         }
 
         @Override
         public void addTo(final NamespaceStorageNode storage, final QName key, final StatementSupport<?, ?, ?> value) {
-            throw new UnsupportedOperationException("StatementSupportNamespace is immutable");
+            throw uoe();
+        }
+
+        private static UnsupportedOperationException uoe() {
+            return new UnsupportedOperationException("StatementSupportNamespace is immutable");
         }
     }
 
@@ -96,7 +90,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
     private final QNameToStatementDefinitionMap qnameToStmtDefMap = new QNameToStatementDefinitionMap();
     private final SupportedStatements statementSupports = new SupportedStatements(qnameToStmtDefMap);
-    private final PrefixToModuleMap prefixToModuleMap = new PrefixToModuleMap();
+    private final HashMapPrefixResolver prefixToModuleMap = new HashMapPrefixResolver();
     private final @NonNull BuildGlobalContext globalContext;
 
     // Freed as soon as we complete ModelProcessingPhase.EFFECTIVE_MODEL
@@ -107,7 +101,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
      * - modules imported via 'import' statement
      * - parent module, declared via 'belongs-to' statement
      */
-    private Collection<RootStatementContext<?, ?, ?>> importedNamespaces = ImmutableList.of();
+    private List<RootStatementContext<?, ?, ?>> importedNamespaces = ImmutableList.of();
     private RootStatementContext<?, ?, ?> root;
     // TODO: consider using ExecutionOrder byte for these two
     private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
@@ -135,7 +129,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         if (def == null) {
             def = globalContext.getModelDefinedStatementDefinition(name);
             if (def == null) {
-                final StatementSupport<?, ?, ?> extension = qnameToStmtDefMap.get(name);
+                final StatementSupport<?, ?, ?> extension = qnameToStmtDefMap.getSupport(name);
                 if (extension != null) {
                     def = new StatementDefinitionContext<>(extension);
                     globalContext.putModelDefinedStatementDefinition(name, def);
@@ -213,20 +207,20 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         LOG.debug("Source {} started phase {}", source, phase);
     }
 
-    private void updateImportedNamespaces(final Class<?> type, final Object value) {
-        if (BelongsToModuleContext.class.isAssignableFrom(type) || ImportedModuleContext.class.isAssignableFrom(type)) {
+    private void updateImportedNamespaces(final ParserNamespace<?, ?> type, final Object value) {
+        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()) {
                 importedNamespaces = new ArrayList<>(1);
             }
-
-            verify(value instanceof RootStatementContext);
             importedNamespaces.add((RootStatementContext<?, ?, ?>) value);
         }
     }
 
     @Override
-    public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key,
-           final V value) {
+    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);
         // FIXME: what about duplicates?
@@ -235,8 +229,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     @Override
-    public <K, V, N extends ParserNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key,
-           final V value) {
+    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);
         if (ret == null) {
@@ -251,7 +244,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     @Override
-    public <K, V, N extends ParserNamespace<K, V>> V getFromLocalStorage(final Class<N> type, final K key) {
+    public <K, V> V getFromLocalStorage(final ParserNamespace<K, V> type, final K key) {
         final V potentialLocal = getRoot().getFromLocalStorage(type, key);
         if (potentialLocal != null) {
             return potentialLocal;
@@ -267,7 +260,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
     }
 
     @Override
-    public <K, V, N extends ParserNamespace<K, V>> Map<K, V> getAllFromLocalStorage(final Class<N> type) {
+    public <K, V> Map<K, V> getAllFromLocalStorage(final ParserNamespace<K, V> type) {
         final Map<K, V> potentialLocal = getRoot().getAllFromLocalStorage(type);
         if (potentialLocal != null) {
             return potentialLocal;
@@ -285,10 +278,9 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
 
     @Override
     @SuppressWarnings("unchecked")
-    public <K, V, N extends ParserNamespace<K, V>> NamespaceBehaviour<K, V, N> getNamespaceBehaviour(
-            final Class<N> type) {
-        if (StatementSupportNamespace.class.equals(type)) {
-            return (NamespaceBehaviour<K, V, N>) statementSupports;
+    public <K, V> NamespaceBehaviour<K, V> getNamespaceBehaviour(final ParserNamespace<K, V> type) {
+        if (StatementSupport.NAMESPACE.equals(type)) {
+            return (NamespaceBehaviour<K, V>) statementSupports;
         }
         return globalContext.getNamespaceBehaviour(type);
     }
@@ -386,17 +378,16 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
             }
         }
 
-        switch (exceptions.size()) {
-            case 0:
-                return Optional.empty();
-            case 1:
-                return Optional.of(exceptions.get(0));
-            default:
+        return switch (exceptions.size()) {
+            case 0 -> Optional.empty();
+            case 1 -> Optional.of(exceptions.get(0));
+            default -> {
                 final String message = String.format("Yang model processing phase %s failed", identifier);
                 final InferenceException ex = new InferenceException(message, root, exceptions.get(0));
                 exceptions.listIterator(1).forEachRemaining(ex::addSuppressed);
-                return Optional.of(ex);
-        }
+                yield Optional.of(ex);
+            }
+        };
     }
 
     void loadStatements() {
@@ -423,9 +414,9 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         }
     }
 
-    private PrefixToModule preLinkagePrefixes() {
-        final PrefixToModuleMap preLinkagePrefixes = new PrefixToModuleMap();
-        final Map<String, XMLNamespace> prefixToNamespaceMap = getAllFromLocalStorage(ImpPrefixToNamespace.class);
+    private PrefixResolver preLinkagePrefixes() {
+        final HashMapPrefixResolver preLinkagePrefixes = new HashMapPrefixResolver();
+        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;
@@ -435,19 +426,17 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
         return preLinkagePrefixes;
     }
 
-    private PrefixToModule prefixes() {
-        final Map<String, StmtContext<?, ?, ?>> allImports = getRoot().getAllFromNamespace(
-            ImportPrefixToModuleCtx.class);
+    private PrefixResolver prefixes() {
+        final var allImports = root.getAllFromNamespace(ParserNamespaces.IMPORT_PREFIX_TO_MODULECTX);
         if (allImports != null) {
             allImports.forEach((key, value) ->
-                prefixToModuleMap.put(key, getRoot().getFromNamespace(ModuleCtxToModuleQName.class, value)));
+                prefixToModuleMap.put(key, root.getFromNamespace(ParserNamespaces.MODULECTX_TO_QNAME, value)));
         }
 
-        final Map<String, StmtContext<?, ?, ?>> allBelongsTo = getRoot().getAllFromNamespace(
-            BelongsToPrefixToModuleCtx.class);
+        final var allBelongsTo = root.getAllFromNamespace(ParserNamespaces.BELONGSTO_PREFIX_TO_MODULECTX);
         if (allBelongsTo != null) {
             allBelongsTo.forEach((key, value) ->
-                prefixToModuleMap.put(key, getRoot().getFromNamespace(ModuleCtxToModuleQName.class, value)));
+                prefixToModuleMap.put(key, root.getFromNamespace(ParserNamespaces.MODULECTX_TO_QNAME, value)));
         }
 
         return prefixToModuleMap;
@@ -466,7 +455,7 @@ final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeha
 
         // We need to any and all extension statements which have been declared in the context
         final Map<QName, StatementSupport<?, ?, ?>> extensions = globalContext.getNamespace(
-                StatementDefinitionNamespace.class);
+            StatementDefinitions.NAMESPACE);
         if (extensions != null) {
             extensions.forEach((qname, support) -> {
                 final StatementSupport<?, ?, ?> existing = qnameToStmtDefMap.putIfAbsent(qname, support);