X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmaven-sal-api-gen-plugin%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fmaven%2Fapi%2Fgen%2Fplugin%2FCodeGeneratorImpl.java;h=46ec389b53ea763ab7e9c1df536946df7694f1fc;hb=dee64a5322619a72a8c0515096b2dc3a239261de;hp=5ed142fba3d7c96dc736f3c0537e0df9bbf45043;hpb=c241dcfa5322ac10810a1068ccd2eb57f6f2dbb2;p=mdsal.git diff --git a/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/mdsal/binding/maven/api/gen/plugin/CodeGeneratorImpl.java b/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/mdsal/binding/maven/api/gen/plugin/CodeGeneratorImpl.java index 5ed142fba3..46ec389b53 100644 --- a/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/mdsal/binding/maven/api/gen/plugin/CodeGeneratorImpl.java +++ b/binding/maven-sal-api-gen-plugin/src/main/java/org/opendaylight/mdsal/binding/maven/api/gen/plugin/CodeGeneratorImpl.java @@ -7,13 +7,16 @@ */ package org.opendaylight.mdsal.binding.maven.api.gen.plugin; +import static java.util.Objects.requireNonNull; + import com.google.common.base.Joiner; -import com.google.common.base.Preconditions; +import com.google.common.base.Stopwatch; import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet.Builder; +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 com.google.common.io.Files; import java.io.BufferedWriter; import java.io.File; import java.io.IOException; @@ -21,7 +24,8 @@ import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.List; import java.util.Map; @@ -29,6 +33,7 @@ import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.function.Supplier; +import java.util.stream.Collectors; import org.apache.maven.project.MavenProject; import org.opendaylight.mdsal.binding.generator.impl.BindingGeneratorImpl; import org.opendaylight.mdsal.binding.java.api.generator.GeneratorJavaFile; @@ -66,7 +71,11 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext outputBaseDir = outputDir == null ? getDefaultOutputBaseDir() : outputDir; + // Step one: determine binding types which we are generating + final Stopwatch sw = Stopwatch.createStarted(); final List types = new BindingGeneratorImpl().generateTypes(context, yangModules); + LOG.info("Found {} Binding types in {}", types.size(), sw); + final GeneratorJavaFile generator = new GeneratorJavaFile(types); File persistentSourcesDir = null; @@ -87,7 +96,9 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext final Table> generatedFiles = generator.generateFileContent( ignoreDuplicateFiles); - final List result = new ArrayList<>(generatedFiles.size()); + + // Step two: create generation tasks for each target file and group them by parent directory + final ListMultimap dirs = MultimapBuilder.hashKeys().arrayListValues().build(); for (Cell> cell : generatedFiles.cellSet()) { final File target; switch (cell.getRowKey()) { @@ -105,21 +116,29 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext throw new IllegalStateException("Unsupported file type in " + cell); } - Files.createParentDirs(target); - try (OutputStream stream = buildContext.newFileOutputStream(target)) { - try (Writer fw = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { - try (BufferedWriter bw = new BufferedWriter(fw)) { - bw.write(cell.getValue().get()); - } - } catch (IOException e) { - LOG.error("Failed to write generate output into {}", target.getPath(), e); - throw e; - } + dirs.put(target.getParentFile().toPath(), new GenerationTask(buildContext, target, cell.getValue())); + } + LOG.info("Generating {} Binding source files into {} directories", dirs.size(), dirs.keySet().size()); + + // Step three: submit parent directory creation tasks (via parallelStream()) and wait for them to complete + sw.reset().start(); + dirs.keySet().parallelStream().forEach(path -> { + try { + Files.createDirectories(path); + } catch (IOException e) { + throw new IllegalStateException("Failed to create " + path, e); } + }); + LOG.debug("Parent directories created in {}", sw); - result.add(target); - } + // Step four: submit all code generation tasks (via parallelStream()) and wait for them to complete + sw.reset().start(); + final List result = dirs.values().parallelStream() + .map(GenerationTask::generateFile) + .collect(Collectors.toList()); + LOG.debug("{} Binding source type files generated in {}", result.size(), sw); + // Step five: generate auxiliary files result.addAll(generateModuleInfos(outputBaseDir, yangModules, context, moduleResourcePathResolver)); return result; } @@ -161,12 +180,12 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext File outputBaseDir; outputBaseDir = new File(DEFAULT_OUTPUT_BASE_DIR_PATH); setOutputBaseDirAsSourceFolder(outputBaseDir, mavenProject); - LOG.debug("Adding " + outputBaseDir.getPath() + " as compile source root"); + LOG.debug("Adding {} as compile source root", outputBaseDir.getPath()); return outputBaseDir; } private static void setOutputBaseDirAsSourceFolder(final File outputBaseDir, final MavenProject mavenProject) { - Preconditions.checkNotNull(mavenProject, "Maven project needs to be set in this phase"); + requireNonNull(mavenProject, "Maven project needs to be set in this phase"); mavenProject.addCompileSourceRoot(outputBaseDir.getPath()); } @@ -188,7 +207,7 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext @Override public void setBuildContext(final BuildContext buildContext) { - this.buildContext = Preconditions.checkNotNull(buildContext); + this.buildContext = requireNonNull(buildContext); } private Set generateYangModuleInfo(final File outputBaseDir, final Module module, final SchemaContext ctx, @@ -232,11 +251,38 @@ public final class CodeGeneratorImpl implements BasicCodeGenerator, BuildContext bw.write(source); } } catch (Exception e) { - LOG.error("Could not write file: {}",file,e); + LOG.error("Could not write file: {}", file, e); } } catch (Exception e) { - LOG.error("Could not create file: {}",file,e); + LOG.error("Could not create file: {}", file, e); } return file; } + + private static final class GenerationTask { + private final BuildContext buildContext; + private final Supplier contentSupplier; + private final File target; + + GenerationTask(final BuildContext buildContext, final File target, final Supplier contentSupplier) { + this.buildContext = requireNonNull(buildContext); + this.target = requireNonNull(target); + this.contentSupplier = requireNonNull(contentSupplier); + } + + File generateFile() { + try { + try (OutputStream stream = buildContext.newFileOutputStream(target)) { + try (Writer fw = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) { + try (BufferedWriter bw = new BufferedWriter(fw)) { + bw.write(contentSupplier.get()); + } + } + } + } catch (IOException e) { + throw new IllegalStateException("Failed to generate file " + target, e); + } + return target; + } + } }