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.SEMANTIC_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) {
86 final String moduleName = stmt.getStatementArgument();
87 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
88 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
89 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
90 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
91 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
93 importAction.apply(new InferenceAction() {
96 StmtContext<?, ?, ?> importedModuleContext = imported.get();
97 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
98 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
100 Verify.verifyNotNull(importedModuleNamespace);
101 final String impPrefix = SourceException.throwIfNull(
102 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
103 stmt.getStatementSourceReference(), "Missing prefix statement");
105 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
109 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
110 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
111 "Imported module [%s] was not found.", moduleName);
117 public void onLinkageDeclared(
118 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
119 if (stmt.isEnabledSemanticVersioning()) {
120 SemanticVersionImport.onLinkageDeclared(stmt);
122 RevisionImport.onLinkageDeclared(stmt);
127 protected SubstatementValidator getSubstatementValidator() {
128 return SUBSTATEMENT_VALIDATOR;
131 private static class RevisionImport {
133 private RevisionImport() {
134 throw new UnsupportedOperationException("Utility class");
137 private static void onLinkageDeclared(
138 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
139 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
140 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
141 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
142 impIdentifier, SOURCE_LINKAGE);
143 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
144 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
146 importAction.apply(new InferenceAction() {
148 public void apply() {
149 StmtContext<?, ?, ?> importedModule = null;
150 ModuleIdentifier importedModuleIdentifier = null;
151 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
152 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
153 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
154 if (recentModuleEntry != null) {
155 importedModuleIdentifier = recentModuleEntry.getKey();
156 importedModule = recentModuleEntry.getValue();
160 if (importedModule == null || importedModuleIdentifier == null) {
161 importedModule = imported.get();
162 importedModuleIdentifier = impIdentifier;
165 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
166 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
167 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
169 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
170 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
174 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
175 if (failed.contains(imported)) {
176 throw new InferenceException(stmt.getStatementSourceReference(),
177 "Imported module [%s] was not found.", impIdentifier);
184 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
185 final ModuleIdentifier impIdentifier,
186 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
188 ModuleIdentifier recentModuleIdentifier = impIdentifier;
189 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
191 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
193 final ModuleIdentifier id = moduleEntry.getKey();
195 if (id.getName().equals(impIdentifier.getName())
196 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
197 recentModuleIdentifier = id;
198 recentModuleEntry = moduleEntry;
202 return recentModuleEntry;
205 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
206 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
207 if (revision == null) {
208 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
211 return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
215 private static class SemanticVersionImport {
216 private SemanticVersionImport() {
217 throw new UnsupportedOperationException("Utility class");
220 private static void onLinkageDeclared(
221 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
222 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
223 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
224 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
225 impIdentifier, SOURCE_LINKAGE);
226 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
227 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
229 importAction.apply(new InferenceAction() {
231 public void apply() {
232 Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
233 impIdentifier.getName(), stmt);
235 StmtContext<?, ?, ?> importedModule = null;
236 ModuleIdentifier importedModuleIdentifier = null;
237 ModuleIdentifier semVerModuleIdentifier = null;
238 if (importedModuleEntry != null) {
239 importedModule = importedModuleEntry.getValue();
240 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
241 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
243 throw new InferenceException(stmt.getStatementSourceReference(),
244 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
245 .getName(), getRequestedImportVersion(stmt));
248 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
249 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
250 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
251 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
253 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
254 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
258 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
259 if (failed.contains(imported)) {
260 throw new InferenceException(stmt.getStatementSourceReference(),
261 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
262 .getName(), getRequestedImportVersion(stmt));
268 private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
269 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
270 if (requestedImportVersion == null) {
271 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
273 return requestedImportVersion;
276 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
277 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
278 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
279 SemanticVersionModuleNamespace.class, moduleName);
280 if (allRelevantModulesMap == null) {
284 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
285 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
286 SemVer.create(requestedImportVersion.getMajor() + 1), false);
287 if (!allRelevantModulesMap.isEmpty()) {
288 return allRelevantModulesMap.lastEntry();
294 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
295 return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
296 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
299 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
300 final SemVer semVer) {
301 return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
302 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
303 Optional.of(importedModuleIdentifier.getRevision()), semVer);