Search in sources :

Example 1 with YangParser

use of org.opendaylight.yangtools.yang.parser.api.YangParser 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 YangParser

use of org.opendaylight.yangtools.yang.parser.api.YangParser 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 3 with YangParser

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

the class SystemTestUtils method parseYangSources.

static EffectiveModelContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles, final List<File> libFiles) throws IOException, YangParserException {
    checkArgument(!testFiles.isEmpty(), "No yang sources");
    final YangParser parser = PARSER_FACTORY.createParser();
    if (supportedFeatures != null) {
        parser.setSupportedFeatures(supportedFeatures);
    }
    for (File file : testFiles) {
        parser.addSource(YangTextSchemaSource.forPath(file.toPath()));
    }
    for (File file : libFiles) {
        parser.addLibSource(YangTextSchemaSource.forPath(file.toPath()));
    }
    return parser.buildEffectiveModel();
}
Also used : YangParser(org.opendaylight.yangtools.yang.parser.api.YangParser) File(java.io.File)

Example 4 with YangParser

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

the class YangToSourcesProcessor method createReactor.

@SuppressWarnings("checkstyle:illegalCatch")
private Optional<ProcessorModuleReactor> createReactor(final List<File> yangFilesInProject, final YangParserConfiguration parserConfig, final Collection<ScannedDependency> dependencies, final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed) throws MojoExecutionException {
    try {
        final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
        final YangParser parser = parserFactory.createParser(parserConfig);
        for (final Entry<YangTextSchemaSource, IRSchemaSource> entry : parsed) {
            final YangTextSchemaSource textSource = entry.getKey();
            final IRSchemaSource astSource = entry.getValue();
            parser.addSource(astSource);
            if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
                // AST indicates a different source identifier, make sure we use that
                sourcesInProject.add(YangTextSchemaSource.delegateForByteSource(astSource.getIdentifier(), textSource));
            } else {
                sourcesInProject.add(textSource);
            }
        }
        final ProcessorModuleReactor reactor = new ProcessorModuleReactor(parser, sourcesInProject, dependencies);
        LOG.debug("Initialized reactor {} with {}", reactor, yangFilesInProject);
        return Optional.of(reactor);
    } catch (IOException | YangSyntaxErrorException | RuntimeException e) {
        // MojoExecutionException is thrown since execution cannot continue
        LOG.error("{} Unable to parse YANG files from {}", LOG_PREFIX, yangFilesRootDir, e);
        Throwable rootCause = Throwables.getRootCause(e);
        throw new MojoExecutionException(LOG_PREFIX + " Unable to parse YANG files from " + yangFilesRootDir, rootCause);
    }
}
Also used : IRSchemaSource(org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArrayList(java.util.ArrayList) YangParser(org.opendaylight.yangtools.yang.parser.api.YangParser) IOException(java.io.IOException) YangSyntaxErrorException(org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException)

Aggregations

YangParser (org.opendaylight.yangtools.yang.parser.api.YangParser)4 IOException (java.io.IOException)3 YangSyntaxErrorException (org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException)3 SourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier)2 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)2 YangParserException (org.opendaylight.yangtools.yang.parser.api.YangParserException)2 IRSchemaSource (org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 NonNull (org.eclipse.jdt.annotation.NonNull)1 YangModuleInfo (org.opendaylight.yangtools.yang.binding.YangModuleInfo)1 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)1 SchemaResolutionException (org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException)1 SemVerSourceIdentifier (org.opendaylight.yangtools.yang.model.repo.api.SemVerSourceIdentifier)1 StatementParserMode (org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode)1 YangModelDependencyInfo (org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo)1