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