2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
13 import java.util.Collection;
14 import java.util.Optional;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.common.Revision;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
23 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
37 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
41 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
42 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
43 YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build();
45 protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
49 public static class Definition extends
50 AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
53 super(YangStmtMapping.INCLUDE);
57 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
62 public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
63 return new IncludeStatementImpl(ctx);
67 public EffectiveStatement<String, IncludeStatement> createEffective(
68 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
69 return new IncludeEffectiveStatementImpl(ctx);
73 public void onPreLinkageDeclared(
74 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
75 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
76 RevisionDateStatement.class);
77 stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument())
78 : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.getStatementArgument()));
82 public void onLinkageDeclared(
83 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
84 final String submoduleName = stmt.getStatementArgument();
85 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
86 RevisionDateStatement.class);
88 final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
89 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
90 if (revision == null) {
91 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
92 NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
94 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
95 ModuleIdentifierImpl.create(submoduleName, Optional.of(revision.getStatementArgument())),
99 includeAction.apply(new InferenceAction() {
101 public void apply(final InferenceContext ctx) {
102 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
104 stmt.addToNs(IncludedModuleContext.class, revision != null
105 ? RevisionSourceIdentifier.create(submoduleName, revision.getStatementArgument())
106 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
107 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.getStatementArgument(),
108 includedSubModuleContext);
112 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
113 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
114 stmt.getStatementSourceReference(),
115 "Included submodule '%s' was not found: ", stmt.getStatementArgument());
121 protected SubstatementValidator getSubstatementValidator() {
122 return SUBSTATEMENT_VALIDATOR;
128 public String getModule() {
133 public RevisionDateStatement getRevisionDate() {
134 return firstDeclared(RevisionDateStatement.class);
138 public DescriptionStatement getDescription() {
139 return firstDeclared(DescriptionStatement.class);
143 public ReferenceStatement getReference() {
144 return firstDeclared(ReferenceStatement.class);