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.Optional;
15 import com.google.common.base.Verify;
17 import java.util.Collection;
18 import java.util.Date;
20 import java.util.Map.Entry;
21 import java.util.NavigableMap;
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.Rfc6020Mapping;
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.parser.builder.impl.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.ModuleCtxToModuleIdentifier;
50 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
51 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
53 public class ImportStatementDefinition extends
54 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
55 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
56 .builder(Rfc6020Mapping.IMPORT)
57 .addMandatory(Rfc6020Mapping.PREFIX)
58 .addOptional(Rfc6020Mapping.REVISION_DATE)
59 .addOptional(SupportedExtensionsMapping.SEMANTIC_VERSION)
62 public ImportStatementDefinition() {
63 super(Rfc6020Mapping.IMPORT);
67 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
72 public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
73 return new ImportStatementImpl(ctx);
77 public EffectiveStatement<String, ImportStatement> createEffective(
78 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
79 return new ImportEffectiveStatementImpl(ctx);
83 public void onFullDefinitionDeclared(
84 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
85 super.onFullDefinitionDeclared(stmt);
86 SUBSTATEMENT_VALIDATOR.validate(stmt);
90 public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
91 final String moduleName = stmt.getStatementArgument();
92 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
93 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
94 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
95 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
96 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
98 importAction.apply(new InferenceAction() {
100 public void apply() {
101 StmtContext<?, ?, ?> importedModuleContext = imported.get();
102 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
103 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
105 Verify.verifyNotNull(importedModuleNamespace);
106 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
107 Verify.verifyNotNull(impPrefix);
108 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
112 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
113 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
114 "Imported module [%s] was not found.", moduleName);
120 public void onLinkageDeclared(
121 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
122 if (stmt.isEnabledSemanticVersioning()) {
123 SemanticVersionImport.onLinkageDeclared(stmt);
125 RevisionImport.onLinkageDeclared(stmt);
129 private static class RevisionImport {
131 private RevisionImport() {
132 throw new UnsupportedOperationException("Utility class");
135 private static void onLinkageDeclared(
136 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
137 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
138 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
139 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
140 impIdentifier, SOURCE_LINKAGE);
141 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
142 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
144 importAction.apply(new InferenceAction() {
146 public void apply() {
147 StmtContext<?, ?, ?> importedModule = null;
148 ModuleIdentifier importedModuleIdentifier = null;
149 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
150 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
151 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
152 if (recentModuleEntry != null) {
153 importedModuleIdentifier = recentModuleEntry.getKey();
154 importedModule = recentModuleEntry.getValue();
158 if (importedModule == null || importedModuleIdentifier == null) {
159 importedModule = imported.get();
160 importedModuleIdentifier = impIdentifier;
163 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
164 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
165 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
167 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
168 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
172 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
173 if (failed.contains(imported)) {
174 throw new InferenceException(stmt.getStatementSourceReference(),
175 "Imported module [%s] was not found.", impIdentifier);
182 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
183 final ModuleIdentifier impIdentifier,
184 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
186 ModuleIdentifier recentModuleIdentifier = impIdentifier;
187 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
189 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
191 final ModuleIdentifier id = moduleEntry.getKey();
193 if (id.getName().equals(impIdentifier.getName())
194 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
195 recentModuleIdentifier = id;
196 recentModuleEntry = moduleEntry;
200 return recentModuleEntry;
203 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
204 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
205 if (revision == null) {
206 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
209 return new ModuleIdentifierImpl(stmt.getStatementArgument(), Optional.absent(),
210 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 new ModuleIdentifierImpl(impStmt.getStatementArgument(), Optional.absent(),
295 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
298 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
299 final SemVer semVer) {
300 return new ModuleIdentifierImpl(importedModuleIdentifier.getName(), Optional.fromNullable(importedModuleIdentifier
301 .getNamespace()), Optional.of(importedModuleIdentifier.getRevision()), semVer);