Remove MojoFailureException declaration
[yangtools.git] / plugin / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesProcessor.java
index dcc724cef0158e428e09e4b6735fb0c3c97f81b8..1b31690d8598ebe84aade2e3ab1ff2dfe1d596a8 100644 (file)
@@ -37,15 +37,14 @@ import org.apache.maven.project.MavenProject;
 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.YangIRSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.rfc7950.ir.IRSchemaSource;
+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;
+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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonatype.plexus.build.incremental.BuildContext;
@@ -72,7 +71,6 @@ class YangToSourcesProcessor {
     private final YangParserFactory parserFactory;
     private final File yangFilesRootDir;
     private final Set<File> excludedFiles;
-    private final List<CodeGeneratorArg> codeGeneratorArgs;
     private final Map<String, FileGeneratorArg> fileGeneratorArgs;
     private final MavenProject project;
     private final boolean inspectDependencies;
@@ -80,34 +78,32 @@ class YangToSourcesProcessor {
     private final YangProvider yangProvider;
 
     private YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
-            final Collection<File> excludedFiles, final List<CodeGeneratorArg> codeGeneratorArgs,
-            final List<FileGeneratorArg> fileGeneratorsArgs,
+            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(codeGeneratorArgs);
-        this.fileGeneratorArgs = Maps.uniqueIndex(fileGeneratorsArgs, FileGeneratorArg::getIdentifier);
+        //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;
+        parserFactory = DEFAULT_PARSER_FACTORY;
     }
 
     @VisibleForTesting
     YangToSourcesProcessor(final File yangFilesRootDir, final Collection<File> excludedFiles,
-            final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies,
+            final List<FileGeneratorArg> fileGenerators, final MavenProject project, final boolean inspectDependencies,
             final YangProvider yangProvider) {
-        this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, codeGenerators, ImmutableList.of(),
+        this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, ImmutableList.of(),
             project, inspectDependencies, yangProvider);
     }
 
     YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir,
