X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Fstmt%2Freactor%2FRootStatementContext.java;h=7417165d8691d9e0f2280a38c6f44f72f2e01844;hb=256d2ca913643ae8d06053f6fb631f209d7eca64;hp=295d2cc8ca3ea6c5df4d1738920e52404a9476f2;hpb=e9f9f9f58e79ea99d4b76637087de1a5df4115db;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java index 295d2cc8ca..7417165d86 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java @@ -7,29 +7,96 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.reactor; +import com.google.common.base.Optional; +import com.google.common.base.Preconditions; +import com.google.common.base.Verify; +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Map; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.YangVersion; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; 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.parser.spi.meta.CopyType; 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.source.SourceException; +import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType; +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.IncludedModuleContext; +import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference; +/** + * 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, E extends EffectiveStatement> extends + StatementContextBase { -class RootStatementContext, E extends EffectiveStatement> - extends StatementContextBase { - + public static final YangVersion DEFAULT_VERSION = YangVersion.VERSION_1; private final SourceSpecificContext sourceContext; + private final A argument; + + private YangVersion version; + + /** + * References to RootStatementContext of submodules which are included in this source. + */ + private Collection> includedContexts = ImmutableList.of(); - RootStatementContext(ContextBuilder builder, SourceSpecificContext sourceContext) throws SourceException { - super(builder); - this.sourceContext = sourceContext; + RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext def, + final StatementSourceReference ref, final String rawArgument) { + super(def, ref, rawArgument); + this.sourceContext = Preconditions.checkNotNull(sourceContext); + this.argument = def.parseArgumentValue(this, rawArgument); } + RootStatementContext(final SourceSpecificContext sourceContext, final StatementDefinitionContext def, + final StatementSourceReference ref, final String rawArgument, final YangVersion version) { + this(sourceContext, def, ref, rawArgument); + this.setRootVersion(version); + } + + RootStatementContext(final RootStatementContext original, final QNameModule newQNameModule, + final CopyType typeOfCopy) { + super(original); + + sourceContext = Preconditions.checkNotNull(original.sourceContext); + this.argument = original.argument; + + final Collection> declared = original.declaredSubstatements(); + final Collection> effective = original.effectiveSubstatements(); + final Collection> buffer = new ArrayList<>(declared.size() + effective.size()); + + for (final StatementContextBase stmtContext : declared) { + if (StmtContextUtils.areFeaturesSupported(stmtContext)) { + buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy)); + } + } + for (final StmtContext stmtContext : effective) { + buffer.add(stmtContext.createCopy(newQNameModule, this, typeOfCopy)); + } + + addEffectiveSubstatements(buffer); + } + + /** + * @return null as root cannot have parent + */ @Override - public StatementContextBase getParentContext() { + public StatementContextBase getParentContext() { return null; } + /** + * @return namespace storage of source context + */ @Override public NamespaceStorageNode getParentNamespaceStorage() { return sourceContext; @@ -41,7 +108,15 @@ class RootStatementContext, E extends Effectiv } @Override - public RootStatementContext getRoot() { + public StorageNodeType getStorageNodeType() { + return StorageNodeType.ROOT_STATEMENT_LOCAL; + } + /** + * @return this as its own root + */ + @Nonnull + @Override + public RootStatementContext getRoot() { return this; } @@ -49,6 +124,122 @@ class RootStatementContext, E extends Effectiv return sourceContext; } + @Override + public A getStatementArgument() { + return argument; + } + /** + * @return copy of this considering {@link CopyType} (augment, uses) + * + * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException + */ + @Override + public StatementContextBase createCopy(final StatementContextBase newParent, + final CopyType typeOfCopy) { + return createCopy(null, newParent, typeOfCopy); + } + + /** + * @return copy of this considering {@link CopyType} (augment, uses) + * + * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException + */ + @Override + public StatementContextBase createCopy(final QNameModule newQNameModule, + final StatementContextBase newParent, final CopyType typeOfCopy) { + final RootStatementContext copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy); + + copy.appendCopyHistory(typeOfCopy, this.getCopyHistory()); + + if (this.getOriginalCtx() != null) { + copy.setOriginalCtx(this.getOriginalCtx()); + } else { + copy.setOriginalCtx(this); + } + definition().onStatementAdded(copy); + return copy; + } + + @Nonnull + @Override + public Optional getSchemaPath() { + return Optional.of(SchemaPath.ROOT); + } + /** + * @return true + */ + @Override + public boolean isRootContext() { + return true; + } + + @Override + public boolean isConfiguration() { + return true; + } + + @Override + public boolean isEnabledSemanticVersioning() { + return sourceContext.isEnabledSemanticVersioning(); + } + + @Override + public > void addToLocalStorage(final Class type, final K key, + final V value) { + if (IncludedModuleContext.class.isAssignableFrom(type)) { + if (includedContexts.isEmpty()) { + includedContexts = new ArrayList<>(1); + } + Verify.verify(value instanceof RootStatementContext); + includedContexts.add((RootStatementContext) value); + } + super.addToLocalStorage(type, key, value); + } + + @Override + public > V getFromLocalStorage(final Class type, final K key) { + final V potentialLocal = super.getFromLocalStorage(type, key); + if (potentialLocal != null) { + return potentialLocal; + } + for (final NamespaceStorageNode includedSource : includedContexts) { + final V potential = includedSource.getFromLocalStorage(type, key); + if (potential != null) { + return potential; + } + } + return null; + } + + @Nullable + @Override + public > Map getAllFromLocalStorage(final Class type) { + final Map potentialLocal = super.getAllFromLocalStorage(type); + if (potentialLocal != null) { + return potentialLocal; + } + for (final NamespaceStorageNode includedSource : includedContexts) { + final Map potential = includedSource.getAllFromLocalStorage(type); + if (potential != null) { + return potential; + } + } + return null; + } + + @Override + public YangVersion getRootVersion() { + return version == null ? DEFAULT_VERSION : version; + } + + @Override + public void setRootVersion(final YangVersion version) { + Preconditions.checkArgument(sourceContext.getSupportedVersions().contains(version), + "Unsupported yang version %s in %s", version, getStatementSourceReference()); + Preconditions.checkState(this.version == null, "Version of root %s has been already set to %s", argument, + this.version); + this.version = Preconditions.checkNotNull(version); + } }