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).add(Rfc6020Mapping.PREFIX, 1, 1).add(Rfc6020Mapping.REVISION_DATE, 0, 1)
57 .add(SupportedExtensionsMapping.SEMANTIC_VERSION, 0, 1).build();
59 public ImportStatementDefinition() {
60 super(Rfc6020Mapping.IMPORT);
64 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
69 public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
70 return new ImportStatementImpl(ctx);
74 public EffectiveStatement<String, ImportStatement> createEffective(
75 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
76 return new ImportEffectiveStatementImpl(ctx);
80 public void onFullDefinitionDeclared(
81 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
82 super.onFullDefinitionDeclared(stmt);
83 SUBSTATEMENT_VALIDATOR.validate(stmt);
87 public void onPreLinkageDeclared(Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
88 final String moduleName = stmt.getStatementArgument();
89 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
90 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
91 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
92 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
93 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
95 importAction.apply(new InferenceAction() {
98 StmtContext<?, ?, ?> importedModuleContext = imported.get();
99 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
100 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
102 Verify.verifyNotNull(importedModuleNamespace);
103 final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
104 Verify.verifyNotNull(impPrefix);
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);
126 private static class RevisionImport {
128 private RevisionImport() {
129 throw new UnsupportedOperationException("Utility class");
132 private static void onLinkageDeclared(
133 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
134 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
135 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
136 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
137 impIdentifier, SOURCE_LINKAGE);
138 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
139 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
141 importAction.apply(new InferenceAction() {
143 public void apply() {
144 StmtContext<?, ?, ?> importedModule = null;
145 ModuleIdentifier importedModuleIdentifier = null;
146 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
147 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
148 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
149 if (recentModuleEntry != null) {
150 importedModuleIdentifier = recentModuleEntry.getKey();
151 importedModule = recentModuleEntry.getValue();
155 if (importedModule == null || importedModuleIdentifier == null) {
156 importedModule = imported.get();
157 importedModuleIdentifier = impIdentifier;
160 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
161 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
162 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
164 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
165 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
169 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
170 if (failed.contains(imported)) {
171 throw new InferenceException(stmt.getStatementSourceReference(),
172 "Imported module [%s] was not found.", impIdentifier);
179 private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
180 final ModuleIdentifier impIdentifier,
181 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
183 ModuleIdentifier recentModuleIdentifier = impIdentifier;
184 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
186 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
188 final ModuleIdentifier id = moduleEntry.getKey();
190 if (id.getName().equals(impIdentifier.getName())
191 && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
192 recentModuleIdentifier = id;
193 recentModuleEntry = moduleEntry;
197 return recentModuleEntry;
200 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
201 Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
202 if (revision == null) {
203 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
206 return new ModuleIdentifierImpl(stmt.getStatementArgument(), Optional.absent(),
207 Optional.of(revision));
211 private static class SemanticVersionImport {
212 private SemanticVersionImport() {
213 throw new UnsupportedOperationException("Utility class");
216 private static void onLinkageDeclared(
217 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
218 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
219 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
220 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
221 impIdentifier, SOURCE_LINKAGE);
222 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
223 .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
225 importAction.apply(new InferenceAction() {
227 public void apply() {
228 Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
229 impIdentifier.getName(), stmt);
231 StmtContext<?, ?, ?> importedModule = null;
232 ModuleIdentifier importedModuleIdentifier = null;
233 ModuleIdentifier semVerModuleIdentifier = null;
234 if (importedModuleEntry != null) {
235 importedModule = importedModuleEntry.getValue();
236 importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
237 semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
239 throw new InferenceException(stmt.getStatementSourceReference(),
240 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
241 .getName(), getRequestedImportVersion(stmt));
244 linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
245 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
246 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
247 stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
249 final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
250 stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
254 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
255 if (failed.contains(imported)) {
256 throw new InferenceException(stmt.getStatementSourceReference(),
257 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
258 .getName(), getRequestedImportVersion(stmt));
264 private static SemVer getRequestedImportVersion(Mutable<?, ?, ?> impStmt) {
265 SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
266 if (requestedImportVersion == null) {
267 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
269 return requestedImportVersion;
272 private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
273 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
274 NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
275 SemanticVersionModuleNamespace.class, moduleName);
276 if (allRelevantModulesMap == null) {
280 final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
281 allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
282 SemVer.create(requestedImportVersion.getMajor() + 1), false);
283 if (!allRelevantModulesMap.isEmpty()) {
284 return allRelevantModulesMap.lastEntry();
290 private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
291 return new ModuleIdentifierImpl(impStmt.getStatementArgument(), Optional.absent(),
292 Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
295 private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
296 final SemVer semVer) {
297 return new ModuleIdentifierImpl(importedModuleIdentifier.getName(), Optional.fromNullable(importedModuleIdentifier
298 .getNamespace()), Optional.of(importedModuleIdentifier.getRevision()), semVer);