2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.rfc7950.stmt.include;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
12 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
14 import com.google.common.annotations.Beta;
15 import com.google.common.collect.ImmutableList;
16 import java.util.Collection;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.Revision;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
26 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStringStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
39 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
40 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
43 public final class IncludeStatementSupport
44 extends BaseStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
45 private static final @NonNull IncludeStatementSupport RFC6020_INSTANCE = new IncludeStatementSupport(
46 SubstatementValidator.builder(YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build());
47 private static final @NonNull IncludeStatementSupport RFC7950_INSTANCE = new IncludeStatementSupport(
48 SubstatementValidator.builder(YangStmtMapping.INCLUDE)
49 .addOptional(YangStmtMapping.REVISION_DATE)
50 .addOptional(YangStmtMapping.DESCRIPTION)
51 .addOptional(YangStmtMapping.REFERENCE)
54 private final SubstatementValidator validator;
56 IncludeStatementSupport(final SubstatementValidator validator) {
57 super(YangStmtMapping.INCLUDE, CopyPolicy.REJECT);
58 this.validator = requireNonNull(validator);
61 public static @NonNull IncludeStatementSupport rfc6020Instance() {
62 return RFC6020_INSTANCE;
65 public static @NonNull IncludeStatementSupport rfc7950Instance() {
66 return RFC7950_INSTANCE;
70 public void onPreLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
71 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
72 RevisionDateStatement.class);
73 stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.argument())
74 : RevisionSourceIdentifier.create(stmt.argument(), revision.argument()));
78 public void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
79 final String submoduleName = stmt.getArgument();
80 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class);
82 final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
83 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
84 if (revision == null) {
85 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
86 NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
88 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
89 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.argument())), SOURCE_LINKAGE);
92 includeAction.apply(new InferenceAction() {
94 public void apply(final InferenceContext ctx) {
95 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
97 stmt.addToNs(IncludedModuleContext.class, revision != null
98 ? RevisionSourceIdentifier.create(submoduleName, revision.argument())
99 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
100 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.argument(), includedSubModuleContext);
104 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
105 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), stmt,
106 "Included submodule '%s' was not found: ", stmt.argument());
112 protected SubstatementValidator getSubstatementValidator() {
117 protected IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
118 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
119 return new RegularIncludeStatement(ctx.getRawArgument(), ctx.getArgument(), substatements);
123 protected IncludeStatement createEmptyDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
124 return new EmptyIncludeStatement(ctx.getRawArgument(), ctx.getArgument());
128 protected IncludeEffectiveStatement createEffective(final Current<String, IncludeStatement> stmt,
129 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
130 return substatements.isEmpty() ? new EmptyIncludeEffectiveStatement(stmt.declared())
131 : new RegularIncludeEffectiveStatement(stmt.declared(), substatements);