Add BaseStringStatementSupport.parseArgument()
[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         final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
64                 .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                 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
71                 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
72                         moduleName);
73                 Verify.verifyNotNull(importedModuleNamespace);
74                 final String impPrefix = SourceException.throwIfNull(
75                     firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
76                     stmt.getStatementSourceReference(), "Missing prefix statement");
77
78                 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
79             }
80
81             @Override
82             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
83                 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
84                         "Imported module [%s] was not found.", moduleName);
85             }
86         });
87     }
88
89     @Override
90     public final void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
91         if (stmt.isEnabledSemanticVersioning()) {
92             SemanticVersionImport.onLinkageDeclared(stmt);
93         } else {
94             RevisionImport.onLinkageDeclared(stmt);
95         }
96     }
97
98     @Override
99     protected final ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx,
100             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
101         return new ImportStatementImpl(ctx, substatements);
102     }
103
104     @Override
105     protected final ImportStatement createEmptyDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
106         throw new IllegalStateException("Unexpected empty declared import statement");
107     }
108
109     @Override
110     protected final ImportEffectiveStatement createEffective(
111             final StmtContext<String, ImportStatement, ImportEffectiveStatement> ctx, final ImportStatement declared,
112             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
113
114         final String prefix = declared.getPrefix().getValue();
115         final SemVer semVer;
116         final Revision revision;
117         if (!ctx.isEnabledSemanticVersioning()) {
118             final Optional<Revision> optRev = substatements.stream()
119                     .filter(RevisionDateEffectiveStatement.class::isInstance)
120                     .findFirst()
121                     .map(stmt -> ((RevisionDateEffectiveStatement) stmt).argument());
122             revision = optRev.isPresent() ? optRev.get() : getImportedRevision(ctx, declared.getModule(), prefix);
123             semVer = null;
124         } else {
125             final SemVerSourceIdentifier importedModuleIdentifier = ctx.getFromNamespace(
126                 ImportPrefixToSemVerSourceIdentifier.class, prefix);
127             revision = importedModuleIdentifier.getRevision().orElse(null);
128             semVer = importedModuleIdentifier.getSemanticVersion().orElse(null);
129         }
130
131         return new ImportEffectiveStatementImpl(declared, substatements, revision, semVer);
132     }
133
134     @Override
135     protected final ImportEffectiveStatement createEmptyEffective(
136             final StmtContext<String, ImportStatement, ImportEffectiveStatement> ctx, final ImportStatement declared) {
137         throw new IllegalStateException("Unexpected empty effective import statement");
138     }
139
140     private static Revision getImportedRevision(final StmtContext<String, ImportStatement, ?> ctx,
141             final String moduleName, final String prefix) {
142         // When 'revision-date' of an import is not specified in yang source, we need to find revision of imported
143         // module.
144         final QNameModule importedModule = StmtContextUtils.getModuleQNameByPrefix(ctx, prefix);
145         SourceException.throwIfNull(importedModule, ctx.getStatementSourceReference(),
146                 "Unable to find import of module %s with prefix %s.", moduleName, prefix);
147         return importedModule.getRevision().orElse(null);
148     }
149 }