Search in sources :

Example 1 with SemVer

use of org.opendaylight.yangtools.concepts.SemVer in project yangtools by opendaylight.

the class YangModelDependencyInfo method parseImports.

private static ImmutableSet<ModuleImport> parseImports(final IRStatement module, final SourceIdentifier source) {
    final Set<ModuleImport> result = new HashSet<>();
    for (final IRStatement substatement : module.statements()) {
        if (isBuiltin(substatement, IMPORT)) {
            final String importedModuleName = safeStringArgument(source, substatement, "imported module name");
            final String revisionDateStr = getRevisionDateString(substatement, source);
            final Revision revisionDate = Revision.ofNullable(revisionDateStr).orElse(null);
            final SemVer importSemVer = findSemanticVersion(substatement, source);
            result.add(new ModuleImportImpl(importedModuleName, revisionDate, importSemVer));
        }
    }
    return ImmutableSet.copyOf(result);
}
Also used : IRStatement(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRStatement) Revision(org.opendaylight.yangtools.yang.common.Revision) ModuleImport(org.opendaylight.yangtools.yang.model.api.ModuleImport) SemVer(org.opendaylight.yangtools.concepts.SemVer) HashSet(java.util.HashSet)

Example 2 with SemVer

use of org.opendaylight.yangtools.concepts.SemVer in project yangtools by opendaylight.

the class SemanticVersionImport method onLinkageDeclared.

static void onLinkageDeclared(final Mutable<String, ImportStatement, ImportEffectiveStatement> stmt) {
    final ModelActionBuilder importAction = stmt.newInferenceAction(SOURCE_LINKAGE);
    final String moduleName = stmt.getArgument();
    final SemVer semanticVersion = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
    final SemanticVersionImport.CompatibleCriterion criterion = semanticVersion == null ? new NoVerCompatibleCriterion(moduleName) : new SemVerCompatibleCriterion(moduleName, semanticVersion);
    final Prerequisite<StmtContext<?, ?, ?>> imported = importAction.requiresCtx(stmt, SemanticVersionModuleNamespace.class, criterion, SOURCE_LINKAGE);
    final Prerequisite<Mutable<?, ?, ?>> linkageTarget = importAction.mutatesCtx(stmt.getRoot(), SOURCE_LINKAGE);
    importAction.apply(new InferenceAction() {

        @Override
        public void apply(final InferenceContext ctx) {
            final StmtContext<?, ?, ?> importedModule = imported.resolve(ctx);
            final SemVer importedVersion = stmt.getFromNamespace(SemanticVersionNamespace.class, stmt);
            final SourceIdentifier importedModuleIdentifier = importedModule.getFromNamespace(ModuleCtxToSourceIdentifier.class, importedModule);
            final SemVerSourceIdentifier semVerModuleIdentifier = createSemVerModuleIdentifier(importedModuleIdentifier, importedVersion);
            stmt.addToNs(ImportedVersionNamespace.class, Empty.value(), semVerModuleIdentifier);
            linkageTarget.resolve(ctx).addToNs(ImportedModuleContext.class, importedModuleIdentifier, importedModule);
            final String impPrefix = firstAttributeOf(stmt.declaredSubstatements(), PrefixStatement.class);
            stmt.addToNs(ImportPrefixToModuleCtx.class, impPrefix, importedModule);
            stmt.addToNs(ImportPrefixToSemVerSourceIdentifier.class, impPrefix, semVerModuleIdentifier);
            final QNameModule mod = InferenceException.throwIfNull(stmt.getFromNamespace(ModuleCtxToModuleQName.class, importedModule), stmt, "Failed to find module of %s", importedModule);
            stmt.addToNs(ModuleQNameToPrefix.class, mod, impPrefix);
        }

        @Override
        public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
            if (failed.contains(imported)) {
                throw new InferenceException(stmt, "Unable to find module compatible with requested import [%s(%s)].", moduleName, getRequestedImportVersionString(stmt));
            }
        }
    });
}
Also used : SemVer(org.opendaylight.yangtools.concepts.SemVer) InferenceAction(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction) ImportPrefixToSemVerSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier) SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) ModuleCtxToSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier) QNameModule(org.opendaylight.yangtools.yang.common.QNameModule) ImportPrefixToModuleCtx(org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx) ImportedModuleContext(org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext) InferenceContext(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext) ImportPrefixToSemVerSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) ModuleCtxToSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ModuleCtxToSourceIdentifier) SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) InferenceException(org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException) StmtContext(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext) PrefixStatement(org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement) Mutable(org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable) SemanticVersionNamespace(org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace) ImportPrefixToSemVerSourceIdentifier(org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier) ModuleQNameToPrefix(org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ModuleQNameToPrefix) ModelActionBuilder(org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)

Example 3 with SemVer

use of org.opendaylight.yangtools.concepts.SemVer in project yangtools by opendaylight.

the class AssembleSources method getSemVerIdentifier.

private static SemVerSourceIdentifier getSemVerIdentifier(final IRSchemaSource source) {
    final SourceIdentifier identifier = source.getIdentifier();
    final SemVer semver = YangModelDependencyInfo.findSemanticVersion(source.getRootStatement(), identifier);
    if (identifier instanceof SemVerSourceIdentifier && semver == null) {
        return (SemVerSourceIdentifier) identifier;
    }
    return SemVerSourceIdentifier.create(identifier.getName(), identifier.getRevision(), semver);
}
Also used : SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) SemVer(org.opendaylight.yangtools.concepts.SemVer)

Example 4 with SemVer

use of org.opendaylight.yangtools.concepts.SemVer in project yangtools by opendaylight.

the class SemVerDependencyResolver method isCompatible.

private static boolean isCompatible(final Optional<SemVer> moduleSemVer, final Optional<SemVer> importSemVer) {
    if (importSemVer.isEmpty()) {
        // Import does not care about the version
        return true;
    }
    if (moduleSemVer.isEmpty()) {
        // Modules which do not declare a semantic version are incompatible with imports which do
        return false;
    }
    final SemVer modVer = moduleSemVer.get();
    final SemVer impVer = importSemVer.get();
    return modVer.getMajor() == impVer.getMajor() && modVer.compareTo(impVer) >= 0;
}
Also used : SemVer(org.opendaylight.yangtools.concepts.SemVer)

Aggregations

SemVer (org.opendaylight.yangtools.concepts.SemVer)4 SemVerSourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier)2 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)2 HashSet (java.util.HashSet)1 QNameModule (org.opendaylight.yangtools.yang.common.QNameModule)1 Revision (org.opendaylight.yangtools.yang.common.Revision)1 ModuleImport (org.opendaylight.yangtools.yang.model.api.ModuleImport)1 PrefixStatement (org.opendaylight.yangtools.yang.model.api.stmt.PrefixStatement)1 IRStatement (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRStatement)1 ModuleQNameToPrefix (org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ModuleQNameToPrefix)1 InferenceException (org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException)1 ModelActionBuilder (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder)1 InferenceAction (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction)1 InferenceContext (org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext)1 SemanticVersionNamespace (org.opendaylight.yangtools.yang.parser.spi.meta.SemanticVersionNamespace)1 StmtContext (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext)1 Mutable (org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable)1 ImportPrefixToModuleCtx (org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToModuleCtx)1 ImportPrefixToSemVerSourceIdentifier (org.opendaylight.yangtools.yang.parser.spi.source.ImportPrefixToSemVerSourceIdentifier)1 ImportedModuleContext (org.opendaylight.yangtools.yang.parser.spi.source.ImportedModuleContext)1