Search in sources :

Example 1 with YangVersion

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);
        }
    });
}
Also used : InferenceContext(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) StmtContext(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext) InferenceAction(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction) Mutable(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable) YangVersion(org.opendaylight.yangtools.yang.common.YangVersion) YangVersionLinkageException(org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException) XMLNamespace(org.opendaylight.yangtools.yang.common.XMLNamespace) ModelActionBuilder(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder) ImpPrefixToNamespace(org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace)

Example 2 with YangVersion

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());
        }
    });
}
Also used : InferenceContext(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext) IncludedModuleContext(org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext) IncludedSubmoduleNameToModuleCtx(org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx) SubmoduleNamespace(org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace) StmtContext(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext) InferenceAction(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction) YangVersion(org.opendaylight.yangtools.yang.common.YangVersion) YangVersionLinkageException(org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException) Revision(org.opendaylight.yangtools.yang.common.Revision) ModelActionBuilder(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)

Aggregations

YangVersion (org.opendaylight.yangtools.yang.common.YangVersion)2 ModelActionBuilder (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)2 InferenceAction (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction)2 InferenceContext (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext)2 StmtContext (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext)2 YangVersionLinkageException (org.opendaylight.yangtools.yang.parser.spi.source.YangVersionLinkageException)2 Revision (org.opendaylight.yangtools.yang.common.Revision)1 XMLNamespace (org.opendaylight.yangtools.yang.common.XMLNamespace)1 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)1 SubmoduleNamespace (org.opendaylight.yangtools.yang.parser.spi.SubmoduleNamespace)1 Mutable (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable)1 ImpPrefixToNamespace (org.opendaylight.yangtools.yang.parser.spi.source.ImpPrefixToNamespace)1 IncludedModuleContext (org.opendaylight.yangtools.yang.parser.spi.source.IncludedModuleContext)1 IncludedSubmoduleNameToModuleCtx (org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx)1