Reformulate StatementContextFactory.createEffective()
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / include / AbstractIncludeStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.include;
9
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;
12
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;
37
38 abstract class AbstractIncludeStatementSupport
39         extends BaseStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
40
41     AbstractIncludeStatementSupport() {
42         super(YangStmtMapping.INCLUDE);
43     }
44
45     @Override
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()));
51     }
52
53     @Override
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);
58
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);
64         } else {
65             requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
66                 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.getStatementArgument())),
67                 SOURCE_LINKAGE);
68         }
69
70         includeAction.apply(new InferenceAction() {
71             @Override
72             public void apply(final InferenceContext ctx) {
73                 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
74
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);
80             }
81
82             @Override
83             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
84                 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
85                     stmt.getStatementSourceReference(),
86                     "Included submodule '%s' was not found: ", stmt.getStatementArgument());
87             }
88         });
89     }
90
91     @Override
92     protected final IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
93             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
94         return new RegularIncludeStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
95             substatements);
96     }
97
98     @Override
99     protected final IncludeStatement createEmptyDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
100         return new EmptyIncludeStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
101     }
102
103     @Override
104     protected final IncludeEffectiveStatement createEffective(final Current<String, IncludeStatement> stmt,
105             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
106         return substatements.isEmpty() ? new EmptyIncludeEffectiveStatement(stmt.declared())
107             : new RegularIncludeEffectiveStatement(stmt.declared(), substatements);
108     }
109 }