Search in sources :

Example 1 with YangParserException

use of org.opendaylight.yangtools.yang.parser.api.YangParserException in project mdsal by opendaylight.

the class ModuleInfoSnapshotBuilder method build.

/**
 * Build {@link ModuleInfoSnapshot} from all {@code moduleInfos} in this builder.
 *
 * @return Resulting {@link ModuleInfoSnapshot}
 * @throws YangParserException if parsing any of the {@link YangModuleInfo} instances fails
 */
@NonNull
public ModuleInfoSnapshot build() throws YangParserException {
    final YangParser parser = parserFactory.createParser();
    final Map<SourceIdentifier, YangModuleInfo> mappedInfos = new HashMap<>();
    final Map<String, ClassLoader> classLoaders = new HashMap<>();
    for (YangModuleInfo info : moduleInfos) {
        final YangTextSchemaSource source = ModuleInfoSnapshotResolver.toYangTextSource(info);
        mappedInfos.put(source.getIdentifier(), info);
        final Class<?> infoClass = info.getClass();
        classLoaders.put(BindingReflections.getModelRootPackageName(infoClass.getPackage()), infoClass.getClassLoader());
        try {
            parser.addSource(source);
        } catch (YangSyntaxErrorException | IOException e) {
            throw new YangParserException("Failed to add source for " + info, e);
        }
    }
    return new DefaultModuleInfoSnapshot(parser.buildEffectiveModel(), mappedInfos, classLoaders);
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) HashMap(java.util.HashMap) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) YangParser(org.opendaylight.yangtools.yang.parser.api.YangParser) IOException(java.io.IOException) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) YangSyntaxErrorException(org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException) YangModuleInfo(org.opendaylight.yangtools.yang.binding.YangModuleInfo) Objects.requireNonNull(java.util.Objects.requireNonNull) NonNull(org.eclipse.jdt.annotation.NonNull)

Example 2 with YangParserException

use of org.opendaylight.yangtools.yang.parser.api.YangParserException in project yangtools by opendaylight.

the class AbstractDynamicMountPointContextFactory method createContext.

