import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
-import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
+import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ImportStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionModuleNamespace;
import org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace;
import org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext;
import org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToModuleIdentifier;
import org.opendaylight.yangtools.yang.parser.spi.source.ModuleNameToNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.ImportEffectiveStatementImpl;
public class ImportStatementDefinition extends
AbstractStatementSupport<String, ImportStatement, EffectiveStatement<String, ImportStatement>> {
private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
- .builder(Rfc6020Mapping.IMPORT)
- .addMandatory(Rfc6020Mapping.PREFIX)
- .addOptional(Rfc6020Mapping.REVISION_DATE)
- .addOptional(SupportedExtensionsMapping.SEMANTIC_VERSION)
+ .builder(YangStmtMapping.IMPORT)
+ .addMandatory(YangStmtMapping.PREFIX)
+ .addOptional(YangStmtMapping.REVISION_DATE)
+ .addOptional(SupportedExtensionsMapping.OPENCONFIG_VERSION)
.build();
public ImportStatementDefinition() {
- super(Rfc6020Mapping.IMPORT);
+ super(YangStmtMapping.IMPORT);
}
@Override
return new ImportEffectiveStatementImpl(ctx);
}
- @Override
- public void onFullDefinitionDeclared(
- final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
- super.onFullDefinitionDeclared(stmt);
- SUBSTATEMENT_VALIDATOR.validate(stmt);
- }
-
@Override
public void onPreLinkageDeclared(final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> stmt) {
+ /*
+ * Add ModuleIdentifier of a module which is required by this module.
+ * Based on this information, required modules are searched from library
+ * sources.
+ */
+ stmt.addRequiredModule(RevisionImport.getImportedModuleIdentifier(stmt));
+
final String moduleName = stmt.getStatementArgument();
final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt,
importAction.apply(new InferenceAction() {
@Override
- public void apply() {
- StmtContext<?, ?, ?> importedModuleContext = imported.get();
+ public void apply(final InferenceContext ctx) {
+ final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
Verify.verify(moduleName.equals(importedModuleContext.getStatementArgument()));
final URI importedModuleNamespace = importedModuleContext.getFromNamespace(ModuleNameToNamespace.class,
moduleName);
Verify.verifyNotNull(importedModuleNamespace);
- final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
- Verify.verifyNotNull(impPrefix);
+ final String impPrefix = SourceException.throwIfNull(
+ firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class),
+ stmt.getStatementSourceReference(), "Missing prefix statement");
+
stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
}
}
}
+ @Override
+ protected SubstatementValidator getSubstatementValidator() {
+ return SUBSTATEMENT_VALIDATOR;
+ }
+
private static class RevisionImport {
private RevisionImport() {
importAction.apply(new InferenceAction() {
@Override
- public void apply() {
+ public void apply(final InferenceContext ctx) {
StmtContext<?, ?, ?> importedModule = null;
ModuleIdentifier importedModuleIdentifier = null;
if (impIdentifier.getRevision() == SimpleDateFormatUtil.DEFAULT_DATE_IMP) {
- Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
+ final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = findRecentModule(
impIdentifier, stmt.getAllFromNamespace(ModuleNamespace.class));
if (recentModuleEntry != null) {
importedModuleIdentifier = recentModuleEntry.getKey();
}
if (importedModule == null || importedModuleIdentifier == null) {
- importedModule = imported.get();
+ importedModule = imported.resolve(ctx);
importedModuleIdentifier = impIdentifier;
}
- linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
- String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+ linkageTarget.resolve(ctx).addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
+ final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
final URI modNs = firstAttributeOf(importedModule.declaredSubstatements(), NamespaceStatement.class);
ModuleIdentifier recentModuleIdentifier = impIdentifier;
Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> recentModuleEntry = null;
- for (Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
+ for (final Entry<ModuleIdentifier, StmtContext<?, ModuleStatement, EffectiveStatement<String, ModuleStatement>>> moduleEntry : allModules
.entrySet()) {
final ModuleIdentifier id = moduleEntry.getKey();
return recentModuleEntry;
}
- private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> stmt) {
+ private static ModuleIdentifier getImportedModuleIdentifier(final StmtContext<String, ImportStatement, ?> stmt) {
Date revision = firstAttributeOf(stmt.declaredSubstatements(), RevisionDateStatement.class);
if (revision == null) {
revision = SimpleDateFormatUtil.DEFAULT_DATE_IMP;
importAction.apply(new InferenceAction() {
@Override
- public void apply() {
- Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
+ public void apply(final InferenceContext ctx) {
+ final Entry<SemVer, StmtContext<?, ?, ?>> importedModuleEntry= findRecentCompatibleModuleEntry(
impIdentifier.getName(), stmt);
StmtContext<?, ?, ?> importedModule = null;
.getName(), getRequestedImportVersion(stmt));
}
- linkageTarget.get().addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
- String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
+ linkageTarget.resolve(ctx).addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
+ final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
stmt.addToNs(ImpPrefixToModuleIdentifier.class, impPrefix, importedModuleIdentifier);
stmt.addToNs(ImpPrefixToSemVerModuleIdentifier.class, impPrefix, semVerModuleIdentifier);
});
}
- private static SemVer getRequestedImportVersion(final Mutable<?, ?, ?> impStmt) {
+ private static SemVer getRequestedImportVersion(final StmtContext<?, ?, ?> impStmt) {
SemVer requestedImportVersion = impStmt.getFromNamespace(SemanticVersionNamespace.class, impStmt);
if (requestedImportVersion == null) {
requestedImportVersion = Module.DEFAULT_SEMANTIC_VERSION;
}
private static Entry<SemVer, StmtContext<?, ?, ?>> findRecentCompatibleModuleEntry(final String moduleName,
- final Mutable<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
+ final StmtContext<String, ImportStatement, EffectiveStatement<String, ImportStatement>> impStmt) {
NavigableMap<SemVer, StmtContext<?, ?, ?>> allRelevantModulesMap = impStmt.getFromNamespace(
SemanticVersionModuleNamespace.class, moduleName);
if (allRelevantModulesMap == null) {
return null;
}
- private static ModuleIdentifier getImportedModuleIdentifier(final Mutable<String, ImportStatement, ?> impStmt) {
+ private static ModuleIdentifier getImportedModuleIdentifier(final StmtContext<String, ImportStatement, ?> impStmt) {
return ModuleIdentifierImpl.create(impStmt.getStatementArgument(), Optional.empty(),
Optional.of(SimpleDateFormatUtil.DEFAULT_DATE_IMP));
}