Bug 7480 - yang-system-test: -p directories are for dependency search
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / ImportStatementDefinition.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
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;
13
14 import com.google.common.base.Verify;
15 import java.net.URI;
16 import java.util.Collection;
17 import java.util.Date;
18 import java.util.Map;
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;
54
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)
62             .build();
63
64     public ImportStatementDefinition() {
65         super(YangStmtMapping.IMPORT);
66     }
67
68     @Override
69     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
70         return value;
71     }
72
73     @Override
74     public ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
75         return new ImportStatementImpl(ctx);
76     }
77
78     @Override
79     public EffectiveStatement<String, ImportStatement> createEffective(
80             final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
81         return new ImportEffectiveStatementImpl(ctx);
82     }
83
84     @Override
85     public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
86         /*
87          * Add ModuleIdentifier of a module which is required by this module.
88          * Based on this information, required modules are searched from library
89          * sources.
90          */
91         stmt.addRequiredModule(RevisionImport.getImportedModuleIdentifier(stmt));
92
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);
99
100         importAction.apply(new InferenceAction() {
101             @Override
102             public void apply() {
103                 final StmtContext<?, ?, ?> importedModuleContext = imported.get();
104                 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
105                 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
106                         moduleName);
107                 Verify.verifyNotNull(importedModuleNamespace);
108                 final String impPrefix = SourceException.throwIfNull(
109                     firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
110                     stmt.getStatementSourceReference(), "Missing prefix statement");
111
112                 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
113             }
114
115             @Override
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);
119             }
120         });
121     }
122
123     @Override
124     public void onLinkageDeclared(
125             final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
126         if (stmt.isEnabledSemanticVersioning()) {
127             SemanticVersionImport.onLinkageDeclared(stmt);
128         } else {
129             RevisionImport.onLinkageDeclared(stmt);
130         }
131     }
132
133     @Override
134     protected SubstatementValidator getSubstatementValidator() {
135         return SUBSTATEMENT_VALIDATOR;
136     }
137
138     private static class RevisionImport {
139
140         private RevisionImport() {
141             throw new UnsupportedOperationException("Utility class");
142         }
143
144         private static void onLinkageDeclared(
145                 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
146             final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
147             final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
148             final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
149                     impIdentifier, SOURCE_LINKAGE);
150             final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
151                     .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
152
153             importAction.apply(new InferenceAction() {
154                 @Override
155                 public void apply() {
156                     StmtContext<?, ?, ?> importedModule = null;
157                     ModuleIdentifier importedModuleIdentifier = null;
158                     if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
159                         final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
160                                 impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
161                         if (recentModuleEntry != null) {
162                             importedModuleIdentifier = recentModuleEntry.getKey();
163                             importedModule = recentModuleEntry.getValue();
164                         }
165                     }
166
167                     if (importedModule == null || importedModuleIdentifier == null) {
168                         importedModule = imported.get();
169                         importedModuleIdentifier = impIdentifier;
170                     }
171
172                     linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
173                     final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
174                     stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
175
176                     final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
177                     stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
178                 }
179
180                 @Override
181                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
182                     if (failed.contains(imported)) {
183                         throw new InferenceException(stmt.getStatementSourceReference(),
184                                 "Imported module [%s] was not found.", impIdentifier);
185                     }
186                 }
187             });
188
189         }
190
191         private static Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> findRecentModule(
192                 final ModuleIdentifier impIdentifier,
193                 final Map<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> allModules) {
194
195             ModuleIdentifier recentModuleIdentifier = impIdentifier;
196             Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
197
198             for (final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
199                     .entrySet()) {
200                 final ModuleIdentifier id = moduleEntry.getKey();
201
202                 if (id.getName().equals(impIdentifier.getName())
203                         && id.getRevision().compareTo(recentModuleIdentifier.getRevision()) > 0) {
204                     recentModuleIdentifier = id;
205                     recentModuleEntry = moduleEntry;
206                 }
207             }
208
209             return recentModuleEntry;
210         }
211
212         private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
213             Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
214             if (revision == null) {
215                 revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
216             }
217
218             return ModuleIdentifierImpl.create(stmt.getStatementArgument(), Optional.empty(), Optional.of(revision));
219         }
220     }
221
222     private static class SemanticVersionImport {
223         private SemanticVersionImport() {
224             throw new UnsupportedOperationException("Utility class");
225         }
226
227         private static void onLinkageDeclared(
228                 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
229             final ModuleIdentifier impIdentifier = getImportedModuleIdentifier(stmt);
230             final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
231             final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, ModuleNamespace.class,
232                     impIdentifier, SOURCE_LINKAGE);
233             final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
234                     .mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
235
236             importAction.apply(new InferenceAction() {
237                 @Override
238                 public void apply() {
239                     final Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
240                             impIdentifier.getName(), stmt);
241
242                     StmtContext<?, ?, ?> importedModule = null;
243                     ModuleIdentifier importedModuleIdentifier = null;
244                     ModuleIdentifier semVerModuleIdentifier = null;
245                     if (importedModuleEntry != null) {
246                         importedModule = importedModuleEntry.getValue();
247                         importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToModuleIdentifier.class, importedModule);
248                         semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedModuleEntry.getKey());
249                     } else {
250                         throw new InferenceException(stmt.getStatementSourceReference(),
251                                 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
252                                         .getName(), getRequestedImportVersion(stmt));
253                     }
254
255                     linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
256                     final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
257                     stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
258                     stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
259
260                     final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
261                     stmt.addToNs(URIStringToImpPrefix.class, modNs.toString(), impPrefix);
262                 }
263
264                 @Override
265                 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
266                     if (failed.contains(imported)) {
267                         throw new InferenceException(stmt.getStatementSourceReference(),
268                                 "Unable to find module compatible with requested import [%s(%s)].", impIdentifier
269                                         .getName(), getRequestedImportVersion(stmt));
270                     }
271                 }
272             });
273         }
274
275         private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
276             SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
277             if (requestedImportVersion == null) {
278                 requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
279             }
280             return requestedImportVersion;
281         }
282
283         private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
284                 final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
285             NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
286                     SemanticVersionModuleNamespace.class, moduleName);
287             if (allRelevantModulesMap == null) {
288                 return null;
289             }
290
291             final SemVer requestedImportVersion = getRequestedImportVersion(impStmt);
292             allRelevantModulesMap = allRelevantModulesMap.subMap(requestedImportVersion, true,
293                     SemVer.create(requestedImportVersion.getMajor() + 1), false);
294             if (!allRelevantModulesMap.isEmpty()) {
295                 return allRelevantModulesMap.lastEntry();
296             }
297
298             return null;
299         }
300
301         private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
302             return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
303                     Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
304         }
305
306         private static ModuleIdentifier createSemVerModuleIdentifier(final ModuleIdentifier importedModuleIdentifier,
307                 final SemVer semVer) {
308             return ModuleIdentifierImpl.create(importedModuleIdentifier.getName(),
309                 Optional.ofNullable(importedModuleIdentifier.getNamespace()),
310                 Optional.of(importedModuleIdentifier.getRevision()), semVer);
311         }
312     }
313 }