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.stmt.rfc6020;
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_LINKAGE;
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;
16 import java.util.Collection;
17 import java.util.Date;
19 import java.util.Map.Entry;
20 import java.util.NavigableMap;
21 import java.util.Optional;
22 import org.opendaylight.yangtools.concepts.SemVer;
23 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
27 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.NamespaceStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
32 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
33 import org.opendaylight.yangtools.yang.model.util.ModuleIdentifierImpl;
34 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
35 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
36 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
46 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
47 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToSemVerModuleIdentifier;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleIdentifier;
52 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
53 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
54 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
56 public class ImportStatementDefinition extends
57 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
58 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
59 .builder(YangStmtMapping.IMPORT)
60 .addMandatory(YangStmtMapping.PREFIX)
61 .addOptional(YangStmtMapping.REVISION_DATE)
62 .addOptional(SupportedExtensionsMapping.OPENCONFIG_VERSION)
65 public ImportStatementDefinition() {
66 super(YangStmtMapping.IMPORT);
70 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
75 public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
76 return new ImportStatementImpl(ctx);
80 public EffectiveStatement<String, ImportStatement> createEffective(
81 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
82 return new ImportEffectiveStatementImpl(ctx);
86 public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
88 * Add ModuleIdentifier of a module which is required by this module.
89 * Based on this information, required modules are searched from library
92 stmt.addRequiredModule(RevisionImport.getImportedModuleIdentifier(stmt));
94 final String moduleName = stmt.getStatementArgument();
95 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
96 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
97 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
98 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
99 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
101 importAction.apply(new InferenceAction() {
103 public void apply(final InferenceContext ctx) {
104 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
105 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
106 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
108 Verify.verifyNotNull(importedModuleNamespace);
109 final String impPrefix = SourceException.throwIfNull(
110 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
111 stmt.getStatementSourceReference(), "Missing prefix statement");
113 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
117 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
118 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
119 "Imported module [%s] was not found.", moduleName);
125 public void onLinkageDeclared(
126 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
127 if (stmt.isEnabledSemanticVersioning()) {
128 SemanticVersionImport.onLinkageDeclared(stmt);
130 RevisionImport.onLinkageDeclared(stmt);
135 protected SubstatementValidator getSubstatementValidator() {
136 return SUBSTATEMENT_VALIDATOR;
139 private static class RevisionImport {
141 private RevisionImport() {
142 throw new UnsupportedOperationException("Utility class");
145 private static void onLinkageDeclared(
146 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
147 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
148 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
149 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
150 impIdentifier, SOURCE_LINKAGE);
151 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
152 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
154 importAction.apply(new InferenceAction() {
156 public void apply(final InferenceContext ctx) {
157 StmtContext<?, ?, ?> importedModule = null;
158 ModuleIdentifier importedModuleIdentifier = null;
159 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
160 final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
161 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
162 if (recentModuleEntry != null) {
163 importedModuleIdentifier = recentModuleEntry.getKey();
164 importedModule = recentModuleEntry.getValue();
168 if (importedModule == null || importedModuleIdentifier == null) {
169 importedModule = imported.resolve(ctx);
170 importedModuleIdentifier = impIdentifier;
173 linkageTarget.resolve(ctx).addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
174 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
175 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
177 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
178 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
182 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
183 if (failed.contains(imported)) {
184 throw new InferenceException(stmt.getStatementSourceReference(),
185 "Imported module [%s] was not found.", impIdentifier);
192 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
193 final ModuleIdentifier impIdentifier,
194 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
196 ModuleIdentifier recentModuleIdentifier = impIdentifier;
197 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
199 for (final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
201 final ModuleIdentifier id = moduleEntry.getKey();
203 if (id.getName().equals(impIdentifier.getName())
204 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
205 recentModuleIdentifier = id;
206 recentModuleEntry = moduleEntry;
210 return recentModuleEntry;
213 private static ModuleIdentifier getImportedModuleIdentifier(final StmtContext<String, ImportStatement, ?> stmt) {
214 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
215 if (revision == null) {
216 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
219 return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
223 private static class SemanticVersionImport {
224 private SemanticVersionImport() {
225 throw new UnsupportedOperationException("Utility class");
228 private static void onLinkageDeclared(
229 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
230 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
231 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
232 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
233 impIdentifier, SOURCE_LINKAGE);
234 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
235 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
237 importAction.apply(new InferenceAction() {
239 public void apply(final InferenceContext ctx) {
240 final Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
241 impIdentifier.getName(), stmt);
243 StmtContext<?, ?, ?> importedModule = null;
244 ModuleIdentifier importedModuleIdentifier = null;
245 ModuleIdentifier semVerModuleIdentifier = null;
246 if (importedModuleEntry != null) {
247 importedModule = importedModuleEntry.getValue();
248 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
249 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
251 throw new InferenceException(stmt.getStatementSourceReference(),
252 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
253 .getName(), getRequestedImportVersion(stmt));
256 linkageTarget.resolve(ctx).addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
257 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
258 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
259 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
261 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
262 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
266 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
267 if (failed.contains(imported)) {
268 throw new InferenceException(stmt.getStatementSourceReference(),
269 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
270 .getName(), getRequestedImportVersion(stmt));
276 private static SemVer getRequestedImportVersion(final StmtContext<?, ?, ?> impStmt) {
277 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
278 if (requestedImportVersion == null) {
279 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
281 return requestedImportVersion;
284 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
285 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
286 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
287 SemanticVersionModuleNamespace.class, moduleName);
288 if (allRelevantModulesMap == null) {
292 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
293 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
294 SemVer.create(requestedImportVersion.getMajor() + 1), false);
295 if (!allRelevantModulesMap.isEmpty()) {
296 return allRelevantModulesMap.lastEntry();
302 private static ModuleIdentifier getImportedModuleIdentifier(final StmtContext<String, ImportStatement, ?> impStmt) {
303 return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
304 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
307 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
308 final SemVer semVer) {
309 return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
310 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
311 Optional.of(importedModuleIdentifier.getRevision()), semVer);