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.Prerequisite;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
46 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
47 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
48 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToSemVerModuleIdentifier;
49 import org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleIdentifier;
51 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
52 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
54 public class ImportStatementDefinition extends
55 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
56 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
57 .builder(YangStmtMapping.IMPORT)
58 .addMandatory(YangStmtMapping.PREFIX)
59 .addOptional(YangStmtMapping.REVISION_DATE)
60 .addOptional(SupportedExtensionsMapping.SEMANTIC_VERSION)
63 public ImportStatementDefinition() {
64 super(YangStmtMapping.IMPORT);
68 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
73 public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
74 return new ImportStatementImpl(ctx);
78 public EffectiveStatement<String, ImportStatement> createEffective(
79 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
80 return new ImportEffectiveStatementImpl(ctx);
84 public void onFullDefinitionDeclared(
85 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
86 super.onFullDefinitionDeclared(stmt);
87 SUBSTATEMENT_VALIDATOR.validate(stmt);
91 public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
92 final String moduleName = stmt.getStatementArgument();
93 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
94 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
95 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
96 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
97 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
99 importAction.apply(new InferenceAction() {
101 public void apply() {
102 StmtContext<?, ?, ?> importedModuleContext = imported.get();
103 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
104 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
106 Verify.verifyNotNull(importedModuleNamespace);
107 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
108 Verify.verifyNotNull(impPrefix);
109 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
113 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
114 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
115 "Imported module [%s] was not found.", moduleName);
121 public void onLinkageDeclared(
122 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
123 if (stmt.isEnabledSemanticVersioning()) {
124 SemanticVersionImport.onLinkageDeclared(stmt);
126 RevisionImport.onLinkageDeclared(stmt);
130 private static class RevisionImport {
132 private RevisionImport() {
133 throw new UnsupportedOperationException("Utility class");
136 private static void onLinkageDeclared(
137 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
138 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
139 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
140 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
141 impIdentifier, SOURCE_LINKAGE);
142 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
143 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
145 importAction.apply(new InferenceAction() {
147 public void apply() {
148 StmtContext<?, ?, ?> importedModule = null;
149 ModuleIdentifier importedModuleIdentifier = null;
150 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
151 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
152 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
153 if (recentModuleEntry != null) {
154 importedModuleIdentifier = recentModuleEntry.getKey();
155 importedModule = recentModuleEntry.getValue();
159 if (importedModule == null || importedModuleIdentifier == null) {
160 importedModule = imported.get();
161 importedModuleIdentifier = impIdentifier;
164 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
165 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
166 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
168 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
169 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
173 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
174 if (failed.contains(imported)) {
175 throw new InferenceException(stmt.getStatementSourceReference(),
176 "Imported module [%s] was not found.", impIdentifier);
183 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
184 final ModuleIdentifier impIdentifier,
185 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
187 ModuleIdentifier recentModuleIdentifier = impIdentifier;
188 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
190 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
192 final ModuleIdentifier id = moduleEntry.getKey();
194 if (id.getName().equals(impIdentifier.getName())
195 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
196 recentModuleIdentifier = id;
197 recentModuleEntry = moduleEntry;
201 return recentModuleEntry;
204 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
205 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
206 if (revision == null) {
207 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
210 return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
214 private static class SemanticVersionImport {
215 private SemanticVersionImport() {
216 throw new UnsupportedOperationException("Utility class");
219 private static void onLinkageDeclared(
220 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
221 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
222 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
223 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
224 impIdentifier, SOURCE_LINKAGE);
225 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
226 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
228 importAction.apply(new InferenceAction() {
230 public void apply() {
231 Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
232 impIdentifier.getName(), stmt);
234 StmtContext<?, ?, ?> importedModule = null;
235 ModuleIdentifier importedModuleIdentifier = null;
236 ModuleIdentifier semVerModuleIdentifier = null;
237 if (importedModuleEntry != null) {
238 importedModule = importedModuleEntry.getValue();
239 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
240 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
242 throw new InferenceException(stmt.getStatementSourceReference(),
243 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
244 .getName(), getRequestedImportVersion(stmt));
247 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
248 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
249 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
250 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
252 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
253 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
257 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
258 if (failed.contains(imported)) {
259 throw new InferenceException(stmt.getStatementSourceReference(),
260 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
261 .getName(), getRequestedImportVersion(stmt));
267 private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
268 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
269 if (requestedImportVersion == null) {
270 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
272 return requestedImportVersion;
275 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
276 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
277 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
278 SemanticVersionModuleNamespace.class, moduleName);
279 if (allRelevantModulesMap == null) {
283 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
284 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
285 SemVer.create(requestedImportVersion.getMajor() + 1), false);
286 if (!allRelevantModulesMap.isEmpty()) {
287 return allRelevantModulesMap.lastEntry();
293 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
294 return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
295 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
298 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
299 final SemVer semVer) {
300 return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
301 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
302 Optional.of(importedModuleIdentifier.getRevision()), semVer);