BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / IncludeStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
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 java.util.Collection;
14 import java.util.Date;
15 import java.util.Optional;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
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.model.util.ModuleIdentifierImpl;
25 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
37 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
38 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
39 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.IncludeEffectiveStatementImpl;
40
41 public class IncludeStatementImpl extends AbstractDeclaredStatement<String> implements IncludeStatement {
42     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
43         YangStmtMapping.INCLUDE).addOptional(YangStmtMapping.REVISION_DATE).build();
44
45     protected IncludeStatementImpl(final StmtContext<String, IncludeStatement, ?> context) {
46         super(context);
47     }
48
49     public static class Definition extends
50             AbstractStatementSupport<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> {
51
52         public Definition() {
53             super(YangStmtMapping.INCLUDE);
54         }
55
56         @Override
57         public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
58             return value;
59         }
60
61         @Override
62         public IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx) {
63             return new IncludeStatementImpl(ctx);
64         }
65
66         @Override
67         public EffectiveStatement<String, IncludeStatement> createEffective(
68                 final StmtContext<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> ctx) {
69             return new IncludeEffectiveStatementImpl(ctx);
70         }
71
72         @Override
73         public void onPreLinkageDeclared(
74                 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
75             final StmtContext<Date, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class);
76             stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.getStatementArgument())
77                 : RevisionSourceIdentifier.create(stmt.getStatementArgument(), revision.rawStatementArgument()));
78         }
79
80         @Override
81         public void onLinkageDeclared(
82                 final Mutable<String, IncludeStatement, EffectiveStatement<String, IncludeStatement>> stmt) {
83             final String submoduleName = stmt.getStatementArgument();
84             final StmtContext<Date, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
85                 RevisionDateStatement.class);
86
87             final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
88             final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
89             if (revision == null) {
90                 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
91                     NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
92             } else {
93                 requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class,
94                     ModuleIdentifierImpl.create(submoduleName, Optional.of(revision.getStatementArgument())),
95                     SOURCE_LINKAGE);
96             }
97
98             includeAction.apply(new InferenceAction() {
99                 @Override
100                 public void apply(final InferenceContext ctx) {
101                     final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
102
103                     stmt.addToNs(IncludedModuleContext.class, revision != null
104                             ? RevisionSourceIdentifier.create(submoduleName, revision.rawStatementArgument())
105                                     : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
106                     stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.getStatementArgument(),
107                         includedSubModuleContext);
108                 }
109
110                 @Override
111                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
112                     InferenceException.throwIf(failed.contains(requiresCtxPrerequisite),
113                         stmt.getStatementSourceReference(),
114                         "Included submodule '%s' was not found: ", stmt.getStatementArgument());
115                 }
116             });
117         }
118
119         @Override
120         protected SubstatementValidator getSubstatementValidator() {
121             return SUBSTATEMENT_VALIDATOR;
122         }
123     }
124
125     @Nonnull
126     @Override
127     public String getModule() {
128         return argument();
129     }
130
131     @Override
132     public RevisionDateStatement getRevisionDate() {
133         return firstDeclared(RevisionDateStatement.class);
134     }
135
136     @Override
137     public DescriptionStatement getDescription() {
138         return firstDeclared(DescriptionStatement.class);
139     }
140
141     @Override
142     public ReferenceStatement getReference() {
143         return firstDeclared(ReferenceStatement.class);
144     }
145
146 }