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.rfc7950.stmt.import_;
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;
13 import com.google.common.base.Verify;
15 import java.util.Collection;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
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;
33 abstract class AbstractImportStatementSupport
34 extends AbstractStatementSupport<String, ImportStatement, ImportEffectiveStatement> {
35 AbstractImportStatementSupport() {
36 super(YangStmtMapping.IMPORT);
40 public final String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45 public final ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
46 return new ImportStatementImpl(ctx);
50 public final ImportEffectiveStatement createEffective(
51 final StmtContext<String, ImportStatement, ImportEffectiveStatement> ctx) {
52 return new ImportEffectiveStatementImpl(ctx);
56 public final void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
58 * Add ModuleIdentifier of a module which is required by this module.
59 * Based on this information, required modules are searched from library
62 stmt.addRequiredSource(RevisionImport.getImportedSourceIdentifier(stmt));
64 final String moduleName = stmt.coerceStatementArgument();
65 final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
66 final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
67 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
68 final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction
69 .mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
71 importAction.apply(new InferenceAction() {
73 public void apply(final InferenceContext ctx) {
74 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
75 Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
76 final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
78 Verify.verifyNotNull(importedModuleNamespace);
79 final String impPrefix = SourceException.throwIfNull(
80 firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
81 stmt.getStatementSourceReference(), "Missing prefix statement");
83 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
87 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
88 InferenceException.throwIf(failed.contains(imported), stmt.getStatementSourceReference(),
89 "Imported module [%s] was not found.", moduleName);
95 public final void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
96 if (stmt.isEnabledSemanticVersioning()) {
97 SemanticVersionImport.onLinkageDeclared(stmt);
99 RevisionImport.onLinkageDeclared(stmt);