Validate cross-revision imports and includes
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / IncludeStatementSupport.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.meta;
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.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import java.util.Collection;
16 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.Revision;
19 import org.opendaylight.yangtools.yang.common.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.IncludeStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
27 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatementDecorators;
29 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
30 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
31 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
32 import org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceKeyCriterion;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
44 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext;
45 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
46 import org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException;
47
48 @Beta
49 public final class IncludeStatementSupport
50         extends AbstractStringStatementSupport<IncludeStatement, IncludeEffectiveStatement> {
51     private static final SubstatementValidator RFC6020_VALIDATOR =
52         SubstatementValidator.builder(YangStmtMapping.INCLUDE)
53             .addOptional(YangStmtMapping.REVISION_DATE)
54             .build();
55     private static final SubstatementValidator RFC7950_VALIDATOR =
56         SubstatementValidator.builder(YangStmtMapping.INCLUDE)
57             .addOptional(YangStmtMapping.REVISION_DATE)
58             .addOptional(YangStmtMapping.DESCRIPTION)
59             .addOptional(YangStmtMapping.REFERENCE)
60             .build();
61
62     IncludeStatementSupport(final YangParserConfiguration config, final SubstatementValidator validator) {
63         super(YangStmtMapping.INCLUDE, StatementPolicy.reject(), config, validator);
64     }
65
66     public static @NonNull IncludeStatementSupport rfc6020Instance(final YangParserConfiguration config) {
67         return new IncludeStatementSupport(config, RFC6020_VALIDATOR);
68     }
69
70     public static @NonNull IncludeStatementSupport rfc7950Instance(final YangParserConfiguration config) {
71         return new IncludeStatementSupport(config, RFC7950_VALIDATOR);
72     }
73
74     @Override
75     public void onPreLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
76         final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt,
77             RevisionDateStatement.class);
78         stmt.addRequiredSource(revision == null ? RevisionSourceIdentifier.create(stmt.argument())
79             : RevisionSourceIdentifier.create(stmt.argument(), revision.argument()));
80     }
81
82     @Override
83     public void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
84         final String submoduleName = stmt.getArgument();
85         final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, 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                 RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.argument())), SOURCE_LINKAGE);
95         }
96
97         includeAction.apply(new InferenceAction() {
98             @Override
99             public void apply(final InferenceContext ctx) {
100                 final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
101                 final YangVersion modVersion = stmt.getRoot().yangVersion();
102                 final YangVersion subVersion = includedSubModuleContext.yangVersion();
103                 if (subVersion != modVersion) {
104                     throw new YangVersionLinkageException(stmt,
105                         "Cannot include a version %s submodule in a version %s module", subVersion, modVersion);
106                 }
107
108                 stmt.addToNs(IncludedModuleContext.class, revision != null
109                         ? RevisionSourceIdentifier.create(submoduleName, revision.argument())
110                                 : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
111                 stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.argument(), includedSubModuleContext);
112             }
113
114             @Override
115             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
116                 InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), stmt,
117                     "Included submodule '%s' was not found: ", stmt.argument());
118             }
119         });
120     }
121
122     @Override
123     protected IncludeStatement createDeclared(final StmtContext<String, IncludeStatement, ?> ctx,
124             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
125         return DeclaredStatements.createInclude(ctx.getRawArgument(), ctx.getArgument(), substatements);
126     }
127
128     @Override
129     protected IncludeStatement attachDeclarationReference(final IncludeStatement stmt,
130             final DeclarationReference reference) {
131         return DeclaredStatementDecorators.decorateInclude(stmt, reference);
132     }
133
134     @Override
135     protected IncludeEffectiveStatement createEffective(final Current<String, IncludeStatement> stmt,
136             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
137         return EffectiveStatements.createInclude(stmt.declared(), substatements);
138     }
139 }