Split out yang-model-ri
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / import_ / ImportStatementSupport.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 com.google.common.base.Preconditions.checkState;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
14 import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.SOURCE_PRE_LINKAGE;
15 import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
16
17 import com.google.common.annotations.Beta;
18 import com.google.common.collect.ImmutableList;
19 import java.util.Collection;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.openconfig.model.api.OpenConfigStatements;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.common.XMLNamespace;
24 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement;
30 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
31 import org.opendaylight.yangtools.yang.parser.spi.PreLinkageModuleNamespace;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStringStatementSupport;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
42 import org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
44 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
45
46 @Beta
47 public final class ImportStatementSupport
48         extends AbstractStringStatementSupport<ImportStatement, ImportEffectiveStatement> {
49     private static final @NonNull ImportStatementSupport RFC6020_INSTANCE = new ImportStatementSupport(
50         SubstatementValidator.builder(YangStmtMapping.IMPORT)
51             .addMandatory(YangStmtMapping.PREFIX)
52             .addOptional(YangStmtMapping.REVISION_DATE)
53             .addOptional(OpenConfigStatements.OPENCONFIG_VERSION)
54             .build());
55     private static final @NonNull ImportStatementSupport RFC7950_INSTANCE = new ImportStatementSupport(
56         SubstatementValidator.builder(YangStmtMapping.IMPORT)
57             .addMandatory(YangStmtMapping.PREFIX)
58             .addOptional(YangStmtMapping.REVISION_DATE)
59             .addOptional(OpenConfigStatements.OPENCONFIG_VERSION)
60             .addOptional(YangStmtMapping.DESCRIPTION)
61             .addOptional(YangStmtMapping.REFERENCE)
62             .build());
63
64     private final SubstatementValidator validator;
65
66     private ImportStatementSupport(final SubstatementValidator validator) {
67         super(YangStmtMapping.IMPORT, StatementPolicy.reject());
68         this.validator = requireNonNull(validator);
69     }
70
71     public static @NonNull ImportStatementSupport rfc6020Instance() {
72         return RFC6020_INSTANCE;
73     }
74
75     public static @NonNull ImportStatementSupport rfc7950Instance() {
76         return RFC7950_INSTANCE;
77     }
78
79     @Override
80     public void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
81         /*
82          * Add ModuleIdentifier of a module which is required by this module.
83          * Based on this information, required modules are searched from library
84          * sources.
85          */
86         stmt.addRequiredSource(RevisionImport.getImportedSourceIdentifier(stmt));
87
88         final String moduleName = stmt.getArgument();
89         final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
90         final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
91                 PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
92         importAction.mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
93
94         importAction.apply(new InferenceAction() {
95             @Override
96             public void apply(final InferenceContext ctx) {
97                 final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
98                 verify(moduleName.equals(importedModuleContext.getRawArgument()));
99                 final XMLNamespace importedModuleNamespace = verifyNotNull(
100                     importedModuleContext.getFromNamespace(ModuleNameToNamespace.class, moduleName));
101                 final String impPrefix = SourceException.throwIfNull(
102                     firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt,
103                     "Missing prefix statement");
104
105                 stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
106             }
107
108             @Override
109             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
110                 InferenceException.throwIf(failed.contains(imported), stmt, "Imported module [%s] was not found.",
111                     moduleName);
112             }
113         });
114     }
115
116     @Override
117     public void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
118         if (stmt.isEnabledSemanticVersioning()) {
119             SemanticVersionImport.onLinkageDeclared(stmt);
120         } else {
121             RevisionImport.onLinkageDeclared(stmt);
122         }
123     }
124
125     @Override
126     protected SubstatementValidator getSubstatementValidator() {
127         return validator;
128     }
129
130     @Override
131     protected ImportStatement createDeclared(final StmtContext<String, ImportStatement, ?> ctx,
132             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
133         return DeclaredStatements.createImport(ctx.getRawArgument(), substatements);
134     }
135
136     @Override
137     protected ImportStatement createEmptyDeclared(final StmtContext<String, ImportStatement, ?> ctx) {
138         return DeclaredStatements.createImport(ctx.getRawArgument());
139     }
140
141     @Override
142     protected ImportEffectiveStatement createEffective(final Current<String, ImportStatement> stmt,
143             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
144         checkState(!substatements.isEmpty(), "Unexpected empty effective import statement");
145
146         return new ImportEffectiveStatementImpl(stmt.declared(), substatements,
147             verifyNotNull(stmt.getFromNamespace(ImportedVersionNamespace.class, Empty.getInstance())));
148     }
149 }