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%2Frfc6020%2FModuleStatementSupport.java;h=28fb20bca28cbe731a034aabbea982a0a4c05300;hb=c47f428aedb1a9555564c11d851f41d7d030f054;hp=963737a7decdb5f6320b5b228d9b9c4d4e27b251;hpb=c4dc5b33e7d24670b59cc81b65e15b37a3268608;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ModuleStatementSupport.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ModuleStatementSupport.java index 963737a7de..28fb20bca2 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ModuleStatementSupport.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/ModuleStatementSupport.java @@ -10,96 +10,169 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf; import java.net.URI; -import java.util.Date; - +import java.util.Optional; +import org.opendaylight.yangtools.concepts.SemVer; import org.opendaylight.yangtools.yang.common.QNameModule; -import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; -import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; +import org.opendaylight.yangtools.yang.common.Revision; +import org.opendaylight.yangtools.yang.model.api.YangStmtMapping; import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement; import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement; -import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement; -import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl; +import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier; +import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace; import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule; +import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; +import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace; +import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace; 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.ImpPrefixToModuleIdentifier; -import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName; +import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils; +import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator; +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.ModuleCtxToModuleQName; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace; +import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNamespaceForBelongsTo; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleQNameToModuleName; import org.opendaylight.yangtools.yang.parser.spi.source.PrefixToModule; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ModuleEffectiveStatementImpl; -import com.google.common.base.Optional; - public class ModuleStatementSupport extends AbstractStatementSupport> { - - private QNameModule qNameModule; + private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping + .MODULE) + .addAny(YangStmtMapping.ANYXML) + .addAny(YangStmtMapping.AUGMENT) + .addAny(YangStmtMapping.CHOICE) + .addOptional(YangStmtMapping.CONTACT) + .addAny(YangStmtMapping.CONTAINER) + .addOptional(YangStmtMapping.DESCRIPTION) + .addAny(YangStmtMapping.DEVIATION) + .addAny(YangStmtMapping.EXTENSION) + .addAny(YangStmtMapping.FEATURE) + .addAny(YangStmtMapping.GROUPING) + .addAny(YangStmtMapping.IDENTITY) + .addAny(YangStmtMapping.IMPORT) + .addAny(YangStmtMapping.INCLUDE) + .addAny(YangStmtMapping.LEAF) + .addAny(YangStmtMapping.LEAF_LIST) + .addAny(YangStmtMapping.LIST) + .addMandatory(YangStmtMapping.NAMESPACE) + .addAny(YangStmtMapping.NOTIFICATION) + .addOptional(YangStmtMapping.ORGANIZATION) + .addMandatory(YangStmtMapping.PREFIX) + .addOptional(YangStmtMapping.REFERENCE) + .addAny(YangStmtMapping.REVISION) + .addAny(YangStmtMapping.RPC) + .addAny(YangStmtMapping.TYPEDEF) + .addAny(YangStmtMapping.USES) + .addOptional(YangStmtMapping.YANG_VERSION) + .addOptional(SupportedExtensionsMapping.OPENCONFIG_VERSION) + .build(); public ModuleStatementSupport() { - super(Rfc6020Mapping.MODULE); + super(YangStmtMapping.MODULE); } @Override - public String parseArgumentValue(StmtContext ctx, String value) { + public String parseArgumentValue(final StmtContext ctx, final String value) { return value; } @Override - public ModuleStatement createDeclared(StmtContext ctx) { + public ModuleStatement createDeclared(final StmtContext ctx) { return new ModuleStatementImpl(ctx); } @Override public EffectiveStatement createEffective( - StmtContext> ctx) { + final StmtContext> ctx) { return new ModuleEffectiveStatementImpl(ctx); } @Override - public void onLinkageDeclared(Mutable> stmt) - throws SourceException { + public void onPreLinkageDeclared(final Mutable> stmt) { + final String moduleName = stmt.getStatementArgument(); + + final URI moduleNs = firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class); + SourceException.throwIfNull(moduleNs, stmt.getStatementSourceReference(), + "Namespace of the module [%s] is missing", stmt.getStatementArgument()); + stmt.addToNs(ModuleNameToNamespace.class, moduleName, moduleNs); + + final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class); + SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(), + "Prefix of the module [%s] is missing", stmt.getStatementArgument()); + stmt.addToNs(ImpPrefixToNamespace.class, modulePrefix, moduleNs); + + stmt.addContext(PreLinkageModuleNamespace.class, moduleName, stmt); + + final Optional revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()); + final QNameModule qNameModule = QNameModule.create(moduleNs, revisionDate.orElse(null)).intern(); + + stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule); + stmt.setRootIdentifier(RevisionSourceIdentifier.create(stmt.getStatementArgument(), revisionDate)); + } + + @Override + public void onLinkageDeclared(final Mutable> stmt) { - Optional moduleNs = Optional.fromNullable(firstAttributeOf(stmt.declaredSubstatements(), + final Optional moduleNs = Optional.ofNullable(firstAttributeOf(stmt.declaredSubstatements(), NamespaceStatement.class)); - if (!moduleNs.isPresent()) { - throw new IllegalArgumentException("Namespace of the module [" + stmt.getStatementArgument() - + "] is missing."); + SourceException.throwIf(!moduleNs.isPresent(), stmt.getStatementSourceReference(), + "Namespace of the module [%s] is missing", stmt.getStatementArgument()); + + final Optional revisionDate = StmtContextUtils.getLatestRevision(stmt.declaredSubstatements()); + final QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orElse(null)).intern(); + final StmtContext> possibleDuplicateModule = + stmt.getFromNamespace(NamespaceToModule.class, qNameModule); + if (possibleDuplicateModule != null && possibleDuplicateModule != stmt) { + throw new SourceException(stmt.getStatementSourceReference(), "Module namespace collision: %s. At %s", + qNameModule.getNamespace(), possibleDuplicateModule.getStatementSourceReference()); } - Optional revisionDate = Optional.fromNullable(firstAttributeOf(stmt.declaredSubstatements(), - RevisionStatement.class)); - - qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orNull()); - ModuleIdentifier moduleIdentifier = new ModuleIdentifierImpl(stmt.getStatementArgument(), - Optional. absent(), revisionDate); + final SourceIdentifier moduleIdentifier = RevisionSourceIdentifier.create(stmt.getStatementArgument(), + revisionDate); stmt.addContext(ModuleNamespace.class, moduleIdentifier, stmt); + stmt.addContext(ModuleNamespaceForBelongsTo.class, moduleIdentifier.getName(), stmt); stmt.addContext(NamespaceToModule.class, qNameModule, stmt); - String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class); - if (modulePrefix == null) { - throw new IllegalArgumentException("Prefix of the module [" + stmt.getStatementArgument() + "] is missing."); - } + final String modulePrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class); + SourceException.throwIfNull(modulePrefix, stmt.getStatementSourceReference(), + "Prefix of the module [%s] is missing", stmt.getStatementArgument()); stmt.addToNs(PrefixToModule.class, modulePrefix, qNameModule); stmt.addToNs(ModuleNameToModuleQName.class, stmt.getStatementArgument(), qNameModule); + stmt.addToNs(ModuleCtxToModuleQName.class, stmt, qNameModule); // tu + stmt.addToNs(ModuleCtxToSourceIdentifier.class, stmt, moduleIdentifier); stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument()); - stmt.addToNs(ModuleIdentifierToModuleQName.class, moduleIdentifier, qNameModule); + stmt.addToNs(ImportPrefixToModuleCtx.class, modulePrefix, stmt); - stmt.addToNs(ImpPrefixToModuleIdentifier.class, modulePrefix, moduleIdentifier); + if (stmt.isEnabledSemanticVersioning()) { + addToSemVerModuleNamespace(stmt, moduleIdentifier); + } } - @Override - public void onFullDefinitionDeclared( - final Mutable> stmt) - throws SourceException { + private static void addToSemVerModuleNamespace( + final Mutable> stmt, + final SourceIdentifier moduleIdentifier) { + final String moduleName = stmt.getStatementArgument(); + final SemVer moduleSemVer = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt); + final SemVerSourceIdentifier id = SemVerSourceIdentifier.create(moduleName, moduleSemVer); + stmt.addToNs(SemanticVersionModuleNamespace.class, id, stmt); + } - stmt.addContext(NamespaceToModule.class, qNameModule, stmt); + @Override + protected SubstatementValidator getSubstatementValidator() { + return SUBSTATEMENT_VALIDATOR; } -} \ No newline at end of file +}