import com.google.common.collect.ImmutableSet;
import java.util.Optional;
import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.plugin.generator.api.ModuleResourceResolver;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
final class ContextHolder implements Immutable, ModuleResourceResolver {
- private final EffectiveModelContext context;
- private final Set<Module> modules;
- private final Set<SourceIdentifier> sources;
+ private final @NonNull EffectiveModelContext context;
+ private final @NonNull ImmutableSet<Module> modules;
+ private final ImmutableSet<SourceIdentifier> sources;
ContextHolder(final EffectiveModelContext context, final Set<Module> modules, final Set<SourceIdentifier> sources) {
this.context = requireNonNull(context);
: Optional.empty();
}
- EffectiveModelContext getContext() {
+ @NonNull EffectiveModelContext getContext() {
return context;
}
- Set<Module> getYangModules() {
+ @NonNull ImmutableSet<Module> getYangModules() {
return modules;
}
}
import static java.util.Objects.requireNonNull;
+import com.google.common.base.MoreObjects;
import com.google.common.base.Stopwatch;
-import com.google.common.collect.ListMultimap;
import com.google.common.collect.MultimapBuilder;
-import com.google.common.collect.Table;
-import com.google.common.collect.Table.Cell;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.stream.Collectors;
-import org.eclipse.jdt.annotation.NonNull;
+import org.apache.maven.project.MavenProject;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.concepts.Identifiable;
import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
+import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
import org.opendaylight.yangtools.plugin.generator.api.GeneratedFile;
-import org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath;
-import org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType;
+import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-final class GeneratorTask {
+@NonNullByDefault
+final class GeneratorTask implements Identifiable<String> {
private static final Logger LOG = LoggerFactory.getLogger(GeneratorTask.class);
- private final @NonNull GeneratorTaskFactory factory;
- private final @NonNull ContextHolder contextHolder;
- private final @NonNull ProjectFileAccess access;
+ private final YangParserConfiguration parserConfig;
+ private final FileGeneratorArg arg;
+ private final FileGenerator gen;
- GeneratorTask(final @NonNull GeneratorTaskFactory factory, final @NonNull ContextHolder contextHolder,
- final ProjectFileAccess access) {
- this.factory = requireNonNull(factory);
- this.contextHolder = requireNonNull(contextHolder);
- this.access = requireNonNull(access);
+ GeneratorTask(final FileGeneratorFactory factory, final FileGeneratorArg arg) throws FileGeneratorException {
+ this.arg = requireNonNull(arg);
+ gen = factory.newFileGenerator(arg.getConfiguration());
+ parserConfig = switch (gen.importResolutionMode()) {
+ case REVISION_EXACT_OR_LATEST -> YangParserConfiguration.DEFAULT;
+ };
}
- List<FileState> execute() throws FileGeneratorException, IOException {
+ @Override
+ public String getIdentifier() {
+ return arg.getIdentifier();
+ }
+
+ YangParserConfiguration parserConfig() {
+ return parserConfig;
+ }
+
+ FileGeneratorArg arg() {
+ return arg;
+ }
+
+ String generatorName() {
+ return gen.getClass().getName();
+ }
+
+ /**
+ * Create a new {@link GeneratorTask} which will work in scope of specified {@link MavenProject} with the effective
+ * model held in specified {@link ContextHolder}.
+ *
+ * @param project current Maven Project
+ * @param context model generation context
+ * @return {@link FileState} for every generated file
+ * @throws FileGeneratorException if the underlying generator fails
+ * @throws IOException when a generated file cannot be written
+ */
+ List<FileState> execute(final MavenProject project, final ContextHolder context)
+ throws FileGeneratorException, IOException {
+ final var access = new ProjectFileAccess(project, getIdentifier());
+
// Step one: determine what files are going to be generated
- final Stopwatch sw = Stopwatch.createStarted();
- final FileGenerator gen = factory.generator();
- final Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> generatedFiles = gen.generateFiles(
- contextHolder.getContext(), contextHolder.getYangModules(), contextHolder);
- LOG.info("{}: Defined {} files in {}", factory.getIdentifier(), generatedFiles.size(), sw);
+ final var sw = Stopwatch.createStarted();
+ final var generatedFiles = gen.generateFiles(context.getContext(), context.getYangModules(), context);
+ LOG.info("{}: Defined {} files in {}", getIdentifier(), generatedFiles.size(), sw);
// Step two: create generation tasks for each target file and group them by parent directory
sw.reset().start();
- final ListMultimap<File, WriteTask> dirs = MultimapBuilder.hashKeys().arrayListValues().build();
- for (Cell<GeneratedFileType, GeneratedFilePath, GeneratedFile> cell : generatedFiles.cellSet()) {
+ final var dirs = MultimapBuilder.hashKeys().arrayListValues().<File, WriteTask>build();
+ for (var cell : generatedFiles.cellSet()) {
final GeneratedFile file = cell.getValue();
final String relativePath = cell.getColumnKey().getPath();
final File target;
return result;
}
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this).add("generator", generatorName()).add("argument", arg).toString();
+ }
+
private static final class WriteTask {
private final GeneratedFile file;
private final File target;
+++ /dev/null
-/*
- * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang2sources.plugin;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.base.MoreObjects;
-import java.io.IOException;
-import java.util.List;
-import org.apache.maven.project.MavenProject;
-import org.eclipse.jdt.annotation.NonNullByDefault;
-import org.opendaylight.yangtools.concepts.Identifiable;
-import org.opendaylight.yangtools.plugin.generator.api.FileGenerator;
-import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorException;
-import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
-import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
-
-@NonNullByDefault
-final class GeneratorTaskFactory implements Identifiable<String> {
- private final YangParserConfiguration parserConfig;
- private final FileGeneratorArg arg;
- private final FileGenerator gen;
-
- private GeneratorTaskFactory(final FileGenerator gen, final FileGeneratorArg arg) {
- this.gen = requireNonNull(gen);
- this.arg = requireNonNull(arg);
- parserConfig = switch (gen.importResolutionMode()) {
- case REVISION_EXACT_OR_LATEST -> YangParserConfiguration.DEFAULT;
- };
- }
-
- static GeneratorTaskFactory of(final FileGeneratorFactory factory, final FileGeneratorArg arg)
- throws FileGeneratorException {
- return new GeneratorTaskFactory(factory.newFileGenerator(arg.getConfiguration()), arg);
- }
-
- @Override
- public String getIdentifier() {
- return arg.getIdentifier();
- }
-
- YangParserConfiguration parserConfig() {
- return parserConfig;
- }
-
- FileGeneratorArg arg() {
- return arg;
- }
-
- FileGenerator generator() {
- return gen;
- }
-
- String generatorName() {
- return gen.getClass().getName();
- }
-
- /**
- * Create a new {@link GeneratorTask} which will work in scope of specified {@link MavenProject} with the effective
- * model held in specified {@link ContextHolder}.
- *
- * @param project current Maven Project
- * @param context model generation context
- * @return {@link FileState} for every generated file
- * @throws FileGeneratorException if the underlying {@link #generator()} fails
- * @throws IOException when a generated file cannot be written
- */
- List<FileState> execute(final MavenProject project, final ContextHolder context)
- throws FileGeneratorException, IOException {
- return new GeneratorTask(this, context, new ProjectFileAccess(project, getIdentifier())).execute();
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this).add("generator", generatorName()).add("argument", arg).toString();
- }
-}
}
// We need to instantiate all code generators to determine required import resolution mode
- final List<GeneratorTaskFactory> codeGenerators = instantiateGenerators();
+ final List<GeneratorTask> codeGenerators = instantiateGenerators();
if (codeGenerators.isEmpty()) {
LOG.warn("{} No code generators provided", LOG_PREFIX);
return;
}
final Set<YangParserConfiguration> parserConfigs = codeGenerators.stream()
- .map(GeneratorTaskFactory::parserConfig)
+ .map(GeneratorTask::parserConfig)
.collect(Collectors.toUnmodifiableSet());
LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
final var outputState = new YangToSourcesState(
codeGenerators.stream()
- .collect(ImmutableMap.toImmutableMap(GeneratorTaskFactory::getIdentifier, GeneratorTaskFactory::arg)),
+ .collect(ImmutableMap.toImmutableMap(GeneratorTask::getIdentifier, GeneratorTask::arg)),
projectYangs, dependencyYangs, new FileStateSet(ImmutableMap.copyOf(uniqueOutputFiles)));
try {
}
}
- private List<GeneratorTaskFactory> instantiateGenerators() throws MojoExecutionException {
+ private List<GeneratorTask> instantiateGenerators() throws MojoExecutionException {
// Search for available FileGenerator implementations
final Map<String, FileGeneratorFactory> 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 generators = new ArrayList<GeneratorTaskFactory>(factories.size());
+ final var generators = new ArrayList<GeneratorTask>(factories.size());
for (Entry<String, FileGeneratorFactory> entry : factories.entrySet()) {
final String id = entry.getKey();
FileGeneratorArg arg = fileGeneratorArgs.get(id);
arg = new FileGeneratorArg(id);
}
+ final GeneratorTask task;
try {
- generators.add(GeneratorTaskFactory.of(entry.getValue(), arg));
+ task = new GeneratorTask(entry.getValue(), arg);
} catch (FileGeneratorException e) {
throw new MojoExecutionException("File generator " + id + " failed", e);
}
+ generators.add(task);
LOG.info("{} Code generator {} instantiated", LOG_PREFIX, id);
}