-                final Collection<File> excludedFiles, final List<CodeGeneratorArg> codeGenerators,
-                final List<FileGeneratorArg> fileGenerators, final MavenProject project,
-                final boolean inspectDependencies) {
-        this(buildContext, yangFilesRootDir, excludedFiles, codeGenerators, fileGenerators, 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,
+            YangProvider.getInstance());
     }
 
     public void execute() throws MojoExecutionException, MojoFailureException {
@@ -140,8 +136,8 @@ class YangToSourcesProcessor {
             return;
         }
 
-        final Set<StatementParserMode> parserModes = codeGenerators.stream()
-            .map(GeneratorTaskFactory::parserMode)
+        final Set<YangParserConfiguration> parserConfigs = codeGenerators.stream()
+            .map(GeneratorTaskFactory::parserConfig)
             .collect(Collectors.toUnmodifiableSet());
 
         LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
@@ -176,11 +172,11 @@ class YangToSourcesProcessor {
         }
 
         final Stopwatch watch = Stopwatch.createStarted();
-        final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed = yangFilesInProject.parallelStream()
+        final List<Entry<YangTextSchemaSource, YangIRSchemaSource>> parsed = yangFilesInProject.parallelStream()
             .map(file -> {
-                final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(file);
+                final YangTextSchemaSource textSource = YangTextSchemaSource.forPath(file.toPath());
                 try {
-                    return Map.entry(textSource,TextToIRTransformer.transformText(textSource));
+                    return Map.entry(textSource, TextToIRTransformer.transformText(textSource));
                 } catch (YangSyntaxErrorException | IOException e) {
                     throw new IllegalArgumentException("Failed to parse " + file, e);
                 }
@@ -191,9 +187,9 @@ class YangToSourcesProcessor {
 
         // FIXME: store these files into state, so that we can verify/clean up
         final Builder<File> files = ImmutableSet.builder();
-        for (StatementParserMode parserMode : parserModes) {
+        for (YangParserConfiguration parserConfig : parserConfigs) {
             final Optional<ProcessorModuleReactor> optReactor = createReactor(yangFilesInProject,
-                parserMode, dependencies, parsed);
+                parserConfig, dependencies, parsed);
             if (optReactor.isPresent()) {
                 final ProcessorModuleReactor reactor = optReactor.orElseThrow();
 
@@ -211,7 +207,7 @@ class YangToSourcesProcessor {
 
                     LOG.info("{} {} YANG models processed in {}", LOG_PREFIX, holder.getContext().getModules().size(),
                         sw);
-                    files.addAll(generateSources(holder, codeGenerators, parserMode));
+                    files.addAll(generateSources(holder, codeGenerators, parserConfig));
                 } else {
                     LOG.info("{} Skipping YANG code generation because property yang.skip is true", LOG_PREFIX);
                 }
@@ -234,18 +230,14 @@ class YangToSourcesProcessor {
             META_INF_YANG_SERVICES_STRING_JAR);
     }
 
-    private List<GeneratorTaskFactory> instantiateGenerators() throws MojoExecutionException, MojoFailureException {
-        final List<GeneratorTaskFactory> generators = new ArrayList<>(codeGeneratorArgs.size());
-        for (CodeGeneratorArg arg : codeGeneratorArgs) {
-            generators.add(CodeGeneratorTaskFactory.create(arg));
-            LOG.info("{} Code generator instantiated from {}", LOG_PREFIX, arg.getCodeGeneratorClass());
-        }
-
+    private List<GeneratorTaskFactory> instantiateGenerators() throws MojoExecutionException {
         // Search for available FileGenerator implementations
         final Map<String, FileGeneratorFactory> factories = Maps.uniqueIndex(
             ServiceLoader.load(FileGeneratorFactory.class), FileGeneratorFactory::getIdentifier);
 
-        // Assign instantiate FileGenerators with appropriate configurate
+        // FIXME: iterate over fileGeneratorArg instances (configuration), not factories (environment)
+        // Assign instantiate FileGenerators with appropriate configuration
+        final List<GeneratorTaskFactory> generators = new ArrayList<>(factories.size());
         for (Entry<String, FileGeneratorFactory> entry : factories.entrySet()) {
             final String id = entry.getKey();
             FileGeneratorArg arg = fileGeneratorArgs.get(id);
@@ -262,20 +254,29 @@ class YangToSourcesProcessor {
             LOG.info("{} Code generator {} instantiated", LOG_PREFIX, id);
         }
 
+        // 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 generators;
     }
 
     @SuppressWarnings("checkstyle:illegalCatch")
     private Optional<ProcessorModuleReactor> createReactor(final List<File> yangFilesInProject,
-            final StatementParserMode parserMode, final Collection<ScannedDependency> dependencies,
-            final List<Entry<YangTextSchemaSource, IRSchemaSource>> parsed) throws MojoExecutionException {
+            final YangParserConfiguration parserConfig, final Collection<ScannedDependency> dependencies,
+            final List<Entry<YangTextSchemaSource, YangIRSchemaSource>> parsed) throws MojoExecutionException {
 
         try {
             final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
-            final YangParser parser = parserFactory.createParser(parserMode);
-            for (final Entry<YangTextSchemaSource, IRSchemaSource> entry : parsed) {
+            final YangParser parser = parserFactory.createParser(parserConfig);
+            for (final Entry<YangTextSchemaSource, YangIRSchemaSource> entry : parsed) {
                 final YangTextSchemaSource textSource = entry.getKey();
-                final IRSchemaSource astSource = entry.getValue();
+                final YangIRSchemaSource astSource = entry.getValue();
                 parser.addSource(astSource);
 
                 if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
@@ -319,10 +320,10 @@ class YangToSourcesProcessor {
      * Call generate on every generator from plugin configuration.
      */
     private Set<File> generateSources(final ContextHolder context, final Collection<GeneratorTaskFactory> generators,
-            final StatementParserMode parserMode) throws MojoFailureException {
+            final YangParserConfiguration parserConfig) throws MojoFailureException {
         final Builder<File> allFiles = ImmutableSet.builder();
         for (GeneratorTaskFactory factory : generators) {
-            if (!parserMode.equals(factory.parserMode())) {
+            if (!parserConfig.equals(factory.parserConfig())) {
                 continue;
             }
 
@@ -338,9 +339,16 @@ class YangToSourcesProcessor {
             }
 
             LOG.debug("{} Sources generated by {}: {}", LOG_PREFIX, factory.generatorName(), files);
-            LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, factory.generatorName(),
-                files == null ? 0 : files.size(), sw);
-            allFiles.addAll(files);
+
+            final int fileCount;
+            if (files != null) {
+                fileCount = files.size();
+                allFiles.addAll(files);
+            } else {
+                fileCount = 0;
+            }
+
+            LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, factory.generatorName(), fileCount, sw);
         }
 
         return allFiles.build();