use of org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException 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.YangSyntaxErrorException in project mdsal by opendaylight.
the class ModuleInfoSnapshotResolver method registerModuleInfo.
/*
* Perform registration of a YangModuleInfo.
*/
@Holding("this")
private RegisteredModuleInfo registerModuleInfo(@NonNull final YangModuleInfo info) {
// First search for an existing explicit registration
final SourceIdentifier sourceId = sourceIdentifierFrom(info);
for (RegisteredModuleInfo reg : sourceToInfoReg.get(sourceId)) {
if (info.equals(reg.info)) {
reg.incRef();
LOG.debug("Reusing registration {}", reg);
return reg;
}
}
// Create an explicit registration
final Registration reg;
try {
reg = ctxResolver.registerSource(toYangTextSource(sourceId, info));
} catch (YangSyntaxErrorException | SchemaSourceException | IOException e) {
throw new IllegalStateException("Failed to register info " + info, e);
}
final RegisteredModuleInfo regInfo = new RegisteredModuleInfo(info, reg, info.getClass().getClassLoader());
LOG.debug("Created new registration {}", regInfo);
sourceToInfoReg.put(sourceId, regInfo);
return regInfo;
}
use of org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException in project yangtools by opendaylight.
the class YangErrorListener method syntaxError.
@Override
@SuppressWarnings("checkstyle:parameterName")
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
LOG.debug("Syntax error in {} at {}:{}: {}", source, line, charPositionInLine, msg, e);
exceptions.add(new YangSyntaxErrorException(source, line, charPositionInLine, msg, e));
}
use of org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException in project yangtools by opendaylight.
the class YangErrorListener method validate.
void validate() throws YangSyntaxErrorException {
if (exceptions.isEmpty()) {
return;
}
// Single exception: just throw it
if (exceptions.size() == 1) {
throw exceptions.get(0);
}
final StringBuilder sb = new StringBuilder();
boolean first = true;
for (YangSyntaxErrorException e : exceptions) {
if (first) {
first = false;
} else {
sb.append('\n');
}
sb.append(e.getFormattedMessage());
}
throw new YangSyntaxErrorException(source, 0, 0, sb.toString());
}
use of org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException 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);
}
Aggregations