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=f66e2b96c067e68fa269307dc0a41ca4d6f78c1d;hb=04fa25a4fe8957f6492618aa9a1e9a4f9af39df4;hp=b34c5eef76a3e0b5e0c9030e56f8e715bbdb9a7b;hpb=da23daf8c56c75066a7b376bc6603cfc0c278ef2;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 b34c5eef76..f66e2b96c0 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 @@ -7,101 +7,125 @@ */ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; +import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX; import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf; - +import com.google.common.base.Optional; import java.net.URI; import java.util.Date; - import org.opendaylight.yangtools.yang.common.QNameModule; +import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping; 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.parser.spi.ModuleNamespace; import org.opendaylight.yangtools.yang.parser.spi.NamespaceToModule; +import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; 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.ModuleCtxToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleIdentifierToModuleQName; import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToModuleQName; +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(Rfc6020Mapping + .MODULE) + .add(Rfc6020Mapping.ANYXML, 0, MAX) + .add(Rfc6020Mapping.AUGMENT, 0, MAX) + .add(Rfc6020Mapping.CHOICE, 0, MAX) + .add(Rfc6020Mapping.CONTACT, 0, 1) + .add(Rfc6020Mapping.CONTAINER, 0, MAX) + .add(Rfc6020Mapping.DESCRIPTION, 0, 1) + .add(Rfc6020Mapping.DEVIATION, 0, MAX) + .add(Rfc6020Mapping.EXTENSION, 0, MAX) + .add(Rfc6020Mapping.FEATURE, 0, MAX) + .add(Rfc6020Mapping.GROUPING, 0, MAX) + .add(Rfc6020Mapping.IDENTITY, 0, MAX) + .add(Rfc6020Mapping.IMPORT, 0, MAX) + .add(Rfc6020Mapping.INCLUDE, 0, MAX) + .add(Rfc6020Mapping.LEAF, 0, MAX) + .add(Rfc6020Mapping.LEAF_LIST, 0, MAX) + .add(Rfc6020Mapping.LIST, 0, MAX) + .add(Rfc6020Mapping.NAMESPACE, 1, 1) + .add(Rfc6020Mapping.NOTIFICATION, 0, MAX) + .add(Rfc6020Mapping.ORGANIZATION, 0, 1) + .add(Rfc6020Mapping.PREFIX, 1, 1) + .add(Rfc6020Mapping.REFERENCE, 0, 1) + .add(Rfc6020Mapping.REVISION, 0, MAX) + .add(Rfc6020Mapping.RPC, 0, MAX) + .add(Rfc6020Mapping.TYPEDEF, 0, MAX) + .add(Rfc6020Mapping.USES, 0, MAX) + .add(Rfc6020Mapping.YANG_VERSION, 0, 1) + .build(); public ModuleStatementSupport() { super(Rfc6020Mapping.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 onLinkageDeclared(final Mutable> stmt) { Optional moduleNs = Optional.fromNullable(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()); - // FIXME: this is wrong, it has to select the newest revision statement, not the first it encounters. - // YANG files are not required to order revisions - Optional revisionDate = Optional.fromNullable(firstAttributeOf(stmt.declaredSubstatements(), - RevisionStatement.class)); + Optional revisionDate = Optional.fromNullable(Utils.getLatestRevision(stmt.declaredSubstatements())); + if (!revisionDate.isPresent()) { + revisionDate = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_REV); + } - qNameModule = QNameModule.cachedReference(QNameModule.create(moduleNs.get(), revisionDate.orNull())); + QNameModule qNameModule = QNameModule.create(moduleNs.get(), revisionDate.orNull()).intern(); ModuleIdentifier moduleIdentifier = new ModuleIdentifierImpl(stmt.getStatementArgument(), Optional. absent(), 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); stmt.addToNs(ModuleQNameToModuleName.class, qNameModule, stmt.getStatementArgument()); stmt.addToNs(ModuleIdentifierToModuleQName.class, moduleIdentifier, qNameModule); - stmt.addToNs(ImpPrefixToModuleIdentifier.class, modulePrefix, moduleIdentifier); } @Override - public void onFullDefinitionDeclared( - final Mutable> stmt) - throws SourceException { - - stmt.addContext(NamespaceToModule.class, qNameModule, stmt); + public void onFullDefinitionDeclared(final Mutable> stmt) throws SourceException { + super.onFullDefinitionDeclared(stmt); + SUBSTATEMENT_VALIDATOR.validate(stmt); } -} \ No newline at end of file +}