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 org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.findFirstDeclaredSubstatement;
13 import com.google.common.collect.ImmutableList;
14 import java.util.Collection;
15 import java.util.Optional;
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.DeclaredStatement;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
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.parser.rfc7950.stmt.BaseStringStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
38 abstract class AbstractIncludeStatementSupport
39 extends BaseStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
41 AbstractIncludeStatementSupport() {
42 super(YangStmtMapping.INCLUDE);
46 public final void onPreLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
47 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
48 RevisionDateStatement.class);
49 stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument())
50 : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.getStatementArgument()));
54 public final void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
55 final String submoduleName = stmt.coerceStatementArgument();
56 final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
57 RevisionDateStatement.class);
59 final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
60 final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
61 if (revision == null) {
62 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
63 NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
65 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
66 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.getStatementArgument())),
70 includeAction.apply(new InferenceAction() {
72 public void apply(final InferenceContext ctx) {
73 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
75 stmt.addToNs(IncludedModuleContext.class, revision != null
76 ? RevisionSourceIdentifier.create(submoduleName, revision.getStatementArgument())
77 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
78 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.getStatementArgument(),
79 includedSubModuleContext);
83 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
84 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), stmt.sourceReference(),
85 "Included submodule '%s' was not found: ", stmt.getStatementArgument());
91 protected final IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
92 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
93 return new RegularIncludeStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
98 protected final IncludeStatement createEmptyDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
99 return new EmptyIncludeStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
103 protected final IncludeEffectiveStatement createEffective(final Current<String, IncludeStatement> stmt,
104 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
105 return substatements.isEmpty() ? new EmptyIncludeEffectiveStatement(stmt.declared())
106 : new RegularIncludeEffectiveStatement(stmt.declared(), substatements);