Simplify YangProvider 98/104798/2
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 23:10:53 +0000 (00:10 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Mar 2023 23:22:00 +0000 (00:22 +0100)
This is a stateless abstract class. Turn it it into a simple functional
interface and move the default implementation to YangToSourcesProcessor,
simplifying test invocations.

Change-Id: Iaaf1d76092bc3a52d7364363a2852309e44f9648
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangProvider.java
plugin/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessorTest.java

index 571f6f8d58da1d382c354ca82c885a84a44cff8d..29dfb8f9a3e68d1b32ca53d60d945c7f3bac66db 100644 (file)
@@ -7,56 +7,16 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import com.google.common.collect.ImmutableList;
-import com.google.common.io.Files;
-import java.io.File;
+import com.google.common.annotations.VisibleForTesting;
 import java.io.IOException;
 import java.util.Collection;
 import org.apache.maven.project.MavenProject;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-abstract class YangProvider {
-    private static final class Default extends YangProvider {
-        private static final Logger LOG = LoggerFactory.getLogger(Default.class);
+@FunctionalInterface
+@VisibleForTesting
+interface YangProvider {
 
-        @Override
-        Collection<FileState> addYangsToMetaInf(final MavenProject project,
-                final Collection<YangTextSchemaSource> modelsInProject) throws IOException {
-
-            final File 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 File withMetaInf = new File(generatedYangDir, YangToSourcesProcessor.META_INF_YANG_STRING);
-            final var stateListBuilder = ImmutableList.<FileState>builderWithExpectedSize(modelsInProject.size());
-
-            for (YangTextSchemaSource source : modelsInProject) {
-                final File file = new File(withMetaInf, source.getIdentifier().toYangFilename());
-                Files.createParentDirs(file);
-
-                stateListBuilder.add(FileState.ofWrittenFile(file, source::copyTo));
-                LOG.debug("Created file {} for {}", file, source.getIdentifier());
-            }
-
-            ProjectFileAccess.addResourceDir(project, generatedYangDir);
-            LOG.debug("{} YANG files marked as resources: {}", YangToSourcesProcessor.LOG_PREFIX, generatedYangDir);
-
-            return stateListBuilder.build();
-        }
-    }
-
-    private static final YangProvider DEFAULT = new Default();
-
-    static YangProvider getInstance() {
-        return DEFAULT;
-    }
-
-    abstract Collection<FileState> addYangsToMetaInf(MavenProject project,
+    Collection<FileState> addYangsToMetaInf(MavenProject project,
             Collection<YangTextSchemaSource> modelsInProject) throws IOException;
 }
index 0bbdf903d63e48de272eb634f19fb8e668240f7a..5e9067fddd07868762c4960fa81b93ae6f3fa66b 100644 (file)
@@ -42,7 +42,6 @@ import org.opendaylight.yangtools.plugin.generator.api.FileGeneratorFactory;
 import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.model.repo.api.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.api.YangParser;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.api.YangParserException;
 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
@@ -63,14 +62,41 @@ class YangToSourcesProcessor {
         DEFAULT_PARSER_FACTORY = it.next();
     }
 
-    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);
+        final var stateListBuilder = ImmutableList.<FileState>builderWithExpectedSize(modelsInProject.size());
+
+        for (var source : modelsInProject) {
+            final File file = new File(withMetaInf, source.getIdentifier().toYangFilename());
+            // FIXME: ditch this use
+            com.google.common.io.Files.createParentDirs(file);
+
+            stateListBuilder.add(FileState.ofWrittenFile(file, source::copyTo));
+            LOG.debug("Created file {} for {}", file, source.getIdentifier());
+        }
+
+        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;
@@ -108,7 +134,7 @@ class YangToSourcesProcessor {
             final Collection<File> excludedFiles, final List<FileGeneratorArg> fileGenerators,
             final MavenProject project, final boolean inspectDependencies) {
         this(buildContext, yangFilesRootDir, excludedFiles, fileGenerators, project, inspectDependencies,
-            YangProvider.getInstance());
+            YANG_PROVIDER);
     }
 
     void execute() throws MojoExecutionException, MojoFailureException {
@@ -338,11 +364,11 @@ class YangToSourcesProcessor {
             final List<Entry<YangTextSchemaSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
 
         try {
-            final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
-            final YangParser parser = parserFactory.createParser(parserConfig);
-            for (final Entry<YangTextSchemaSource, YangIRSchemaSource> entry : parsed) {
-                final YangTextSchemaSource textSource = entry.getKey();
-                final YangIRSchemaSource astSource = entry.getValue();
+            final var sourcesInProject = new ArrayList<YangTextSchemaSource>(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())) {
@@ -354,15 +380,14 @@ class YangToSourcesProcessor {
                 }
             }
 
-            final ProcessorModuleReactor reactor = new ProcessorModuleReactor(parser, sourcesInProject, dependencies);
-            LOG.debug("Initialized reactor {} with {}", reactor, yangFilesInProject);
-            return 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));
         }
     }
 
index b9ee3f879bda23ef34349bcc19eb84bd400c7ee9..f18efcdcc1c8c7314fc2b9066b62aa1c3a1641ab 100644 (file)
@@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.junit.jupiter.MockitoExtension;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.sonatype.plexus.build.incremental.DefaultBuildContext;
 
 @ExtendWith(MockitoExtension.class)
 class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
@@ -27,11 +28,11 @@ class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
     @Test
     void basicTest() {
         assertMojoExecution(
-            new YangToSourcesProcessor(file, List.of(), List.of(new FileGeneratorArg("mockGenerator")), project, true,
-                YangProvider.getInstance()),
+            new YangToSourcesProcessor(new DefaultBuildContext(), file, List.of(),
+                List.of(new FileGeneratorArg("mockGenerator")), project, true),
             mock -> {
                 doAnswer(invocation -> {
-                    final Set<Module> localModules = invocation.getArgument(1);
+                    final var localModules = invocation.<Set<Module>>getArgument(1);
                     assertEquals(2, localModules.size());
                     return ImmutableTable.of();
                 }).when(mock).generateFiles(any(), any(), any());
@@ -46,11 +47,11 @@ class YangToSourcesProcessorTest extends AbstractCodeGeneratorTest {
         final var excludedYang = new File(getClass().getResource("/yang/excluded-file.yang").getFile());
 
         assertMojoExecution(
-            new YangToSourcesProcessor(file, List.of(excludedYang), List.of(new FileGeneratorArg("mockGenerator")),
-                project, true, YangProvider.getInstance()),
+            new YangToSourcesProcessor(new DefaultBuildContext(), file, List.of(excludedYang),
+                List.of(new FileGeneratorArg("mockGenerator")), project, true),
             mock -> {
                 doAnswer(invocation -> {
-                    final Set<Module> localModules = invocation.getArgument(1);
+                    final var localModules = invocation.<Set<Module>>getArgument(1);
                     assertEquals(1, localModules.size());
                     assertEquals("mock", localModules.iterator().next().getName());
                     return ImmutableTable.of();