*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Date;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.Set;
-import org.opendaylight.yangtools.concepts.Immutable;
+import java.util.stream.Collectors;
+import javax.annotation.Nonnull;
import org.opendaylight.yangtools.concepts.SemVer;
import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.common.QNameModule;
-import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
+import org.opendaylight.yangtools.yang.common.Revision;
+import org.opendaylight.yangtools.yang.common.YangVersion;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
import org.opendaylight.yangtools.yang.model.api.Deviation;
import org.opendaylight.yangtools.yang.model.api.ExtensionDefinition;
import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.ModuleImport;
import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
import org.opendaylight.yangtools.yang.model.api.UsesNode;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.stmt.SubmoduleStatement;
-import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
-import org.opendaylight.yangtools.yang.parser.spi.source.DeclarationInTextSource;
-import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToIdentifier;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
abstract class AbstractEffectiveModule<D extends DeclaredStatement<String>> extends
- AbstractEffectiveDocumentedNode<String, D> implements Module, Immutable {
-
+ AbstractEffectiveDocumentedNode<String, D> implements Module, MutableStatement {
private final String name;
- private final String sourcePath;
private final String prefix;
- private final String yangVersion;
+ private final YangVersion yangVersion;
private final String organization;
private final String contact;
private final Set<ModuleImport> imports;
- private final Set<Module> submodules;
private final Set<FeatureDefinition> features;
private final Set<NotificationDefinition> notifications;
- private final Set<AugmentationSchema> augmentations;
+ private final Set<AugmentationSchemaNode> augmentations;
private final Set<RpcDefinition> rpcs;
private final Set<Deviation> deviations;
private final List<ExtensionDefinition> extensionNodes;
private final Set<DataSchemaNode> publicChildNodes;
private final SemVer semanticVersion;
+ private Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
+ submoduleContextsToBuild;
+ private Set<Module> submodules;
+ private boolean sealed;
+
AbstractEffectiveModule(final StmtContext<String, D, ? extends EffectiveStatement<String, ?>> ctx) {
super(ctx);
this.name = argument();
- PrefixEffectiveStatementImpl prefixStmt = firstEffective(PrefixEffectiveStatementImpl.class);
- this.prefix = (prefixStmt == null) ? null : prefixStmt.argument();
+ EffectiveStatementBase<?, ?> parentOfPrefix = this;
+ if (ctx.getPublicDefinition() == YangStmtMapping.SUBMODULE) {
+ parentOfPrefix = firstEffective(BelongsToEffectiveStatementImpl.class);
+ SourceException.throwIfNull(parentOfPrefix, ctx.getStatementSourceReference(),
+ "Unable to find belongs-to statement in submodule %s.", ctx.getStatementArgument());
+ }
- YangVersionEffectiveStatementImpl yangVersionStmt = firstEffective(YangVersionEffectiveStatementImpl.class);
- this.yangVersion = (yangVersionStmt == null) ? "1" : yangVersionStmt.argument();
+ final PrefixEffectiveStatementImpl prefixStmt = parentOfPrefix
+ .firstEffective(PrefixEffectiveStatementImpl.class);
+ SourceException.throwIfNull(prefixStmt, ctx.getStatementSourceReference(),
+ "Unable to resolve prefix for module or submodule %s.", ctx.getStatementArgument());
+ this.prefix = prefixStmt.argument();
- SemanticVersionEffectiveStatementImpl semanticVersionStmt = firstEffective(SemanticVersionEffectiveStatementImpl.class);
- this.semanticVersion = (semanticVersionStmt == null) ? DEFAULT_SEMANTIC_VERSION : semanticVersionStmt.argument();
+ final YangVersionEffectiveStatementImpl yangVersionStmt =
+ firstEffective(YangVersionEffectiveStatementImpl.class);
+ this.yangVersion = yangVersionStmt == null ? YangVersion.VERSION_1 : yangVersionStmt.argument();
- OrganizationEffectiveStatementImpl organizationStmt = firstEffective(OrganizationEffectiveStatementImpl.class);
- this.organization = (organizationStmt == null) ? null : organizationStmt.argument();
+ final OpenconfigVersionEffectiveStatementImpl semanticVersionStmt =
+ firstEffective(OpenconfigVersionEffectiveStatementImpl.class);
+ this.semanticVersion = semanticVersionStmt == null ? null : semanticVersionStmt.argument();
- ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
- this.contact = (contactStmt == null) ? null : contactStmt.argument();
+ final OrganizationEffectiveStatementImpl organizationStmt =
+ firstEffective(OrganizationEffectiveStatementImpl.class);
+ this.organization = organizationStmt == null ? null : organizationStmt.argument();
- if (ctx.getStatementSourceReference() instanceof DeclarationInTextSource) {
- this.sourcePath = ((DeclarationInTextSource) ctx.getStatementSourceReference()).getSourceName();
- } else {
- this.sourcePath = null;
- }
+ final ContactEffectiveStatementImpl contactStmt = firstEffective(ContactEffectiveStatementImpl.class);
+ this.contact = contactStmt == null ? null : contactStmt.argument();
// init submodules and substatements of submodules
final List<EffectiveStatement<?, ?>> substatementsOfSubmodules;
- final Map<String, ModuleIdentifier> includedSubmodulesMap = ctx
- .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToIdentifier.class);
+ final Map<String, StmtContext<?, ?, ?>> includedSubmodulesMap = ctx
+ .getAllFromCurrentStmtCtxNamespace(IncludedSubmoduleNameToModuleCtx.class);
if (includedSubmodulesMap == null || includedSubmodulesMap.isEmpty()) {
this.submodules = ImmutableSet.of();
+ this.submoduleContextsToBuild = ImmutableSet.of();
substatementsOfSubmodules = ImmutableList.of();
- } else {
- Collection<ModuleIdentifier> includedSubmodules = includedSubmodulesMap.values();
- Set<Module> submodulesInit = new HashSet<>();
- List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
- for (ModuleIdentifier submoduleIdentifier : includedSubmodules) {
- @SuppressWarnings("unchecked")
- Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>> submoduleCtx =
- (Mutable<String, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)
- ctx.getFromNamespace(SubmoduleNamespace.class, submoduleIdentifier);
- SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
+ } else if (YangStmtMapping.MODULE.equals(ctx.getPublicDefinition())) {
+ /*
+ * Aggregation of substatements from submodules should be done only
+ * for modules. In case of submodules it does not make sense because
+ * of possible circular chains of includes between submodules.
+ */
+ final Collection<StmtContext<?, ?, ?>> includedSubmodules = includedSubmodulesMap.values();
+ final Set<Module> submodulesInit = new HashSet<>();
+ final List<EffectiveStatement<?, ?>> substatementsOfSubmodulesInit = new ArrayList<>();
+ for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodules) {
+ final SubmoduleEffectiveStatementImpl submodule = (SubmoduleEffectiveStatementImpl) submoduleCtx
.buildEffective();
submodulesInit.add(submodule);
- substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements());
+ substatementsOfSubmodulesInit.addAll(submodule.effectiveSubstatements().stream()
+ .filter(sub -> sub instanceof SchemaNode || sub instanceof DataNodeContainer)
+ .collect(Collectors.toList()));
}
this.submodules = ImmutableSet.copyOf(submodulesInit);
+ this.submoduleContextsToBuild = ImmutableSet.of();
substatementsOfSubmodules = ImmutableList.copyOf(substatementsOfSubmodulesInit);
+ } else {
+ /*
+ * Because of possible circular chains of includes between submodules we can
+ * collect only submodule contexts here and then build them during
+ * sealing of this statement.
+ */
+ final Set<StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>>
+ submoduleContextsInit = new HashSet<>();
+ for (final StmtContext<?, ?, ?> submoduleCtx : includedSubmodulesMap.values()) {
+ submoduleContextsInit.add(
+ (StmtContext<?, SubmoduleStatement, EffectiveStatement<String, SubmoduleStatement>>)submoduleCtx);
+ }
+
+ this.submoduleContextsToBuild = ImmutableSet.copyOf(submoduleContextsInit);
+ substatementsOfSubmodules = ImmutableList.of();
+ }
+
+ if (!submoduleContextsToBuild.isEmpty()) {
+ ((Mutable<?, ?, ?>) ctx).addMutableStmtToSeal(this);
+ sealed = false;
+ } else {
+ sealed = true;
}
// init substatements collections
- List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
+ final List<EffectiveStatement<?, ?>> effectiveSubstatements = new ArrayList<>();
effectiveSubstatements.addAll(effectiveSubstatements());
effectiveSubstatements.addAll(substatementsOfSubmodules);
- List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
- Set<AugmentationSchema> augmentationsInit = new LinkedHashSet<>();
- Set<ModuleImport> importsInit = new HashSet<>();
- Set<NotificationDefinition> notificationsInit = new HashSet<>();
- Set<RpcDefinition> rpcsInit = new HashSet<>();
- Set<Deviation> deviationsInit = new HashSet<>();
- Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
- Set<FeatureDefinition> featuresInit = new HashSet<>();
- List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
-
- Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
- Set<GroupingDefinition> mutableGroupings = new HashSet<>();
- Set<UsesNode> mutableUses = new HashSet<>();
- Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
- Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
-
- for (EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
+ final List<UnknownSchemaNode> unknownNodesInit = new ArrayList<>();
+ final Set<AugmentationSchemaNode> augmentationsInit = new LinkedHashSet<>();
+ final Set<ModuleImport> importsInit = new HashSet<>();
+ final Set<NotificationDefinition> notificationsInit = new HashSet<>();
+ final Set<RpcDefinition> rpcsInit = new HashSet<>();
+ final Set<Deviation> deviationsInit = new HashSet<>();
+ final Set<IdentitySchemaNode> identitiesInit = new HashSet<>();
+ final Set<FeatureDefinition> featuresInit = new HashSet<>();
+ final List<ExtensionDefinition> extensionNodesInit = new ArrayList<>();
+
+ final Map<QName, DataSchemaNode> mutableChildNodes = new LinkedHashMap<>();
+ final Set<GroupingDefinition> mutableGroupings = new HashSet<>();
+ final Set<UsesNode> mutableUses = new HashSet<>();
+ final Set<TypeDefinition<?>> mutableTypeDefinitions = new LinkedHashSet<>();
+ final Set<DataSchemaNode> mutablePublicChildNodes = new LinkedHashSet<>();
+
+ for (final EffectiveStatement<?, ?> effectiveStatement : effectiveSubstatements) {
if (effectiveStatement instanceof UnknownSchemaNode) {
unknownNodesInit.add((UnknownSchemaNode) effectiveStatement);
}
- if (effectiveStatement instanceof AugmentationSchema) {
- augmentationsInit.add((AugmentationSchema) effectiveStatement);
+ if (effectiveStatement instanceof AugmentationSchemaNode) {
+ augmentationsInit.add((AugmentationSchemaNode) effectiveStatement);
}
if (effectiveStatement instanceof ModuleImport) {
importsInit.add((ModuleImport) effectiveStatement);
featuresInit.add((FeatureDefinition) effectiveStatement);
}
if (effectiveStatement instanceof ExtensionEffectiveStatementImpl) {
- ExtensionEffectiveStatementImpl extensionDefinition = (ExtensionEffectiveStatementImpl) effectiveStatement;
- extensionDefinition.initUnknownSchemaNodes();
- extensionNodesInit
- .add(extensionDefinition);
+ extensionNodesInit.add((ExtensionEffectiveStatementImpl) effectiveStatement);
}
if (effectiveStatement instanceof DataSchemaNode) {
- DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
+ final DataSchemaNode dataSchemaNode = (DataSchemaNode) effectiveStatement;
if (!mutableChildNodes.containsKey(dataSchemaNode.getQName())) {
mutableChildNodes.put(dataSchemaNode.getQName(), dataSchemaNode);
mutablePublicChildNodes.add(dataSchemaNode);
}
}
if (effectiveStatement instanceof UsesNode) {
- UsesNode usesNode = (UsesNode) effectiveStatement;
+ final UsesNode usesNode = (UsesNode) effectiveStatement;
if (!mutableUses.contains(usesNode)) {
mutableUses.add(usesNode);
} else {
}
}
if (effectiveStatement instanceof TypeDefEffectiveStatementImpl) {
- TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
- TypeDefinition<?> type = typeDef.getTypeDefinition();
+ final TypeDefEffectiveStatementImpl typeDef = (TypeDefEffectiveStatementImpl) effectiveStatement;
+ final TypeDefinition<?> type = typeDef.getTypeDefinition();
if (!mutableTypeDefinitions.contains(type)) {
mutableTypeDefinitions.add(type);
} else {
}
}
if (effectiveStatement instanceof GroupingDefinition) {
- GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
+ final GroupingDefinition grp = (GroupingDefinition) effectiveStatement;
if (!mutableGroupings.contains(grp)) {
mutableGroupings.add(grp);
} else {
this.unknownNodes = ImmutableList.copyOf(unknownNodesInit);
this.augmentations = ImmutableSet.copyOf(augmentationsInit);
- if (ctx.isEnabledSemanticVersioning()) {
- this.imports = ImmutableSet.copyOf(importsInit);
- } else {
- this.imports = ImmutableSet.copyOf(resolveModuleImports(importsInit, ctx));
- }
+ this.imports = ImmutableSet.copyOf(importsInit);
this.notifications = ImmutableSet.copyOf(notificationsInit);
this.rpcs = ImmutableSet.copyOf(rpcsInit);
this.deviations = ImmutableSet.copyOf(deviationsInit);
}
- private static Set<ModuleImport> resolveModuleImports(final Set<ModuleImport> importsInit,
- final StmtContext<String, ? extends DeclaredStatement<String>, ? extends EffectiveStatement<String, ?>> ctx) {
- Set<ModuleImport> resolvedModuleImports = new LinkedHashSet<>();
- for (ModuleImport moduleImport : importsInit) {
- if (moduleImport.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_IMP)) {
- QNameModule impModuleQName = Utils.getModuleQNameByPrefix(ctx, moduleImport.getPrefix());
- if (!impModuleQName.getRevision().equals(SimpleDateFormatUtil.DEFAULT_DATE_REV)) {
- ModuleImport resolvedModuleImport = new ModuleImportImpl(moduleImport.getModuleName(),
- impModuleQName.getRevision(), moduleImport.getPrefix());
- resolvedModuleImports.add(resolvedModuleImport);
- }
- } else {
- resolvedModuleImports.add(moduleImport);
- }
- }
- return resolvedModuleImports;
- }
-
- @Override
- public String getModuleSourcePath() {
- return sourcePath;
- }
-
- @Override
- public String getSource() {
- return null;
- }
-
@Override
public URI getNamespace() {
return getQNameModule().getNamespace();
}
@Override
- public Date getRevision() {
+ public Optional<Revision> getRevision() {
return getQNameModule().getRevision();
}
}
@Override
- public String getYangVersion() {
+ public YangVersion getYangVersion() {
return yangVersion;
}
@Override
public Set<Module> getSubmodules() {
+ checkState(sealed, "Attempt to get base submodules from unsealed submodule effective statement %s",
+ getQNameModule());
return submodules;
}
}
@Override
- public Set<AugmentationSchema> getAugmentations() {
+ public Set<AugmentationSchemaNode> getAugmentations() {
return augmentations;
}
return identities;
}
+ @Nonnull
@Override
public List<UnknownSchemaNode> getUnknownSchemaNodes() {
return unknownNodes;
}
@Override
- public final DataSchemaNode getDataChildByName(final QName name) {
- // Child nodes are keyed by their container name, so we can do a direct
- // lookup
- return childNodes.get(name);
+ public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
+ // Child nodes are keyed by their container name, so we can do a direct lookup
+ return Optional.ofNullable(childNodes.get(requireNonNull(name)));
}
@Override
}
@Override
- public SemVer getSemanticVersion() {
- return semanticVersion;
+ public Optional<SemVer> getSemanticVersion() {
+ return Optional.ofNullable(semanticVersion);
}
@Override
public String toString() {
- return this.getClass().getSimpleName() + "[" +
- "name=" + name +
- ", namespace=" + getNamespace() +
- ", revision=" + getRevision() +
- ", prefix=" + prefix +
- ", yangVersion=" + yangVersion +
- "]";
+ return MoreObjects.toStringHelper(this).omitNullValues()
+ .add("name", name)
+ .add("namespace", getNamespace())
+ .add("revision", getRevision().orElse(null))
+ .add("prefix", prefix)
+ .add("yangVersion", yangVersion)
+ .toString();
}
+ @Override
+ public void seal() {
+ if (!sealed) {
+ submodules = ImmutableSet.copyOf(Iterables.transform(submoduleContextsToBuild,
+ ctx -> (Module) ctx.buildEffective()));
+ submoduleContextsToBuild = ImmutableSet.of();
+ sealed = true;
+ }
+ }
}