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.model.ri.stmt.DeclaredStatements;
28 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
29 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
41 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
42 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
45 public final class IncludeStatementSupport
46 extends AbstractStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
47 private static final @NonNull IncludeStatementSupport RFC6020_INSTANCE = new IncludeStatementSupport(
48 SubstatementValidator.builder(YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build());
49 private static final @NonNull IncludeStatementSupport RFC7950_INSTANCE = new IncludeStatementSupport(
50 SubstatementValidator.builder(YangStmtMapping.INCLUDE)
51 .addOptional(YangStmtMapping.REVISION_DATE)
52 .addOptional(YangStmtMapping.DESCRIPTION)
53 .addOptional(YangStmtMapping.REFERENCE)
56 private final SubstatementValidator validator;
58 IncludeStatementSupport(final SubstatementValidator validator) {
59 super(YangStmtMapping.INCLUDE, StatementPolicy.reject());
60 this.validator = requireNonNull(validator);
63 public static @NonNull IncludeStatementSupport rfc6020Instance() {
64 return RFC6020_INSTANCE;
67 public static @NonNull IncludeStatementSupport rfc7950Instance() {
68 return RFC7950_INSTANCE;
72 public void onPreLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
73 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
74 RevisionDateStatement.class);
75 stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.argument())
76 : RevisionSourceIdentifier.create(stmt.argument(), revision.argument()));
80 public void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
81 final String submoduleName = stmt.getArgument();
82 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class);
84 final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
85 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
86 if (revision == null) {
87 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
88 NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
90 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
91 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.argument())), SOURCE_LINKAGE);
94 includeAction.apply(new InferenceAction() {
96 public void apply(final InferenceContext ctx) {
97 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
99 stmt.addToNs(IncludedModuleContext.class, revision != null
100 ? RevisionSourceIdentifier.create(submoduleName, revision.argument())
101 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
102 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.argument(), includedSubModuleContext);
106 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
107 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), stmt,
108 "Included submodule '%s' was not found: ", stmt.argument());
114 protected SubstatementValidator getSubstatementValidator() {
119 protected IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
120 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
121 return DeclaredStatements.createInclude(ctx.getRawArgument(), ctx.getArgument(), substatements);
125 protected IncludeStatement createEmptyDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
126 return DeclaredStatements.createInclude(ctx.getRawArgument(), ctx.getArgument());
130 protected IncludeEffectiveStatement createEffective(final Current<String, IncludeStatement> stmt,
131 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
132 return EffectiveStatements.createInclude(stmt.declared(), substatements);