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%2FIncludeStatementImpl.java;h=872426cf3529aca09d110c5d091652a140e35054;hb=04fa25a4fe8957f6492618aa9a1e9a4f9af39df4;hp=70effef3906ca37ae4656323a829ad26a2cbd699;hpb=345f2191afb6c174dca82f0e343fb54b5516eef3;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/IncludeStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/IncludeStatementImpl.java index 70effef390..872426cf35 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/IncludeStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/IncludeStatementImpl.java @@ -1,4 +1,4 @@ -/** +/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the @@ -9,11 +9,8 @@ package org.opendaylight.yangtools.yang.parser.stmt.rfc6020; import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE; import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf; - -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl; - +import com.google.common.base.Optional; import java.net.URI; -import java.text.ParseException; import java.util.Collection; import java.util.Date; import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil; @@ -25,6 +22,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement; import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement; import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl; import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace; +import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; @@ -33,12 +31,15 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Infere import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite; 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.IncludedSubmoduleNameToIdentifier; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; -import com.google.common.base.Optional; +import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl; public class IncludeStatementImpl extends AbstractDeclaredStatement implements IncludeStatement { + private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder( + Rfc6020Mapping.INCLUDE).add(Rfc6020Mapping.REVISION_DATE, 0, 1).build(); - protected IncludeStatementImpl(StmtContext context) { + protected IncludeStatementImpl(final StmtContext context) { super(context); } @@ -50,69 +51,67 @@ public class IncludeStatementImpl extends AbstractDeclaredStatement impl } @Override - public String parseArgumentValue(StmtContext ctx, String value) { + public String parseArgumentValue(final StmtContext ctx, final String value) { return value; } @Override - public IncludeStatement createDeclared(StmtContext ctx) { + public IncludeStatement createDeclared(final StmtContext ctx) { return new IncludeStatementImpl(ctx); } @Override public EffectiveStatement createEffective( - StmtContext> ctx) { + final StmtContext> ctx) { return new IncludeEffectiveStatementImpl(ctx); } @Override public void onLinkageDeclared( - final Mutable> stmt) - throws SourceException { + final Mutable> stmt) { final ModuleIdentifier includeSubmoduleIdentifier = getIncludeSubmoduleIdentifier(stmt); ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE); final Prerequisite> requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class, includeSubmoduleIdentifier, SOURCE_LINKAGE); - final Prerequisite> mutatesCtxPrerequisite = includeAction.mutatesCtx(stmt.getRoot(), - SOURCE_LINKAGE); includeAction.apply(new InferenceAction() { - @Override - public void apply() throws InferenceException { - StmtContext includedSubmoduleStmt = requiresCtxPrerequisite.get(); + public void apply() { + StmtContext includedSubModuleContext = requiresCtxPrerequisite.get(); - mutatesCtxPrerequisite.get().addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier, - includedSubmoduleStmt); + stmt.addToNs(IncludedModuleContext.class, includeSubmoduleIdentifier, + includedSubModuleContext); + stmt.addToNs(IncludedSubmoduleNameToIdentifier.class, + stmt.getStatementArgument(), includeSubmoduleIdentifier); } @Override - public void prerequisiteFailed(Collection> failed) throws InferenceException { - if (failed.contains(requiresCtxPrerequisite)) { - throw new InferenceException("Included submodule was not found.", stmt - .getStatementSourceReference()); - } + public void prerequisiteFailed(final Collection> failed) { + InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), + stmt.getStatementSourceReference(), + "Included submodule '%s' was not found: ", stmt.getStatementArgument()); } }); } - private static ModuleIdentifier getIncludeSubmoduleIdentifier(Mutable stmt) { + private static ModuleIdentifier getIncludeSubmoduleIdentifier(final Mutable stmt) { String subModuleName = stmt.getStatementArgument(); - String revisionArg = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class); - final Optional revision; - if (revisionArg != null) { - try { - revision = Optional.of(SimpleDateFormatUtil.getRevisionFormat().parse(revisionArg)); - } catch (ParseException e) { - throw new IllegalArgumentException(e); - } - } else { - revision = Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP); + + Date revisionDate = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class); + if (revisionDate == null) { + revisionDate = SimpleDateFormatUtil.DEFAULT_DATE_IMP; } - return new ModuleIdentifierImpl(subModuleName, Optional. absent(), revision); + return new ModuleIdentifierImpl(subModuleName, Optional. absent(), Optional. of(revisionDate)); + } + + @Override + public void onFullDefinitionDeclared(final Mutable> stmt) throws SourceException { + super.onFullDefinitionDeclared(stmt); + SUBSTATEMENT_VALIDATOR.validate(stmt); } }