Convert AbstractIncludeStatementSupport
[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.InferenceException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
34 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
36
37 abstract class AbstractIncludeStatementSupport
38         extends BaseStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
39
40     AbstractIncludeStatementSupport() {
41         super(YangStmtMapping.INCLUDE);
42     }
43
44     @Override
45     public final void onPreLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
46         final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
47             RevisionDateStatement.class);
48         stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument())
49             : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.getStatementArgument()));
50     }
51
52     @Override
53     public final void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
54         final String submoduleName = stmt.coerceStatementArgument();
55         final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
56             RevisionDateStatement.class);
57
58         final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
59         final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
60         if (revision == null) {
61             requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
62                 NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
63         } else {
64             requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
65                 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.getStatementArgument())),
66                 SOURCE_LINKAGE);
67         }
68
69         includeAction.apply(new InferenceAction() {
70             @Override
71             public void apply(final InferenceContext ctx) {
72                 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
73
74                 stmt.addToNs(IncludedModuleContext.class, revision != null
75                         ? RevisionSourceIdentifier.create(submoduleName, revision.getStatementArgument())
76                                 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
77                 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.getStatementArgument(),
78                     includedSubModuleContext);
79             }
80
81             @Override
82             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
83                 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
84                     stmt.getStatementSourceReference(),
85                     "Included submodule '%s' was not found: ", stmt.getStatementArgument());
86             }
87         });
88     }
89
90     @Override
91     protected final IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
92             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
93         return new RegularIncludeStatement(ctx, substatements);
94     }
95
96     @Override
97     protected final IncludeStatement createEmptyDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
98         return new EmptyIncludeStatement(ctx);
99     }
100
101     @Override
102     protected final IncludeEffectiveStatement createEffective(
103             final StmtContext<String, IncludeStatement, IncludeEffectiveStatement> ctx,
104             final IncludeStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
105         return new RegularIncludeEffectiveStatement(declared, substatements);
106     }
107
108     @Override
109     protected final IncludeEffectiveStatement createEmptyEffective(
110             final StmtContext<String, IncludeStatement, IncludeEffectiveStatement> ctx,
111             final IncludeStatement declared) {
112         return new EmptyIncludeEffectiveStatement(declared);
113     }
114 }