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.spi.source.SourceException;
53 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
55 public class ImportStatementDefinition extends
56 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
57 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
58 .builder(YangStmtMapping.IMPORT)
59 .addMandatory(YangStmtMapping.PREFIX)
60 .addOptional(YangStmtMapping.REVISION_DATE)
61 .addOptional(SupportedExtensionsMapping.OPENCONFIG_VERSION)
64 public ImportStatementDefinition() {
65 super(YangStmtMapping.IMPORT);
69 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
74 public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
75 return new ImportStatementImpl(ctx);
79 public EffectiveStatement<String, ImportStatement> createEffective(
80 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
81 return new ImportEffectiveStatementImpl(ctx);
85 public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
87 * Add ModuleIdentifier of a module which is required by this module.
88 * Based on this information, required modules are searched from library
91 stmt.addRequiredModule(RevisionImport.getImportedModuleIdentifier(stmt));
93 final String moduleName = stmt.getStatementArgument();
94 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
95 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
96 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
97 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
98 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
100 importAction.apply(new InferenceAction() {
102 public void apply() {
103 final StmtContext<?, ?, ?> importedModuleContext = imported.get();
104 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
105 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
107 Verify.verifyNotNull(importedModuleNamespace);
108 final String impPrefix = SourceException.throwIfNull(
109 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
110 stmt.getStatementSourceReference(), "Missing prefix statement");
112 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
116 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
117 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
118 "Imported module [%s] was not found.", moduleName);
124 public void onLinkageDeclared(
125 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
126 if (stmt.isEnabledSemanticVersioning()) {
127 SemanticVersionImport.onLinkageDeclared(stmt);
129 RevisionImport.onLinkageDeclared(stmt);
134 protected SubstatementValidator getSubstatementValidator() {
135 return SUBSTATEMENT_VALIDATOR;
138 private static class RevisionImport {
140 private RevisionImport() {
141 throw new UnsupportedOperationException("Utility class");
144 private static void onLinkageDeclared(
145 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
146 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
147 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
148 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
149 impIdentifier, SOURCE_LINKAGE);
150 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
151 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
153 importAction.apply(new InferenceAction() {
155 public void apply() {
156 StmtContext<?, ?, ?> importedModule = null;
157 ModuleIdentifier importedModuleIdentifier = null;
158 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
159 final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
160 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
161 if (recentModuleEntry != null) {
162 importedModuleIdentifier = recentModuleEntry.getKey();
163 importedModule = recentModuleEntry.getValue();
167 if (importedModule == null || importedModuleIdentifier == null) {
168 importedModule = imported.get();
169 importedModuleIdentifier = impIdentifier;
172 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
173 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
174 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
176 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
177 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
181 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
182 if (failed.contains(imported)) {
183 throw new InferenceException(stmt.getStatementSourceReference(),
184 "Imported module [%s] was not found.", impIdentifier);
191 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
192 final ModuleIdentifier impIdentifier,
193 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
195 ModuleIdentifier recentModuleIdentifier = impIdentifier;
196 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
198 for (final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
200 final ModuleIdentifier id = moduleEntry.getKey();
202 if (id.getName().equals(impIdentifier.getName())
203 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
204 recentModuleIdentifier = id;
205 recentModuleEntry = moduleEntry;
209 return recentModuleEntry;
212 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
213 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
214 if (revision == null) {
215 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
218 return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
222 private static class SemanticVersionImport {
223 private SemanticVersionImport() {
224 throw new UnsupportedOperationException("Utility class");
227 private static void onLinkageDeclared(
228 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
229 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
230 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
231 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
232 impIdentifier, SOURCE_LINKAGE);
233 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
234 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
236 importAction.apply(new InferenceAction() {
238 public void apply() {
239 final Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
240 impIdentifier.getName(), stmt);
242 StmtContext<?, ?, ?> importedModule = null;
243 ModuleIdentifier importedModuleIdentifier = null;
244 ModuleIdentifier semVerModuleIdentifier = null;
245 if (importedModuleEntry != null) {
246 importedModule = importedModuleEntry.getValue();
247 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
248 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
250 throw new InferenceException(stmt.getStatementSourceReference(),
251 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
252 .getName(), getRequestedImportVersion(stmt));
255 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
256 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
257 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
258 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
260 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
261 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
265 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
266 if (failed.contains(imported)) {
267 throw new InferenceException(stmt.getStatementSourceReference(),
268 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
269 .getName(), getRequestedImportVersion(stmt));
275 private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
276 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
277 if (requestedImportVersion == null) {
278 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
280 return requestedImportVersion;
283 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
284 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
285 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
286 SemanticVersionModuleNamespace.class, moduleName);
287 if (allRelevantModulesMap == null) {
291 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
292 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
293 SemVer.create(requestedImportVersion.getMajor() + 1), false);
294 if (!allRelevantModulesMap.isEmpty()) {
295 return allRelevantModulesMap.lastEntry();
301 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
302 return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
303 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
306 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
307 final SemVer semVer) {
308 return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
309 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
310 Optional.of(importedModuleIdentifier.getRevision()), semVer);