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 onFullDefinitionDeclared(
86 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
87 super.onFullDefinitionDeclared(stmt);
88 SUBSTATEMENT_VALIDATOR.validate(stmt);
92 public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> 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 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);
133 private static class RevisionImport {
135 private RevisionImport() {
136 throw new UnsupportedOperationException("Utility class");
139 private static void onLinkageDeclared(
140 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
141 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
142 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
143 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
144 impIdentifier, SOURCE_LINKAGE);
145 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
146 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
148 importAction.apply(new InferenceAction() {
150 public void apply() {
151 StmtContext<?, ?, ?> importedModule = null;
152 ModuleIdentifier importedModuleIdentifier = null;
153 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
154 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
155 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
156 if (recentModuleEntry != null) {
157 importedModuleIdentifier = recentModuleEntry.getKey();
158 importedModule = recentModuleEntry.getValue();
162 if (importedModule == null || importedModuleIdentifier == null) {
163 importedModule = imported.get();
164 importedModuleIdentifier = impIdentifier;
167 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
168 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
169 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
171 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
172 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
176 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
177 if (failed.contains(imported)) {
178 throw new InferenceException(stmt.getStatementSourceReference(),
179 "Imported module [%s] was not found.", impIdentifier);
186 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
187 final ModuleIdentifier impIdentifier,
188 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
190 ModuleIdentifier recentModuleIdentifier = impIdentifier;
191 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
193 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
195 final ModuleIdentifier id = moduleEntry.getKey();
197 if (id.getName().equals(impIdentifier.getName())
198 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
199 recentModuleIdentifier = id;
200 recentModuleEntry = moduleEntry;
204 return recentModuleEntry;
207 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
208 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
209 if (revision == null) {
210 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
213 return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
217 private static class SemanticVersionImport {
218 private SemanticVersionImport() {
219 throw new UnsupportedOperationException("Utility class");
222 private static void onLinkageDeclared(
223 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
224 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
225 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
226 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
227 impIdentifier, SOURCE_LINKAGE);
228 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
229 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
231 importAction.apply(new InferenceAction() {
233 public void apply() {
234 Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
235 impIdentifier.getName(), stmt);
237 StmtContext<?, ?, ?> importedModule = null;
238 ModuleIdentifier importedModuleIdentifier = null;
239 ModuleIdentifier semVerModuleIdentifier = null;
240 if (importedModuleEntry != null) {
241 importedModule = importedModuleEntry.getValue();
242 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
243 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
245 throw new InferenceException(stmt.getStatementSourceReference(),
246 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
247 .getName(), getRequestedImportVersion(stmt));
250 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
251 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
252 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
253 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
255 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
256 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
260 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
261 if (failed.contains(imported)) {
262 throw new InferenceException(stmt.getStatementSourceReference(),
263 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
264 .getName(), getRequestedImportVersion(stmt));
270 private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
271 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
272 if (requestedImportVersion == null) {
273 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
275 return requestedImportVersion;
278 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
279 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
280 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
281 SemanticVersionModuleNamespace.class, moduleName);
282 if (allRelevantModulesMap == null) {
286 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
287 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
288 SemVer.create(requestedImportVersion.getMajor() + 1), false);
289 if (!allRelevantModulesMap.isEmpty()) {
290 return allRelevantModulesMap.lastEntry();
296 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
297 return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
298 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
301 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
302 final SemVer semVer) {
303 return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
304 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
305 Optional.of(importedModuleIdentifier.getRevision()), semVer);