use of org.opendaylight.yangtools.yang.common.YangVersion in project yangtools by opendaylight.
the class ImportStatementSupport method onPreLinkageDeclared.
@Override
public void onPreLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
/*
* Add ModuleIdentifier of a module which is required by this module.
* Based on this information, required modules are searched from library
* sources.
*/
final SourceIdentifier importId = RevisionImport.getImportedSourceIdentifier(stmt);
stmt.addRequiredSource(importId);
final String moduleName = stmt.getArgument();
final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_PRE_LINKAGE);
final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, PreLinkageModuleNamespace.class, moduleName, SOURCE_PRE_LINKAGE);
final Prerequisite<Mutable<?, ?, ?>> rootPrereq = importAction.mutatesCtx(stmt.getRoot(), SOURCE_PRE_LINKAGE);
importAction.apply(new InferenceAction() {
@Override
public void apply(final InferenceContext ctx) {
final StmtContext<?, ?, ?> importedModuleContext = imported.resolve(ctx);
verify(moduleName.equals(importedModuleContext.getRawArgument()));
final XMLNamespace importedModuleNamespace = verifyNotNull(importedModuleContext.getFromNamespace(ModuleNameToNamespace.class, moduleName));
final String impPrefix = SourceException.throwIfNull(firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class), stmt, "Missing prefix statement");
final Mutable<?, ?, ?> root = rootPrereq.resolve(ctx);
// Version 1 sources must not import-by-revision Version 1.1 modules
if (importId.getRevision().isPresent() && root.yangVersion() == YangVersion.VERSION_1) {
final YangVersion importedVersion = importedModuleContext.yangVersion();
if (importedVersion != YangVersion.VERSION_1) {
throw new YangVersionLinkageException(stmt, "Cannot import by revision version %s module %s", importedVersion, moduleName);
}
}
stmt.addToNs(ImpPrefixToNamespace.class, impPrefix, importedModuleNamespace);
}
@Override
public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
InferenceException.throwIf(failed.contains(imported), stmt, "Imported module [%s] was not found.", moduleName);
}
});
}
use of org.opendaylight.yangtools.yang.common.YangVersion in project yangtools by opendaylight.
the class IncludeStatementSupport method onLinkageDeclared.
@Override
public void onLinkageDeclared(final Mutable<String, IncludeStatement, IncludeEffectiveStatement> stmt) {
final String submoduleName = stmt.getArgument();
final StmtContext<Revision, ?, ?> revision = findFirstDeclaredSubstatement(stmt, RevisionDateStatement.class);
final ModelActionBuilder includeAction = stmt.newInferenceAction(SOURCE_LINKAGE);
final Prerequisite<StmtContext<?, ?, ?>> requiresCtxPrerequisite;
if (revision == null) {
requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class, NamespaceKeyCriterion.latestRevisionModule(submoduleName), SOURCE_LINKAGE);
} else {
requiresCtxPrerequisite = includeAction.requiresCtx(stmt, SubmoduleNamespace.class, RevisionSourceIdentifier.create(submoduleName, Optional.of(revision.argument())), SOURCE_LINKAGE);
}
includeAction.apply(new InferenceAction() {
@Override
public void apply(final InferenceContext ctx) {
final StmtContext<?, ?, ?> includedSubModuleContext = requiresCtxPrerequisite.resolve(ctx);
final YangVersion modVersion = stmt.getRoot().yangVersion();
final YangVersion subVersion = includedSubModuleContext.yangVersion();
if (subVersion != modVersion) {
throw new YangVersionLinkageException(stmt, "Cannot include a version %s submodule in a version %s module", subVersion, modVersion);
}
stmt.addToNs(IncludedModuleContext.class, revision != null ? RevisionSourceIdentifier.create(submoduleName, revision.argument()) : RevisionSourceIdentifier.create(submoduleName), includedSubModuleContext);
stmt.addToNs(IncludedSubmoduleNameToModuleCtx.class, stmt.argument(), includedSubModuleContext);
}
@Override
public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
InferenceException.throwIf(failed.contains(requiresCtxPrerequisite), stmt, "Included submodule '%s' was not found: ", stmt.argument());
}
});
}
Aggregations