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.StmtContextUtils.firstAttributeOf;
15 import java.util.Map.Entry;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
18 import com.google.common.base.Optional;
20 import java.util.Collection;
21 import java.util.Date;
22 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
23 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
24 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
30 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
38 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
39 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
40 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
42 public class ImportStatementDefinition
44 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
46 public ImportStatementDefinition() {
47 super(Rfc6020Mapping.IMPORT);
51 public String parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) {
56 public ImportStatement createDeclared(
57 StmtContext<String, ImportStatement, ?> ctx) {
58 return new ImportStatementImpl(ctx);
62 public EffectiveStatement<String, ImportStatement> createEffective(
63 StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
64 return new ImportEffectiveStatementImpl(ctx);
68 public void onLinkageDeclared(
69 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
70 throws SourceException {
71 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
72 ModelActionBuilder importAction = stmt
73 .newInferenceAction(SOURCE_LINKAGE);
74 final Prerequisite<StmtContext<?, ?, ?>> imported;
75 final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
76 imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
77 impIdentifier, SOURCE_LINKAGE);
78 linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
80 importAction.apply(new InferenceAction() {
83 public void apply() throws InferenceException {
84 StmtContext<?, ?, ?> importedModule = null;
85 ModuleIdentifier importedModuleIdentifier = null;
86 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
87 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
89 stmt.getAllFromNamespace(ModuleNamespace.class));
90 if (recentModuleEntry != null) {
91 importedModuleIdentifier = recentModuleEntry.getKey();
92 importedModule = recentModuleEntry.getValue();
96 if(importedModule == null || importedModuleIdentifier == null) {
97 importedModule = imported.get();
98 importedModuleIdentifier = impIdentifier;
101 linkageTarget.get().addToNs(ImportedModuleContext.class,
102 importedModuleIdentifier, importedModule);
103 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(),
104 PrefixStatement.class);
105 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix,
106 importedModuleIdentifier);
109 private Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
110 ModuleIdentifier impIdentifier,
111 Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
113 ModuleIdentifier recentModuleIdentifier = impIdentifier;
114 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
116 Set<Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>>> moduleEntrySet = allModules
118 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : moduleEntrySet) {
119 if (moduleEntry.getKey().getName()
120 .equals(impIdentifier.getName())
125 recentModuleIdentifier
126 .getRevision()) > 0) {
127 recentModuleIdentifier = moduleEntry.getKey();
128 recentModuleEntry = moduleEntry;
132 return recentModuleEntry;
136 public void prerequisiteFailed(
137 Collection<? extends Prerequisite<?>> failed)
138 throws InferenceException {
139 if (failed.contains(imported)) {
140 throw new InferenceException(String.format(
141 "Imported module [%s] was not found.",
142 impIdentifier), stmt.getStatementSourceReference());
148 private ModuleIdentifier getImportedModuleIdentifier(
149 Mutable<String, ImportStatement, ?> stmt) throws SourceException {
151 String moduleName = stmt.getStatementArgument();
152 Date revision = firstAttributeOf(stmt.declaredSubstatements(),
153 RevisionDateStatement.class);
154 if (revision == null) {
155 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
158 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(),
159 Optional.<Date> of(revision));