@Override
public final MountPointContext createContext(final Map<ContainerName, MountPointChild> libraryContainers, final MountPointChild schemaMounts) throws YangParserException {
    for (Entry<ContainerName, MountPointChild> entry : libraryContainers.entrySet()) {
        // Context for the specific code word
        final Optional<EffectiveModelContext> optLibContext = findSchemaForLibrary(entry.getKey());
        if (optLibContext.isEmpty()) {
            LOG.debug("YANG Library context for mount point {} container {} not found", getIdentifier(), entry.getKey());
            continue;
        }
        final NormalizedNode libData;
        try {
            libData = entry.getValue().normalizeTo(optLibContext.get());
        } catch (IOException e) {
            throw new YangParserException("Failed to interpret yang-library data", e);
        }
        if (!(libData instanceof ContainerNode)) {
            throw new YangParserException("Invalid yang-library non-container " + libData);
        }
        final EffectiveModelContext schemaContext = bindLibrary(entry.getKey(), (ContainerNode) libData);
        if (schemaMounts == null) {
            return new EmptyMountPointContext(schemaContext);
        }
        final NormalizedNode mountData;
        try {
            mountData = schemaMounts.normalizeTo(schemaContext);
        } catch (IOException e) {
            throw new YangParserException("Failed to interpret schema-mount data", e);
        }
        if (!(mountData instanceof ContainerNode)) {
            throw new YangParserException("Invalid schema-mount non-container " + mountData);
        }
        return createMountPointContext(schemaContext, (ContainerNode) mountData);
    }
    throw new YangParserException("Failed to interpret " + libraryContainers);
}
Also used : ContainerName(org.opendaylight.yangtools.rfc8528.data.api.YangLibraryConstants.ContainerName) MountPointChild(org.opendaylight.yangtools.rfc8528.data.api.MountPointChild) ContainerNode(org.opendaylight.yangtools.yang.data.api.schema.ContainerNode) IOException(java.io.IOException) NormalizedNode(org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 3 with YangParserException

use of org.opendaylight.yangtools.yang.parser.api.YangParserException in project yangtools by opendaylight.

the class YangToSourcesProcessor method conditionalExecute.

void conditionalExecute(final boolean skip) throws MojoExecutionException, MojoFailureException {
    /*
         * Collect all files which affect YANG context. This includes all
         * files in current project and optionally any jars/files in the
         * dependencies.
         */
    final List<File> yangFilesInProject;
    try {
        yangFilesInProject = listFiles(yangFilesRootDir, excludedFiles);
    } catch (IOException e) {
        throw new MojoFailureException("Failed to list project files", e);
    }
    if (yangFilesInProject.isEmpty()) {
        // No files to process, skip.
        LOG.info("{} No input files found", LOG_PREFIX);
        return;
    }
    // We need to instantiate all code generators to determine required import resolution mode
    final List<GeneratorTaskFactory> codeGenerators = instantiateGenerators();
    if (codeGenerators.isEmpty()) {
        LOG.warn("{} No code generators provided", LOG_PREFIX);
        return;
    }
    final Set<YangParserConfiguration> parserConfigs = codeGenerators.stream().map(GeneratorTaskFactory::parserConfig).collect(Collectors.toUnmodifiableSet());
    LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
    final Collection<File> allFiles = new ArrayList<>(yangFilesInProject);
    final Collection<ScannedDependency> dependencies;
    if (inspectDependencies) {
        dependencies = new ArrayList<>();
        final Stopwatch watch = Stopwatch.createStarted();
        try {
            ScannedDependency.scanDependencies(project).forEach(dep -> {
                allFiles.add(dep.file());
                dependencies.add(dep);
            });
        } catch (IOException e) {
            LOG.error("{} Failed to scan dependencies", LOG_PREFIX, e);
            throw new MojoExecutionException(LOG_PREFIX + " Failed to scan dependencies ", e);
        }
        LOG.info("{} Found {} dependencies in {}", LOG_PREFIX, dependencies.size(), watch);
    } else {
        dependencies = ImmutableList.of();
    }
    /*
         * Check if any of the listed files changed. If no changes occurred, simply return empty, which indicates
         * end of execution.
         */
    if (!allFiles.stream().anyMatch(buildContext::hasDelta)) {
        LOG.info("{} None of {} input files changed", LOG_PREFIX, allFiles.size());
        return;
    }
    final Stopwatch watch = Stopwatch.createStarted();
    final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed = yangFilesInProject.parallelStream().map(file -> {
        final YangTextSchemaSource textSource = YangTextSchemaSource.forPath(file.toPath());
        try {
            return Map.entry(textSource, TextToIRTransformer.transformText(textSource));
        } catch (YangSyntaxErrorException | IOException e) {
            throw new IllegalArgumentException("Failed to parse " + file, e);
        }
    }).collect(Collectors.toList());
    LOG.debug("Found project files: {}", yangFilesInProject);
    LOG.info("{} Project model files found: {} in {}", LOG_PREFIX, yangFilesInProject.size(), watch);
    // FIXME: store these files into state, so that we can verify/clean up
    final Builder<File> files = ImmutableSet.builder();
    for (YangParserConfiguration parserConfig : parserConfigs) {
        final Optional<ProcessorModuleReactor> optReactor = createReactor(yangFilesInProject, parserConfig, dependencies, parsed);
        if (optReactor.isPresent()) {
            final ProcessorModuleReactor reactor = optReactor.orElseThrow();
            if (!skip) {
                final Stopwatch sw = Stopwatch.createStarted();
                final ContextHolder holder;
                try {
                    holder = reactor.toContext();
                } catch (YangParserException e) {
                    throw new MojoFailureException("Failed to process reactor " + reactor, e);
                } catch (IOException e) {
                    throw new MojoExecutionException("Failed to read reactor " + reactor, e);
                }
                LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(), sw);
                files.addAll(generateSources(holder, codeGenerators, parserConfig));
            } else {
                LOG.info("{} Skipping YANG code generation because property yang.skip is true", LOG_PREFIX);
            }
            // FIXME: this is not right: we should be generating the models exactly once!
            // add META_INF/yang
            final Collection<YangTextSchemaSource> models = reactor.getModelsInProject();
            try {
                yangProvider.addYangsToMetaInf(project, models);
            } catch (IOException e) {
                throw new MojoExecutionException("Failed write model files for " + models, e);
            }
        }
    }
    // add META_INF/services
    File generatedServicesDir = new GeneratedDirectories(project).getYangServicesDir();
    YangProvider.setResource(generatedServicesDir, project);
    LOG.debug("{} Yang services files from: {} marked as resources: {}", LOG_PREFIX, generatedServicesDir, META_INF_YANG_SERVICES_STRING_JAR);
}
Also used : YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) BuildContext(org.sonatype.plexus.build.incremental.BuildContext) YangParserConfiguration(org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration) YangSyntaxErrorException(org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException) Stopwatch(com.google.common.base.Stopwatch) LoggerFactory(org.slf4j.LoggerFactory) YangParserFactory(org.opendaylight.yangtools.yang.parser.api.YangParserFactory) ArrayList(java.util.ArrayList) Builder(com.google.common.collect.ImmutableSet.Builder) ImmutableList(com.google.common.collect.ImmutableList) MavenProject(org.apache.maven.project.MavenProject) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) DefaultBuildContext(org.sonatype.plexus.build.incremental.DefaultBuildContext) CodeGeneratorArg(org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg) Path(java.nio.file.Path) FileGeneratorFactory(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Files(java.nio.file.Files) TextToIRTransformer(org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer) Collection(java.util.Collection) Throwables(com.google.common.base.Throwables) Set(java.util.Set) IOException(java.io.IOException) ServiceLoader(java.util.ServiceLoader) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) File(java.io.File) Preconditions.checkState(com.google.common.base.Preconditions.checkState) MojoFailureException(org.apache.maven.plugin.MojoFailureException) List(java.util.List) IRSchemaSource(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource) YangConstants(org.opendaylight.yangtools.yang.common.YangConstants) Entry(java.util.Map.Entry) Optional(java.util.Optional) FileGeneratorException(org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) YangParser(org.opendaylight.yangtools.yang.parser.api.YangParser) ArrayList(java.util.ArrayList) Stopwatch(com.google.common.base.Stopwatch) Entry(java.util.Map.Entry) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) YangParserConfiguration(org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) File(java.io.File)

