Merge GeneratorTask(Factory) 95/104795/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 21:39:10 +0000 (22:39 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 21:51:03 +0000 (22:51 +0100)
GeneratorTask is hidden behind a mostly-useless factory, merge the two
concepts to simplify interactions. This enables us to better integrate
with build cycle.

JIRA: YANGTOOLS-745
Change-Id: I30db56e60c69ee2893999e41764ca6a6071e632b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ContextHolder.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratorTask.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratorTaskFactory.java [deleted file]
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java

index 1a1c4ed39cee91fbefb931ff65643165727be743..aab8f5e2d3347b647e1999f2dd28d95f3b13c98b 100644 (file)
@@ -13,6 +13,7 @@ import static java.util.Objects.requireNonNull;
 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;
@@ -23,9 +24,9 @@ import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
 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);
@@ -44,11 +45,11 @@ final class ContextHolder implements Immutable, ModuleResourceResolver {
                         : Optional.empty();
     }
 
-    EffectiveModelContext getContext() {
+    @NonNull EffectiveModelContext getContext() {
         return context;
     }
 
-    Set<Module> getYangModules() {
+    @NonNull ImmutableSet<Module> getYangModules() {
         return modules;
     }
 }
index c7281df0cdf49dc876e938dbb9df3870792254a2..d5714545ae02931b7f971e12aae79c0177fc8726 100644 (file)
@@ -9,51 +9,81 @@ package org.opendaylight.yangtools.yang2sources.plugin;
 
 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;
@@ -98,6 +128,11 @@ final class GeneratorTask {
         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;
diff --git a/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratorTaskFactory.java b/plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/GeneratorTaskFactory.java
deleted file mode 100644 (file)
index c876629..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * 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();
-    }
-}
index 1eaed68577f81fc4fcff594cfb8ff8c462648c7b..58f9a24089468e6d870b87e4af058bb69d299db6 100644 (file)
@@ -137,14 +137,14 @@ class YangToSourcesProcessor {
         }
 
         // 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);
@@ -284,7 +284,7 @@ class YangToSourcesProcessor {
 
         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 {
@@ -294,14 +294,14 @@ class YangToSourcesProcessor {
         }
     }
 
-    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);
@@ -310,11 +310,13 @@ class YangToSourcesProcessor {
                 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);
         }