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