X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-reactor%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FSourceSpecificContext.java;h=911e92cd63057410a25a2178d951516855669ae9;hb=5db72610e7358baa1f5c9dd85abc9e93f3c8b020;hp=0c0783e130097c154eceb2d6e143ec2148e849a9;hpb=715697a8019fccbfed3c826cb69510dedb1560fd;p=yangtools.git diff --git a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java index 0c0783e130..911e92cd63 100644 --- a/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java +++ b/yang/yang-parser-reactor/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SourceSpecificContext.java @@ -23,22 +23,16 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.Set; -import javax.annotation.Nullable; 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.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.api.meta.StatementSource; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; 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.MutableStatement; 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; @@ -63,9 +57,8 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable { - - public enum PhaseCompletionProgress { +final class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable { + enum PhaseCompletionProgress { NO_PROGRESS, PROGRESS, FINISHED @@ -76,8 +69,10 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh private final Multimap modifiers = HashMultimap.create(); private final QNameToStatementDefinitionMap qnameToStmtDefMap = new QNameToStatementDefinitionMap(); private final PrefixToModuleMap prefixToModuleMap = new PrefixToModuleMap(); - private final BuildGlobalContext currentContext; - private final StatementStreamSource source; + private final @NonNull BuildGlobalContext globalContext; + + // Freed as soon as we complete ModelProcessingPhase.EFFECTIVE_MODEL + private StatementStreamSource source; /* * "imported" namespaces in this source -- this points to RootStatementContexts of @@ -89,44 +84,29 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh private ModelProcessingPhase inProgressPhase; private RootStatementContext root; - SourceSpecificContext(final BuildGlobalContext currentContext, final StatementStreamSource source) { - this.currentContext = requireNonNull(currentContext); + SourceSpecificContext(final BuildGlobalContext globalContext, final StatementStreamSource source) { + this.globalContext = requireNonNull(globalContext); this.source = requireNonNull(source); } - boolean isEnabledSemanticVersioning() { - return currentContext.isEnabledSemanticVersioning(); + @NonNull BuildGlobalContext globalContext() { + return globalContext; } ModelProcessingPhase getInProgressPhase() { return inProgressPhase; } - Optional> lookupDeclaredChild(final StatementContextBase current, - final int childId) { - if (current == null) { - return Optional.empty(); - } - - // Fast path: we are entering a statement which was emitted in previous phase - StatementContextBase existing = current.lookupSubstatement(childId); - while (existing != null && StatementSource.CONTEXT == existing.getStatementSource()) { - existing = existing.lookupSubstatement(childId); - } - - return Optional.ofNullable(existing); - } - - StatementContextBase createDeclaredChild(final StatementContextBase current, final int childId, - final QName name, final String argument, final StatementSourceReference ref) { - StatementDefinitionContext def = currentContext.getStatementDefinition(getRootVersion(), name); + AbstractResumedStatement createDeclaredChild(final AbstractResumedStatement current, + final int childId, final QName name, final String argument, final StatementSourceReference ref) { + StatementDefinitionContext def = globalContext.getStatementDefinition(getRootVersion(), name); if (def == null) { - def = currentContext.getModelDefinedStatementDefinition(name); + def = globalContext.getModelDefinedStatementDefinition(name); if (def == null) { final StatementSupport extension = qnameToStmtDefMap.get(name); if (extension != null) { def = new StatementDefinitionContext<>(extension); - currentContext.putModelDefinedStatementDefinition(name, def); + globalContext.putModelDefinedStatementDefinition(name, def); } } } else if (current != null && StmtContextUtils.isUnrecognizedStatement(current)) { @@ -140,7 +120,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh } if (InferenceException.throwIfNull(def, ref, "Statement %s does not have type mapping defined.", name) - .hasArgument()) { + .getArgumentDefinition().isPresent()) { SourceException.throwIfNull(argument, ref, "Statement %s requires an argument", name); } else { SourceException.throwIf(argument != null, ref, "Statement %s does not take argument", name); @@ -172,7 +152,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh root.getRootIdentifier()); } else { final QName rootStatement = root.definition().getStatementName(); - final String rootArgument = root.rawStatementArgument(); + final String rootArgument = root.rawArgument(); checkState(Objects.equals(def.getStatementName(), rootStatement) && Objects.equals(argument, rootArgument), "Root statement was already defined as '%s %s'.", rootStatement, rootArgument); @@ -193,16 +173,8 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh return root != null ? root.getRootVersion() : RootStatementContext.DEFAULT_VERSION; } - DeclaredStatement buildDeclared() { - return root.buildDeclared(); - } - - EffectiveStatement buildEffective() { - return root.buildEffective(); - } - void startPhase(final ModelProcessingPhase phase) { - @Nullable final ModelProcessingPhase previousPhase = phase.getPreviousPhase(); + final ModelProcessingPhase previousPhase = phase.getPreviousPhase(); verify(Objects.equals(previousPhase, finishedPhase), "Phase sequencing violation: previous phase should be %s, source %s has %s", previousPhase, source, finishedPhase); @@ -268,7 +240,6 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh return null; } - @Nullable @Override public > Map getAllFromLocalStorage(final Class type) { final Map potentialLocal = getRoot().getAllFromLocalStorage(type); @@ -289,29 +260,32 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh @Override public > NamespaceBehaviour getNamespaceBehaviour( final Class type) { - return currentContext.getNamespaceBehaviour(type); + return globalContext.getNamespaceBehaviour(type); } @Override - public NamespaceStorageNode getParentNamespaceStorage() { - return currentContext; + public BuildGlobalContext getParentNamespaceStorage() { + return globalContext; } PhaseCompletionProgress tryToCompletePhase(final ModelProcessingPhase phase) { final Collection currentPhaseModifiers = modifiers.get(phase); boolean hasProgressed = tryToProgress(currentPhaseModifiers); - - checkNotNull(this.root, "Malformed source. Valid root element is missing."); - final boolean phaseCompleted = root.tryToCompletePhase(phase); + final boolean phaseCompleted = requireNonNull(root, "Malformed source. Valid root element is missing.") + .tryToCompletePhase(phase); hasProgressed |= tryToProgress(currentPhaseModifiers); if (phaseCompleted && currentPhaseModifiers.isEmpty()) { finishedPhase = phase; LOG.debug("Source {} finished phase {}", source, phase); + if (phase == ModelProcessingPhase.EFFECTIVE_MODEL) { + // We have the effective model acquired, which is the final phase of source interaction. + LOG.trace("Releasing source {}", source); + source = null; + } return PhaseCompletionProgress.FINISHED; - } return hasProgressed ? PhaseCompletionProgress.PROGRESS : PhaseCompletionProgress.NO_PROGRESS; @@ -359,8 +333,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh } final String message = String.format("Yang model processing phase %s failed", identifier); - final InferenceException e = new InferenceException(message, root.getStatementSourceReference(), - exceptions.get(0)); + final InferenceException e = new InferenceException(message, root.sourceReference(), exceptions.get(0)); exceptions.listIterator(1).forEachRemaining(e::addSuppressed); return Optional.of(e); @@ -391,7 +364,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh } private PrefixToModule preLinkagePrefixes() { - final PrefixToModuleMap preLinkagePrefixes = new PrefixToModuleMap(true); + final PrefixToModuleMap preLinkagePrefixes = new PrefixToModuleMap(); final Map prefixToNamespaceMap = getAllFromLocalStorage(ImpPrefixToNamespace.class); if (prefixToNamespaceMap == null) { //:FIXME if it is a submodule without any import, the map is null. Handle also submodules and includes... @@ -422,7 +395,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh private QNameToStatementDefinition stmtDef() { // regular YANG statements and extension supports added - final StatementSupportBundle supportsForPhase = currentContext.getSupportsForPhase(inProgressPhase); + final StatementSupportBundle supportsForPhase = globalContext.getSupportsForPhase(inProgressPhase); qnameToStmtDefMap.putAll(supportsForPhase.getCommonDefinitions()); qnameToStmtDefMap.putAll(supportsForPhase.getDefinitionsSpecificForVersion(getRootVersion())); @@ -432,7 +405,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh } // We need to any and all extension statements which have been declared in the context - final Map> extensions = currentContext.getAllFromNamespace( + final Map> extensions = globalContext.getNamespace( StatementDefinitionNamespace.class); if (extensions != null) { extensions.forEach((qname, support) -> { @@ -448,14 +421,6 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh return qnameToStmtDefMap; } - public Set getSupportedVersions() { - return currentContext.getSupportedVersions(); - } - - void addMutableStmtToSeal(final MutableStatement mutableStatement) { - currentContext.addMutableStmtToSeal(mutableStatement); - } - Collection getRequiredSources() { return root.getRequiredSources(); }