Do not access TypeNamespace during build
[yangtools.git] / yang / yang-parser-reactor / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / RootStatementContext.java
index e0c7cf26365ed5a1afa7dc52195157f4bb3000ad..1869d6213b3d18be51bb5b1707da37735cf86843 100644 (file)
@@ -13,6 +13,7 @@ import static com.google.common.base.Verify.verify;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
 import java.util.Collection;
@@ -21,6 +22,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -28,24 +30,36 @@ 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.SourceIdentifier;
+import org.opendaylight.yangtools.yang.parser.spi.GroupingNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.TypeNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
+import org.opendaylight.yangtools.yang.parser.spi.meta.RootStmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Root statement class for a YANG source. All statements defined in that YANG source are mapped underneath an instance
  * of this class, hence recursive lookups from them cross this class.
  */
-public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
-        AbstractResumedStatement<A, D, E> {
-
+public final class RootStatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+        extends AbstractResumedStatement<A, D, E> implements RootStmtContext.Mutable<A, D, E> {
     public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1;
 
-    private final SourceSpecificContext sourceContext;
+    private static final Logger LOG = LoggerFactory.getLogger(RootStatementContext.class);
+    // These namespaces are well-known and not needed after the root is cleaned up
+    private static final Map<Class<?>, SweptNamespace> SWEPT_NAMESPACES = ImmutableMap.of(
+        GroupingNamespace.class, new SweptNamespace(GroupingNamespace.class),
+        SchemaTreeNamespace.class, new SweptNamespace(SchemaTreeNamespace.class),
+        TypeNamespace.class, new SweptNamespace(TypeNamespace.class));
+
+    private final @NonNull SourceSpecificContext sourceContext;
     private final A argument;
 
     private YangVersion rootVersion;
@@ -61,7 +75,7 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         final StatementSourceReference ref, final String rawArgument) {
         super(def, ref, rawArgument);
         this.sourceContext = requireNonNull(sourceContext);
-        this.argument = def.parseArgumentValue(this, rawStatementArgument());
+        this.argument = def.parseArgumentValue(this, rawArgument());
     }
 
     RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext<A, D, E> def,
@@ -84,11 +98,6 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return sourceContext;
     }
 
-    @Override
-    public Registry getBehaviourRegistry() {
-        return sourceContext;
-    }
-
     @Override
     public StorageNodeType getStorageNodeType() {
         return StorageNodeType.ROOT_STATEMENT_LOCAL;
@@ -105,23 +114,19 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
     }
 
     @Override
-    public A getStatementArgument() {
+    public A argument() {
         return argument;
     }
 
     @Override
-    public Optional<SchemaPath> getSchemaPath() {
+    @Deprecated
+    public Optional<SchemaPath> schemaPath() {
         return Optional.of(SchemaPath.ROOT);
     }
 
     @Override
-    public boolean isConfiguration() {
-        return true;
-    }
-
-    @Override
-    public boolean isEnabledSemanticVersioning() {
-        return sourceContext.isEnabledSemanticVersioning();
+    public EffectiveConfig effectiveConfig() {
+        return EffectiveConfig.UNDETERMINED;
     }
 
     @Override
@@ -195,35 +200,6 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return null;
     }
 
-    @Override
-    public YangVersion getRootVersion() {
-        return rootVersion == null ? DEFAULT_VERSION : rootVersion;
-    }
-
-    @Override
-    public void setRootVersion(final YangVersion version) {
-        checkArgument(sourceContext.getSupportedVersions().contains(version),
-                "Unsupported yang version %s in %s", version, getStatementSourceReference());
-        checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
-                this.rootVersion);
-        this.rootVersion = requireNonNull(version);
-    }
-
-    @Override
-    public void addMutableStmtToSeal(final MutableStatement mutableStatement) {
-        sourceContext.addMutableStmtToSeal(mutableStatement);
-    }
-
-    @Override
-    public void addRequiredSource(final SourceIdentifier dependency) {
-        checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
-                "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
-        if (requiredSources.isEmpty()) {
-            requiredSources = new HashSet<>();
-        }
-        requiredSources.add(dependency);
-    }
-
     /**
      * Return the set of required sources.
      *
@@ -233,11 +209,6 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return ImmutableSet.copyOf(requiredSources);
     }
 
-    @Override
-    public void setRootIdentifier(final SourceIdentifier identifier) {
-        this.rootIdentifier = requireNonNull(identifier);
-    }
-
     SourceIdentifier getRootIdentifier() {
         return rootIdentifier;
     }
@@ -257,8 +228,51 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
         return true;
     }
 
+    void setRootIdentifierImpl(final SourceIdentifier identifier) {
+        this.rootIdentifier = requireNonNull(identifier);
+    }
+
+    @NonNull Registry getBehaviourRegistryImpl() {
+        return sourceContext;
+    }
+
+    boolean isEnabledSemanticVersioningImpl() {
+        return sourceContext.globalContext().isEnabledSemanticVersioning();
+    }
+
+    @NonNull YangVersion getRootVersionImpl() {
+        return rootVersion == null ? DEFAULT_VERSION : rootVersion;
+    }
+
+    void setRootVersionImpl(final YangVersion version) {
+        checkArgument(sourceContext.globalContext().getSupportedVersions().contains(version),
+                "Unsupported yang version %s in %s", version, sourceReference());
+        checkState(this.rootVersion == null, "Version of root %s has been already set to %s", argument,
+                this.rootVersion);
+        this.rootVersion = requireNonNull(version);
+    }
+
+    void addMutableStmtToSealImpl(final MutableStatement mutableStatement) {
+        sourceContext.globalContext().addMutableStmtToSeal(mutableStatement);
+    }
+
+    void addRequiredSourceImpl(final SourceIdentifier dependency) {
+        checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE,
+                "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase");
+        if (requiredSources.isEmpty()) {
+            requiredSources = new HashSet<>();
+        }
+        requiredSources.add(dependency);
+    }
+
     @Override
     StatementContextBase<A, D, E> reparent(final StatementContextBase<?, ?, ?> newParent) {
         throw new UnsupportedOperationException("Root statement cannot be reparented to" + newParent);
     }
+
+    @Override
+    void sweepNamespaces() {
+        LOG.trace("Sweeping root {}", this);
+        sweepNamespaces(SWEPT_NAMESPACES);
+    }
 }