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;
12 import com.google.common.base.Optional;
14 import java.util.Collection;
15 import java.util.Date;
17 import java.util.Map.Entry;
19 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
20 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
27 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleIdentifierImpl;
28 import org.opendaylight.yangtools.yang.parser.spi.ModuleNamespace;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToModuleIdentifier;
37 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
40 public class ImportStatementDefinition
42 AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
44 public ImportStatementDefinition() {
45 super(Rfc6020Mapping.IMPORT);
49 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
54 public ImportStatement createDeclared(
55 final StmtContext<String, ImportStatement, ?> ctx) {
56 return new ImportStatementImpl(ctx);
60 public EffectiveStatement<String, ImportStatement> createEffective(
61 final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
62 return new ImportEffectiveStatementImpl(ctx);
66 public void onLinkageDeclared(
67 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt)
68 throws SourceException {
69 final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
70 ModelActionBuilder importAction = stmt
71 .newInferenceAction(SOURCE_LINKAGE);
72 final Prerequisite<StmtContext<?, ?, ?>> imported;
73 final Prerequisite<Mutable<?, ?, ?>> linkageTarget;
74 imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
75 impIdentifier, SOURCE_LINKAGE);
76 linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
78 importAction.apply(new InferenceAction() {
81 public void apply() throws InferenceException {
82 StmtContext<?, ?, ?> importedModule = null;
83 ModuleIdentifier importedModuleIdentifier = null;
84 if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
85 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
87 stmt.getAllFromNamespace(ModuleNamespace.class));
88 if (recentModuleEntry != null) {
89 importedModuleIdentifier = recentModuleEntry.getKey();
90 importedModule = recentModuleEntry.getValue();
94 if(importedModule == null || importedModuleIdentifier == null) {
95 importedModule = imported.get();
96 importedModuleIdentifier = impIdentifier;
99 linkageTarget.get().addToNs(ImportedModuleContext.class,
100 importedModuleIdentifier, importedModule);
101 String impPrefix = firstAttributeOf(stmt.declaredSubstatements(),
102 PrefixStatement.class);
103 stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix,
104 importedModuleIdentifier);
107 private Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
108 final ModuleIdentifier impIdentifier,
109 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
111 ModuleIdentifier recentModuleIdentifier = impIdentifier;
112 Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
114 Set<Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>>> moduleEntrySet = allModules
116 for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : moduleEntrySet) {
117 if (moduleEntry.getKey().getName()
118 .equals(impIdentifier.getName())
123 recentModuleIdentifier
124 .getRevision()) > 0) {
125 recentModuleIdentifier = moduleEntry.getKey();
126 recentModuleEntry = moduleEntry;
130 return recentModuleEntry;
134 public void prerequisiteFailed(
135 final Collection<? extends Prerequisite<?>> failed)
136 throws InferenceException {
137 if (failed.contains(imported)) {
138 throw new InferenceException(String.format(
139 "Imported module [%s] was not found.",
140 impIdentifier), stmt.getStatementSourceReference());
146 private static ModuleIdentifier getImportedModuleIdentifier(
147 final Mutable<String, ImportStatement, ?> stmt) throws SourceException {
149 String moduleName = stmt.getStatementArgument();
150 Date revision = firstAttributeOf(stmt.declaredSubstatements(),
151 RevisionDateStatement.class);
152 if (revision == null) {
153 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
156 return new ModuleIdentifierImpl(moduleName, Optional.<URI> absent(),
157 Optional.<Date> of(revision));