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);
}
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);
}
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();
}
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);
}
}
Aggregations