2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.import_;
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;
14 import com.google.common.base.Verify;
15 import com.google.common.collect.ImmutableList;
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;
46 abstract class AbstractImportStatementSupport
47 extends BaseStringStatementSupport<ImportStatement, ImportEffectiveStatement> {
48 AbstractImportStatementSupport() {
49 super(YangStmtMapping.IMPORT);
53 public final void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
55 * Add ModuleIdentifier of a module which is required by this module.
56 * Based on this information, required modules are searched from library
59 stmt.addRequiredSource(RevisionImport.getImportedSourceIdentifier(stmt));
61 final String moduleName = stmt.coerceStatementArgument();
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);
67 importAction.apply(new InferenceAction() {
69 public void apply(final InferenceContext ctx) {
70 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
71 Verify.verify(moduleName.equals(importedModuleContext.coerceRawStatementArgument()));
72 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
74 Verify.verifyNotNull(importedModuleNamespace);
75 final String impPrefix = SourceException.throwIfNull(
76 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
77 stmt.getStatementSourceReference(), "Missing prefix statement");
79 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
83 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
84 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
85 "Imported module [%s] was not found.", moduleName);
91 public final void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
92 if (stmt.isEnabledSemanticVersioning()) {
93 SemanticVersionImport.onLinkageDeclared(stmt);
95 RevisionImport.onLinkageDeclared(stmt);
100 protected final ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx,
101 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
102 return new ImportStatementImpl(ctx.coerceRawStatementArgument(), substatements);
106 protected final ImportStatement createEmptyDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
107 throw new IllegalStateException("Unexpected empty declared import statement");
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");
115 final ImportStatement declared = stmt.declared();
116 final String prefix = declared.getPrefix().getValue();
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)
124 .map(subStmt -> ((RevisionDateEffectiveStatement) subStmt).argument());
125 revision = optRev.isPresent() ? optRev.get() : getImportedRevision(rabbit, declared.getModule(), prefix);
128 final SemVerSourceIdentifier importedModuleIdentifier = stmt.getFromNamespace(
129 ImportPrefixToSemVerSourceIdentifier.class, prefix);
130 revision = importedModuleIdentifier.getRevision().orElse(null);
131 semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
134 return new ImportEffectiveStatementImpl(declared, substatements, revision, semVer);
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
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);