Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / import_ / AbstractImportStatementSupport.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.rfc7950.stmt.import_;
9
10 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_PRE_LINKAGE;
11 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
12
13 import com.google.common.base.Verify;
14 import java.net.URI;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
31 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
32
33 abstract class AbstractImportStatementSupport extends
34         AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
35     AbstractImportStatementSupport() {
36         super(YangStmtMapping.IMPORT);
37     }
38
39     @Override
40     public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41         return value;
42     }
43
44     @Override
45     public final ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
46         return new ImportStatementImpl(ctx);
47     }
48
49     @Override
50     public final EffectiveStatement<String, ImportStatement> createEffective(
51             final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> ctx) {
52         return new ImportEffectiveStatementImpl(ctx);
53     }
54
55     @Override
56     public final void onPreLinkageDeclared(final Mutable<String, ImportStatement,
57             EffectiveStatement<String, ImportStatement>> stmt) {
58         /*
59          * Add ModuleIdentifier of a module which is required by this module.
60          * Based on this information, required modules are searched from library
61          * sources.
62          */
63         stmt.addRequiredSource(RevisionImport.getImportedSourceIdentifier(stmt));
64
65         final String moduleName = stmt.getStatementArgument();
66         final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
67         final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
68                 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
69         final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
70                 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
71
72         importAction.apply(new InferenceAction() {
73             @Override
74             public void apply(final InferenceContext ctx) {
75                 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
76                 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
77                 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
78                         moduleName);
79                 Verify.verifyNotNull(importedModuleNamespace);
80                 final String impPrefix = SourceException.throwIfNull(
81                     firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
82                     stmt.getStatementSourceReference(), "Missing prefix statement");
83
84                 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
85             }
86
87             @Override
88             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
89                 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
90                         "Imported module [%s] was not found.", moduleName);
91             }
92         });
93     }
94
95     @Override
96     public final void onLinkageDeclared(
97             final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
98         if (stmt.isEnabledSemanticVersioning()) {
99             SemanticVersionImport.onLinkageDeclared(stmt);
100         } else {
101             RevisionImport.onLinkageDeclared(stmt);
102         }
103     }
104 }