Example 4 with YangParserException

use of org.opendaylight.yangtools.yang.parser.api.YangParserException in project yangtools by opendaylight.

the class AssembleSources method apply.

@Override
public FluentFuture<EffectiveModelContext> apply(final List<IRSchemaSource> sources) throws SchemaResolutionException, ReactorException {
    final Map<SourceIdentifier, IRSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
    final Map<SourceIdentifier, YangModelDependencyInfo> deps = Maps.transformValues(srcs, YangModelDependencyInfo::forIR);
    LOG.debug("Resolving dependency reactor {}", deps);
    final StatementParserMode statementParserMode = config.getStatementParserMode();
    final DependencyResolver res = statementParserMode == StatementParserMode.SEMVER_MODE ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
    if (!res.getUnresolvedSources().isEmpty()) {
        LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(), res.getUnsatisfiedImports());
        throw new SchemaResolutionException("Failed to resolve required models", res.getResolvedSources(), res.getUnsatisfiedImports());
    }
    final YangParser parser = parserFactory.createParser(res.parserConfig());
    config.getSupportedFeatures().ifPresent(parser::setSupportedFeatures);
    config.getModulesDeviatedByModules().ifPresent(parser::setModulesWithSupportedDeviations);
    for (final Entry<SourceIdentifier, IRSchemaSource> entry : srcs.entrySet()) {
        try {
            parser.addSource(entry.getValue());
        } catch (YangSyntaxErrorException | IOException e) {
            throw new SchemaResolutionException("Failed to add source " + entry.getKey(), e);
        }
    }
    final EffectiveModelContext schemaContext;
    try {
        schemaContext = parser.buildEffectiveModel();
    } catch (final YangParserException e) {
        throw new SchemaResolutionException("Failed to resolve required models", e);
    }
    return immediateFluentFuture(schemaContext);
}
Also used : IRSchemaSource(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource) SourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier) SemVerSourceIdentifier(org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier) YangParser(org.opendaylight.yangtools.yang.parser.api.YangParser) IOException(java.io.IOException) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException) YangModelDependencyInfo(org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo) SchemaResolutionException(org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException) YangSyntaxErrorException(org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException) StatementParserMode(org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Example 5 with YangParserException

use of org.opendaylight.yangtools.yang.parser.api.YangParserException in project yangtools by opendaylight.

the class MountPointData method write.

void write(@NonNull final NormalizedNodeStreamWriter writer) throws IOException {
    final StreamWriterMountPointExtension mountWriter = writer.getExtensions().getInstance(StreamWriterMountPointExtension.class);
    if (mountWriter == null) {
        LOG.debug("Writer {} does not support mount points, ignoring data in {}", writer, getIdentifier());
        return;
    }
    final MountPointContext mountCtx;
    try {
        mountCtx = contextFactory.createContext(yangLib, schemaMounts);
    } catch (YangParserException e) {
        throw new IOException("Failed to resolve mount point " + getIdentifier(), e);
    }
    try (NormalizedNodeStreamWriter nestedWriter = mountWriter.startMountPoint(getIdentifier(), mountCtx)) {
        for (MountPointChild child : children) {
            child.writeTo(nestedWriter, mountCtx);
        }
    }
}
Also used : NormalizedNodeStreamWriter(org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter) MountPointChild(org.opendaylight.yangtools.rfc8528.data.api.MountPointChild) IOException(java.io.IOException) StreamWriterMountPointExtension(org.opendaylight.yangtools.rfc8528.data.api.StreamWriterMountPointExtension) MountPointContext(org.opendaylight.yangtools.rfc8528.data.api.MountPointContext) YangParserException(org.opendaylight.yangtools.yang.parser.api.YangParserException)

Aggregations

IOException (java.io.IOException)5 YangParserException (org.opendaylight.yangtools.yang.parser.api.YangParserException)5 YangParser (org.opendaylight.yangtools.yang.parser.api.YangParser)3 YangSyntaxErrorException (org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException)3 Objects.requireNonNull (java.util.Objects.requireNonNull)2 MountPointChild (org.opendaylight.yangtools.rfc8528.data.api.MountPointChild)2 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Stopwatch (com.google.common.base.Stopwatch)1 Throwables (com.google.common.base.Throwables)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Builder (com.google.common.collect.ImmutableSet.Builder)1 Maps (com.google.common.collect.Maps)1 File (java.io.File)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1