Simplify Module/Submodule statement argument usage
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / import_ / AbstractImportStatementSupport.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.rfc7950.stmt.import_;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_PRE_LINKAGE;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
12
13 import com.google.common.base.Verify;
14 import com.google.common.collect.ImmutableList;
15 import java.net.URI;
16 import java.util.Collection;
17 import java.util.Optional;
18 import org.opendaylight.yangtools.concepts.SemVer;
19 import org.opendaylight.yangtools.yang.common.QNameModule;
20 import org.opendaylight.yangtools.yang.common.Revision;
21 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.ImportEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStringStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
39 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
40 import org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier;
41 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43
44 abstract class AbstractImportStatementSupport
45         extends BaseStringStatementSupport<ImportStatement, ImportEffectiveStatement> {
46     AbstractImportStatementSupport() {
47         super(YangStmtMapping.IMPORT);
48     }
49
50     @Override
51     public final void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
52         /*
53          * Add ModuleIdentifier of a module which is required by this module.
54          * Based on this information, required modules are searched from library
55          * sources.
56          */
57         stmt.addRequiredSource(RevisionImport.getImportedSourceIdentifier(stmt));
58
59         final String moduleName = stmt.coerceStatementArgument();
60         final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
61         final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
62                 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
63         importAction.mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
64
65         importAction.apply(new InferenceAction() {
66             @Override
67             public void apply(final InferenceContext ctx) {
68                 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
69                 Verify.verify(moduleName.equals(importedModuleContext.coerceRawStatementArgument()));
70                 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
71                         moduleName);
72                 Verify.verifyNotNull(importedModuleNamespace);
73                 final String impPrefix = SourceException.throwIfNull(
74                     firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
75                     stmt.getStatementSourceReference(), "Missing prefix statement");
76
77                 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
78             }
79
80             @Override
81             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
82                 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
83                         "Imported module [%s] was not found.", moduleName);
84             }
85         });
86     }
87
88     @Override
89     public final void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
90         if (stmt.isEnabledSemanticVersioning()) {
91             SemanticVersionImport.onLinkageDeclared(stmt);
92         } else {
93             RevisionImport.onLinkageDeclared(stmt);
94         }
95     }
96
97     @Override
98     protected final ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx,
99             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
100         return new ImportStatementImpl(ctx, substatements);
101     }
102
103     @Override
104     protected final ImportStatement createEmptyDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
105         throw new IllegalStateException("Unexpected empty declared import statement");
106     }
107
108     @Override
109     protected final ImportEffectiveStatement createEffective(
110             final StmtContext<String, ImportStatement, ImportEffectiveStatement> ctx, final ImportStatement declared,
111             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
112
113         final String prefix = declared.getPrefix().getValue();
114         final SemVer semVer;
115         final Revision revision;
116         if (!ctx.isEnabledSemanticVersioning()) {
117             final Optional<Revision> optRev = substatements.stream()
118                     .filter(RevisionDateEffectiveStatement.class::isInstance)
119                     .findFirst()
120                     .map(stmt -> ((RevisionDateEffectiveStatement) stmt).argument());
121             revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx, declared.getModule(), prefix);
122             semVer = null;
123         } else {
124             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
125                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
126             revision = importedModuleIdentifier.getRevision().orElse(null);
127             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
128         }
129
130         return new ImportEffectiveStatementImpl(declared, substatements, revision, semVer);
131     }
132
133     @Override
134     protected final ImportEffectiveStatement createEmptyEffective(
135             final StmtContext<String, ImportStatement, ImportEffectiveStatement> ctx, final ImportStatement declared) {
136         throw new IllegalStateException("Unexpected empty effective import statement");
137     }
138
139     private static Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx,
140             final String moduleName, final String prefix) {
141         // When 'revision-date' of an import is not specified in yang source, we need to find revision of imported
142         // module.
143         final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
144         SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
145                 "Unable to find import of module %s with prefix %s.", moduleName, prefix);
146         return importedModule.getRevision().orElse(null);
147     }
148 }