*/
package org.opendaylight.yangtools.yang2sources.plugin;
-import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.requireNonNull;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Comparator;
-import java.util.HashMap;
-import java.util.Iterator;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
+import java.util.NoSuchElementException;
import java.util.ServiceLoader;
import java.util.Set;
+import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
+import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
import org.opendaylight.yangtools.yang.common.YangConstants;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParser;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserException;
-import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
-import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
+import org.opendaylight.yangtools.yang.model.spi.source.DelegatedYangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.YangIRSchemaSource;
+import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
+import org.opendaylight.yangtools.yang.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
-import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
-import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
-import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator.ImportResolutionMode;
-import org.opendaylight.yangtools.yang2sources.spi.BuildContextAware;
-import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonatype.plexus.build.incremental.BuildContext;
import org.sonatype.plexus.build.incremental.DefaultBuildContext;
+// FIXME: rename to Execution
+// FIXME: final
class YangToSourcesProcessor {
private static final Logger LOG = LoggerFactory.getLogger(YangToSourcesProcessor.class);
private static final YangParserFactory DEFAULT_PARSER_FACTORY;
static {
- final Iterator<YangParserFactory> it = ServiceLoader.load(YangParserFactory.class).iterator();
- checkState(it.hasNext(), "Failed to find a YangParserFactory implementation");
- DEFAULT_PARSER_FACTORY = it.next();
+ try {
+ DEFAULT_PARSER_FACTORY = ServiceLoader.load(YangParserFactory.class).iterator().next();
+ } catch (NoSuchElementException e) {
+ throw new IllegalStateException("Failed to find a YangParserFactory implementation", e);
+ }
}
- static final String LOG_PREFIX = "yang-to-sources:";
private static final String META_INF_STR = "META-INF";
private static final String YANG_STR = "yang";
+ static final String LOG_PREFIX = "yang-to-sources:";
static final String META_INF_YANG_STRING = META_INF_STR + File.separator + YANG_STR;
static final String META_INF_YANG_STRING_JAR = META_INF_STR + "/" + YANG_STR;
static final String META_INF_YANG_SERVICES_STRING_JAR = META_INF_STR + "/" + "services";
+ private static final YangProvider YANG_PROVIDER = (project, modelsInProject) -> {
+ final var generatedYangDir =
+ // FIXME: why are we generating these in "generated-sources"? At the end of the day YANG files are more
+ // resources (except we do not them to be subject to filtering)
+ new File(new File(project.getBuild().getDirectory(), "generated-sources"), "yang");
+ LOG.debug("Generated dir {}", generatedYangDir);
+
+ // copy project's src/main/yang/*.yang to ${project.builddir}/generated-sources/yang/META-INF/yang/
+ // This honors setups like a Eclipse-profile derived one
+ final var withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
+ Files.createDirectories(withMetaInf.toPath());
+
+ final var stateListBuilder = ImmutableList.<FileState>builderWithExpectedSize(modelsInProject.size());
+ for (var source : modelsInProject) {
+ final File file = new File(withMetaInf, source.sourceId().toYangFilename());
+ stateListBuilder.add(FileState.ofWrittenFile(file,
+ out -> source.asByteSource(StandardCharsets.UTF_8).copyTo(out)));
+ LOG.debug("Created file {} for {}", file, source.sourceId());
+ }
+
+ ProjectFileAccess.addResourceDir(project, generatedYangDir);
+ LOG.debug("{} YANG files marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, generatedYangDir);
+
+ return stateListBuilder.build();
+ };
+
private final YangParserFactory parserFactory;
private final File yangFilesRootDir;
private final Set<File> excludedFiles;
- private final List<CodeGeneratorArg> codeGeneratorArgs;
- private final MavenProject project;
+ private final ImmutableMap<String, FileGeneratorArg> fileGeneratorArgs;
+ private final @NonNull MavenProject project;
private final boolean inspectDependencies;
- private final BuildContext buildContext;
+ private final @NonNull BuildContext buildContext;
private final YangProvider yangProvider;
+ private final StateStorage stateStorage;
+ private final String projectBuildDirectory;
private YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
- final Collection<File> excludedFiles, final List<CodeGeneratorArg> codeGenerators,
+ final Collection<File> excludedFiles, final List<FileGeneratorArg> fileGeneratorsArgs,
final MavenProject project, final boolean inspectDependencies, final YangProvider yangProvider) {
this.buildContext = requireNonNull(buildContext, "buildContext");
this.yangFilesRootDir = requireNonNull(yangFilesRootDir, "yangFilesRootDir");
this.excludedFiles = ImmutableSet.copyOf(excludedFiles);
- this.codeGeneratorArgs = ImmutableList.copyOf(codeGenerators);
+ //FIXME multiple FileGeneratorArg entries of same identifier became one here
+ fileGeneratorArgs = Maps.uniqueIndex(fileGeneratorsArgs, FileGeneratorArg::getIdentifier);
this.project = requireNonNull(project);
this.inspectDependencies = inspectDependencies;
this.yangProvider = requireNonNull(yangProvider);
- this.parserFactory = DEFAULT_PARSER_FACTORY;
+ projectBuildDirectory = project.getBuild().getDirectory();
+ stateStorage = StateStorage.of(buildContext, stateFilePath(projectBuildDirectory));
+ parserFactory = DEFAULT_PARSER_FACTORY;
}
@VisibleForTesting
- YangToSourcesProcessor(final File yangFilesRootDir, final Collection<File> excludedFiles,
- final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies,
- final YangProvider yangProvider) {
- this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, codeGenerators, project,
- inspectDependencies, yangProvider);
+ YangToSourcesProcessor(final File yangFilesRootDir, final List<FileGeneratorArg> fileGenerators,
+ final MavenProject project, final YangProvider yangProvider) {
+ this(new DefaultBuildContext(), yangFilesRootDir, List.of(), List.of(), project, false, yangProvider);
}
YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
- final Collection<File> excludedFiles, final List<CodeGeneratorArg> codeGenerators,
- final MavenProject project, final boolean inspectDependencies) {
- this(buildContext, yangFilesRootDir, excludedFiles, codeGenerators, project, inspectDependencies,
- YangProvider.getInstance());
+ final Collection<File> excludedFiles, final List<FileGeneratorArg> fileGenerators,
+ final MavenProject project, final boolean inspectDependencies) {
+ this(buildContext, yangFilesRootDir, excludedFiles, fileGenerators, project, inspectDependencies,
+ YANG_PROVIDER);
}
- public void execute() throws MojoExecutionException, MojoFailureException {
- conditionalExecute(false);
- }
+ void execute() throws MojoExecutionException, MojoFailureException {
+ YangToSourcesState prevState;
+ try {
+ prevState = stateStorage.loadState();
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to restore execution state", e);
+ }
+ if (prevState == null) {
+ LOG.debug("{} no previous execution state present", LOG_PREFIX);
+ prevState = new YangToSourcesState(ImmutableMap.of(),
+ FileStateSet.empty(), FileStateSet.empty(), FileStateSet.empty());
+ }
- 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.
- */
+ // Collect all files in the current project.
final List<File> yangFilesInProject;
try {
yangFilesInProject = listFiles(yangFilesRootDir, excludedFiles);
if (yangFilesInProject.isEmpty()) {
// No files to process, skip.
LOG.info("{} No input files found", LOG_PREFIX);
+ wipeAllState(prevState);
return;
}
// We need to instantiate all code generators to determine required import resolution mode
- final List<Entry<CodeGeneratorArg, BasicCodeGenerator>> codeGenerators = instantiateGenerators();
- final StatementParserMode importMode = determineRequiredImportMode(codeGenerators);
- final Optional<ProcessorModuleReactor> optReactor = createReactor(importMode, yangFilesInProject);
- if (!optReactor.isPresent()) {
+ final var codeGenerators = instantiateGenerators();
+ if (codeGenerators.isEmpty()) {
+ LOG.warn("{} No code generators provided", LOG_PREFIX);
+ wipeAllState(prevState);
return;
}
- final ProcessorModuleReactor reactor = optReactor.get();
- if (!skip) {
+ LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
+
+ // All files which affect YANG context. This minimally includes all files in the current project, but optionally
+ // may include any YANG files in the dependencies.
+ final List<ScannedDependency> dependencies;
+ if (inspectDependencies) {
final Stopwatch watch = Stopwatch.createStarted();
- final ContextHolder holder;
+ try {
+ dependencies = ScannedDependency.scanDependencies(project);
+ } 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 = List.of();
+ }
+
+ // Determine hash/size of YANG input files and dependencies in parallel
+ final var hashTimer = Stopwatch.createStarted();
+ final var projectYangs = new FileStateSet(yangFilesInProject.parallelStream()
+ .map(file -> {
+ try {
+ return FileState.ofFile(file);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to read " + file, e);
+ }
+ })
+ .collect(ImmutableMap.toImmutableMap(FileState::path, Function.identity())));
+ // TODO: this produces false positives for Jar files -- there we want to capture the contents of the YANG files,
+ // not the entire file
+ final var dependencyYangs = new FileStateSet(dependencies.parallelStream()
+ .map(ScannedDependency::file)
+ .map(file -> {
+ try {
+ return FileState.ofFile(file);
+ } catch (IOException e) {
+ throw new IllegalStateException("Failed to read " + file, e);
+ }
+ })
+ .collect(ImmutableMap.toImmutableMap(FileState::path, Function.identity())));
+ LOG.debug("{} Input state determined in {}", LOG_PREFIX, hashTimer);
+
+ // We have collected our current inputs and previous state. Instantiate a support object which will guide us for
+ // the rest of the way.
+ final var buildSupport = new IncrementalBuildSupport(prevState,
+ codeGenerators.stream()
+ .collect(ImmutableMap.toImmutableMap(GeneratorTask::getIdentifier, GeneratorTask::arg)),
+ projectYangs, dependencyYangs);
+
+ // Check if any inputs changed, which is supposed to be fast. If they did not, we need to also validate our
+ // our previous are also up-to-date.
+ if (!buildSupport.inputsChanged()) {
+ final boolean outputsChanged;
+ try {
+ outputsChanged = buildSupport.outputsChanged(projectBuildDirectory);
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to reconcile generation outputs", e);
+ }
+ if (!outputsChanged) {
+ // FIXME: YANGTOOLS-745: still need to add all resources/directories to maven project
+ LOG.info("{}: Everything is up to date, nothing to do", LOG_PREFIX);
+ return;
+ }
+ }
+
+ final Stopwatch watch = Stopwatch.createStarted();
+
+ final List<Entry<YangTextSource, YangIRSchemaSource>> parsed = yangFilesInProject.parallelStream()
+ .map(file -> {
+ final var textSource = YangTextSource.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);
+
+ final var outputFiles = ImmutableList.<FileState>builder();
+ Collection<YangTextSource> modelsInProject = null;
+ for (var parserConfig : codeGenerators.stream().map(GeneratorTask::parserConfig).collect(Collectors.toSet())) {
+ final var moduleReactor = createReactor(yangFilesInProject, parserConfig, dependencies, parsed);
+ final var yangSw = Stopwatch.createStarted();
+
+ final ContextHolder holder;
try {
- holder = reactor.toContext();
+ holder = moduleReactor.toContext();
} catch (YangParserException e) {
- throw new MojoFailureException("Failed to process reactor " + reactor, e);
+ throw new MojoFailureException("Failed to process reactor " + moduleReactor, e);
} catch (IOException e) {
- throw new MojoExecutionException("Failed to read reactor " + reactor, e);
+ throw new MojoExecutionException("Failed to read reactor " + moduleReactor, e);
}
+ LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(), yangSw);
- LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(), watch);
- generateSources(holder, codeGenerators);
- } else {
- LOG.info("{} Skipping YANG code generation because property yang.skip is true", LOG_PREFIX);
+ for (var factory : codeGenerators) {
+ if (!parserConfig.equals(factory.parserConfig())) {
+ continue;
+ }
+
+ final var genSw = Stopwatch.createStarted();
+ final List<FileState> files;
+ try {
+ files = factory.execute(project, buildContext, holder);
+ } catch (FileGeneratorException e) {
+ throw new MojoFailureException(LOG_PREFIX + " Generator " + factory + " failed", e);
+ }
+
+ outputFiles.addAll(files);
+ LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, factory.generatorName(), files.size(),
+ genSw);
+ }
+
+ if (modelsInProject == null) {
+ // FIXME: this is an invariant, we should prepare these separately
+ modelsInProject = moduleReactor.getModelsInProject();
+ }
}
- // add META_INF/yang
- final Collection<YangTextSchemaSource> models = reactor.getModelsInProject();
+ // add META_INF/yang once
try {
- yangProvider.addYangsToMetaInf(project, models);
+ outputFiles.addAll(yangProvider.addYangsToMetaInf(project, modelsInProject));
} catch (IOException e) {
- throw new MojoExecutionException("Failed write model files for " + models, e);
+ throw new MojoExecutionException("Failed write model files for " + modelsInProject, e);
}
// add META_INF/services
- File generatedServicesDir = new GeneratedDirectories(project).getYangServicesDir();
- YangProvider.setResource(generatedServicesDir, project);
+ File generatedServicesDir = new File(new File(projectBuildDirectory, "generated-sources"), "spi");
+ ProjectFileAccess.addResourceDir(project, generatedServicesDir);
LOG.debug("{} Yang services files from: {} marked as resources: {}", LOG_PREFIX, generatedServicesDir,
META_INF_YANG_SERVICES_STRING_JAR);
- }
- private static StatementParserMode determineRequiredImportMode(
- final Collection<Entry<CodeGeneratorArg, BasicCodeGenerator>> codeGenerators) throws MojoFailureException {
- ImportResolutionMode requestedMode = null;
- BasicCodeGenerator requestingGenerator = null;
-
- for (Entry<CodeGeneratorArg, BasicCodeGenerator> entry : codeGenerators) {
- final BasicCodeGenerator generator = entry.getValue();
- final ImportResolutionMode mode = generator.getImportResolutionMode();
- if (mode == null) {
- // the generator does not care about the mode
- continue;
+ final var uniqueOutputFiles = new LinkedHashMap<String, FileState>();
+ for (var fileHash : outputFiles.build()) {
+ final var prev = uniqueOutputFiles.putIfAbsent(fileHash.path(), fileHash);
+ if (prev != null) {
+ throw new MojoFailureException("Duplicate files " + prev + " and " + fileHash);
}
+ }
- if (requestedMode == null) {
- // No mode yet, we have just determined it
- requestedMode = mode;
- requestingGenerator = generator;
- continue;
- }
+ // Reconcile push output files into project directory and acquire the execution state
+ final YangToSourcesState outputState;
+ try {
+ outputState = buildSupport.reconcileOutputFiles(buildContext, projectBuildDirectory, uniqueOutputFiles);
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to reconcile output files", e);
+ }
- if (mode != requestedMode) {
- throw new MojoFailureException(String.format(
- "Import resolution mode conflict between %s (%s) and %s (%s)", requestingGenerator, requestedMode,
- generator, mode));
- }
+ // Store execution state
+ try {
+ stateStorage.storeState(outputState);
+ } catch (IOException e) {
+ throw new MojoFailureException("Failed to store execution state", e);
}
+ }
- if (requestedMode == null) {
- return StatementParserMode.DEFAULT_MODE;
+ private void wipeAllState(final YangToSourcesState prevState) throws MojoExecutionException {
+ try {
+ prevState.deleteOutputFiles();
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to delete output files", e);
}
- switch (requestedMode) {
- case REVISION_EXACT_OR_LATEST:
- return StatementParserMode.DEFAULT_MODE;
- case SEMVER_LATEST:
- return StatementParserMode.SEMVER_MODE;
- default:
- throw new IllegalStateException("Unhandled import resolution mode " + requestedMode);
+ try {
+ stateStorage.deleteState();
+ } catch (IOException e) {
+ throw new MojoExecutionException("Failed to remove execution state", e);
}
}
- private List<Entry<CodeGeneratorArg, BasicCodeGenerator>> instantiateGenerators() throws MojoExecutionException {
- final List<Entry<CodeGeneratorArg, BasicCodeGenerator>> generators = new ArrayList<>(codeGeneratorArgs.size());
- for (CodeGeneratorArg arg : codeGeneratorArgs) {
- arg.check();
+ private ImmutableList<@NonNull GeneratorTask> instantiateGenerators() throws MojoExecutionException {
+ // Search for available FileGenerator implementations
+ final var factories = Maps.uniqueIndex(
+ ServiceLoader.load(FileGeneratorFactory.class), FileGeneratorFactory::getIdentifier);
+
+ // FIXME: iterate over fileGeneratorArg instances (configuration), not factories (environment)
+ // Assign instantiate FileGenerators with appropriate configuration
+ final var builder = ImmutableList.<@NonNull GeneratorTask>builderWithExpectedSize(factories.size());
+ for (var entry : factories.entrySet()) {
+ final var id = entry.getKey();
+ var arg = fileGeneratorArgs.get(id);
+ if (arg == null) {
+ LOG.debug("{} No configuration for {}, using empty", LOG_PREFIX, id);
+ arg = new FileGeneratorArg(id);
+ }
- final BasicCodeGenerator generator;
+ final GeneratorTask task;
try {
- generator = getInstance(arg.getCodeGeneratorClass(), BasicCodeGenerator.class);
- } catch (ReflectiveOperationException e) {
- throw new MojoExecutionException("Failed to instantiate code generator "
- + arg.getCodeGeneratorClass(), e);
+ task = new GeneratorTask(entry.getValue(), arg);
+ } catch (FileGeneratorException e) {
+ throw new MojoExecutionException("File generator " + id + " failed", e);
}
-
- LOG.info("{} Code generator instantiated from {}", LOG_PREFIX, arg.getCodeGeneratorClass());
- generators.add(new SimpleImmutableEntry<>(arg, generator));
+ builder.add(task);
+ LOG.info("{} Code generator {} instantiated", LOG_PREFIX, id);
}
- return generators;
+ // Notify if no factory found for defined identifiers
+ fileGeneratorArgs.keySet().forEach(
+ fileGenIdentifier -> {
+ if (!factories.containsKey(fileGenIdentifier)) {
+ LOG.warn("{} No generator found for identifier {}", LOG_PREFIX, fileGenIdentifier);
+ }
+ }
+ );
+
+ return builder.build();
}
@SuppressWarnings("checkstyle:illegalCatch")
- private Optional<ProcessorModuleReactor> createReactor(final StatementParserMode parserMode,
- final List<File> yangFilesInProject) throws MojoExecutionException {
- LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
+ private @NonNull ProcessorModuleReactor createReactor(final List<File> yangFilesInProject,
+ final YangParserConfiguration parserConfig, final Collection<ScannedDependency> dependencies,
+ final List<Entry<YangTextSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
try {
- final Collection<File> allFiles = new ArrayList<>(yangFilesInProject);
- final Collection<ScannedDependency> dependencies;
- if (inspectDependencies) {
- dependencies = new ArrayList<>();
- final Stopwatch watch = Stopwatch.createStarted();
- ScannedDependency.scanDependencies(project).forEach(dep -> {
- allFiles.add(dep.file());
- dependencies.add(dep);
- });
- 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 null, which indicates and of execution.
- */
- final Stopwatch watch = Stopwatch.createStarted();
- if (!allFiles.stream().anyMatch(buildContext::hasDelta)) {
- LOG.info("{} None of {} input files changed", LOG_PREFIX, allFiles.size());
- return Optional.empty();
- }
-
- final YangParser parser = parserFactory.createParser(parserMode);
- final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
-
- final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed = yangFilesInProject.parallelStream()
- .map(file -> {
- final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(file);
- try {
- return new SimpleImmutableEntry<>(textSource,
- TextToIRTransformer.transformText(textSource));
- } catch (YangSyntaxErrorException | IOException e) {
- throw new IllegalArgumentException("Failed to parse " + file, e);
- }
- })
- .collect(Collectors.toList());
-
- for (final Entry<YangTextSchemaSource, IRSchemaSource> entry : parsed) {
- final YangTextSchemaSource textSource = entry.getKey();
- final IRSchemaSource astSource = entry.getValue();
+ final var sourcesInProject = new ArrayList<YangTextSource>(yangFilesInProject.size());
+ final var parser = parserFactory.createParser(parserConfig);
+ for (var entry : parsed) {
+ final var textSource = entry.getKey();
+ final var astSource = entry.getValue();
parser.addSource(astSource);
- if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
+ if (!astSource.sourceId().equals(textSource.sourceId())) {
// AST indicates a different source identifier, make sure we use that
- sourcesInProject.add(YangTextSchemaSource.delegateForByteSource(astSource.getIdentifier(),
- textSource));
+ sourcesInProject.add(new DelegatedYangTextSource(astSource.sourceId(), textSource));
} else {
sourcesInProject.add(textSource);
}
}
- LOG.debug("Found project files: {}", yangFilesInProject);
- LOG.info("{} Project model files found: {} in {}", LOG_PREFIX, yangFilesInProject.size(), watch);
-
- final ProcessorModuleReactor reactor = new ProcessorModuleReactor(parser, sourcesInProject, dependencies);
- LOG.debug("Initialized reactor {} with {}", reactor, yangFilesInProject);
- return Optional.of(reactor);
+ final var moduleReactor = new ProcessorModuleReactor(parser, sourcesInProject, dependencies);
+ LOG.debug("Initialized reactor {} with {}", moduleReactor, yangFilesInProject);
+ return moduleReactor;
} 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);
+ Throwables.getRootCause(e));
}
}
- private static List<File> listFiles(final File root, final Collection<File> excludedFiles)
+ private static ImmutableList<File> listFiles(final File root, final Collection<File> excludedFiles)
throws IOException {
if (!root.isDirectory()) {
LOG.warn("{} YANG source directory {} not found. No code will be generated.", LOG_PREFIX, root);
return ImmutableList.of();
}
- return Files.walk(root.toPath()).map(Path::toFile).filter(File::isFile).filter(f -> {
- if (excludedFiles.contains(f)) {
- LOG.info("{} YANG file excluded {}", LOG_PREFIX, f);
- return false;
- }
- return true;
- }).filter(f -> f.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)).collect(Collectors.toList());
- }
-
- /**
- * Call generate on every generator from plugin configuration.
- */
- @SuppressWarnings("checkstyle:illegalCatch")
- private void generateSources(final ContextHolder context,
- final Collection<Entry<CodeGeneratorArg, BasicCodeGenerator>> generators) throws MojoFailureException {
- if (generators.isEmpty()) {
- LOG.warn("{} No code generators provided", LOG_PREFIX);
- return;
- }
-
- final Map<String, String> thrown = new HashMap<>();
- for (Entry<CodeGeneratorArg, BasicCodeGenerator> entry : generators) {
- final String codeGeneratorClass = entry.getKey().getCodeGeneratorClass();
-
- try {
- generateSourcesWithOneGenerator(context, entry.getKey(), entry.getValue());
- } catch (Exception e) {
- // try other generators, exception will be thrown after
- LOG.error("{} Unable to generate sources with {} generator", LOG_PREFIX, codeGeneratorClass, e);
- thrown.put(codeGeneratorClass, e.getClass().getCanonicalName());
- }
- }
-
- if (!thrown.isEmpty()) {
- LOG.error("{} One or more code generators failed, including failed list(generatorClass=exception) {}",
- LOG_PREFIX, thrown);
- throw new MojoFailureException(LOG_PREFIX
- + " One or more code generators failed, including failed list(generatorClass=exception) " + thrown);
- }
- }
-
- /**
- * Complete initialization of a code generator and invoke it.
- */
- private void generateSourcesWithOneGenerator(final ContextHolder context, final CodeGeneratorArg codeGeneratorCfg,
- final BasicCodeGenerator codeGenerator) throws IOException {
-
- final File outputDir = requireNonNull(codeGeneratorCfg.getOutputBaseDir(project),
- "outputBaseDir is null. Please provide a valid outputBaseDir value in pom.xml");
-
- project.addCompileSourceRoot(outputDir.getAbsolutePath());
-
- LOG.info("{} Sources will be generated to {}", LOG_PREFIX, outputDir);
- LOG.debug("{} Project root dir is {}", LOG_PREFIX, project.getBasedir());
- LOG.debug("{} Additional configuration picked up for : {}: {}", LOG_PREFIX, codeGeneratorCfg
- .getCodeGeneratorClass(), codeGeneratorCfg.getAdditionalConfiguration());
-
- if (codeGenerator instanceof BuildContextAware) {
- ((BuildContextAware)codeGenerator).setBuildContext(buildContext);
- }
- if (codeGenerator instanceof MavenProjectAware) {
- ((MavenProjectAware)codeGenerator).setMavenProject(project);
- }
- codeGenerator.setAdditionalConfig(codeGeneratorCfg.getAdditionalConfiguration());
-
- File resourceBaseDir = codeGeneratorCfg.getResourceBaseDir(project);
- YangProvider.setResource(resourceBaseDir, project);
- codeGenerator.setResourceBaseDir(resourceBaseDir);
- LOG.debug("{} Folder: {} marked as resources for generator: {}", LOG_PREFIX, resourceBaseDir,
- codeGeneratorCfg.getCodeGeneratorClass());
-
- if (outputDir.exists()) {
- Files.walk(outputDir.toPath()).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
- LOG.info("{} Succesfully deleted output directory {}", LOG_PREFIX, outputDir);
- }
- final Stopwatch watch = Stopwatch.createStarted();
- Collection<File> generated = codeGenerator.generateSources(context.getContext(), outputDir,
- context.getYangModules(), context);
-
- LOG.debug("{} Sources generated by {}: {}", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(), generated);
- LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(),
- generated == null ? 0 : generated.size(), watch);
+ return Files.walk(root.toPath())
+ .map(Path::toFile)
+ .filter(File::isFile)
+ .filter(f -> {
+ if (excludedFiles.contains(f)) {
+ LOG.info("{} YANG file excluded {}", LOG_PREFIX, f);
+ return false;
+ }
+ return true;
+ })
+ .filter(f -> f.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION))
+ .collect(ImmutableList.toImmutableList());
}
- /**
- * Instantiate object from fully qualified class name.
- */
- private static <T> T getInstance(final String codeGeneratorClass, final Class<T> baseType)
- throws ReflectiveOperationException {
- final Class<?> clazz = Class.forName(codeGeneratorClass);
- checkArgument(baseType.isAssignableFrom(clazz), "Code generator %s has to implement %s", clazz, baseType);
- return baseType.cast(clazz.getConstructor().newInstance());
+ @VisibleForTesting
+ static @NonNull Path stateFilePath(final String projectBuildDirectory) {
+ // ${project.build.directory}/maven-status/yang-maven-plugin/execution.state
+ return Path.of(projectBuildDirectory, "maven-status", "yang-maven-plugin", "execution.state");
}
}