use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFile in project mdsal by opendaylight.
the class JavaFileGenerator method generateFiles.
@Override
public Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> generateFiles(final EffectiveModelContext context, final Set<Module> localModules, final ModuleResourceResolver moduleResourcePathResolver) throws FileGeneratorException {
final Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> result = generateFiles(bindingGenerator.generateTypes(context, localModules), ignoreDuplicateFiles);
// YangModuleInfo files
final Builder<String> bindingProviders = ImmutableSet.builder();
for (Module module : localModules) {
final YangModuleInfoTemplate template = new YangModuleInfoTemplate(module, context, mod -> moduleResourcePathResolver.findModuleResourcePath(mod, YangTextSchemaSource.class));
final String path = DOT_MATCHER.replaceFrom(template.getPackageName(), '/') + "/";
result.put(GeneratedFileType.SOURCE, GeneratedFilePath.ofPath(path + MODULE_INFO), new SupplierGeneratedFile(GeneratedFileLifecycle.TRANSIENT, template::generate));
result.put(GeneratedFileType.SOURCE, GeneratedFilePath.ofPath(path + MODEL_BINDING_PROVIDER), new SupplierGeneratedFile(GeneratedFileLifecycle.TRANSIENT, template::generateModelProvider));
bindingProviders.add(template.getModelBindingProviderName());
}
// META-INF/services entries, sorted to make the build predictable
final List<String> sorted = new ArrayList<>(bindingProviders.build());
sorted.sort(String::compareTo);
result.put(GeneratedFileType.RESOURCE, MODEL_BINDING_PROVIDER_SERVICE, GeneratedFile.of(GeneratedFileLifecycle.TRANSIENT, String.join("\n", sorted)));
return ImmutableTable.copyOf(result);
}
use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFile in project mdsal by opendaylight.
the class JavaFileGenerator method generateFiles.
@VisibleForTesting
static Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> generateFiles(final List<GeneratedType> types, final boolean ignoreDuplicateFiles) {
final Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> result = HashBasedTable.create();
for (Type type : types) {
for (CodeGenerator generator : GENERATORS) {
if (!generator.isAcceptable(type)) {
continue;
}
final GeneratedFileLifecycle kind = type instanceof GeneratedTransferObject && ((GeneratedTransferObject) type).isUnionTypeBuilder() ? GeneratedFileLifecycle.PERSISTENT : GeneratedFileLifecycle.TRANSIENT;
final GeneratedFilePath file = GeneratedFilePath.ofFilePath(type.getPackageName().replace('.', File.separatorChar) + File.separator + generator.getUnitName(type) + ".java");
if (result.contains(GeneratedFileType.SOURCE, file)) {
if (ignoreDuplicateFiles) {
LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be " + "generated.", type.getFullyQualifiedName());
continue;
}
throw new IllegalStateException("Duplicate " + kind + " file '" + file.getPath() + "' for " + type.getFullyQualifiedName());
}
result.put(GeneratedFileType.SOURCE, file, new CodeGeneratorGeneratedFile(kind, generator, type));
}
}
return result;
}
use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFile in project yangtools by opendaylight.
the class FileGeneratorTask method execute.
@Override
Collection<File> execute(final FileGeneratorTaskFactory factory, final ContextHolder modelContext, final BuildContext buildContext) throws FileGeneratorException, IOException {
// 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(modelContext.getContext(), modelContext.getYangModules(), modelContext);
LOG.info("{}: Defined {} files in {}", suffix, 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 GeneratedFile file = cell.getValue();
final String relativePath = cell.getColumnKey().getPath();
final File target;
switch(file.getLifecycle()) {
case PERSISTENT:
target = new File(persistentPath(cell.getRowKey()), relativePath);
if (target.exists()) {
LOG.debug("Skipping existing persistent {}", target);
continue;
}
break;
case TRANSIENT:
target = new File(transientPath(cell.getRowKey()), relativePath);
break;
default:
throw new IllegalStateException("Unsupported file type in " + file);
}
dirs.put(target.getParentFile(), new WriteTask(buildContext, target, cell.getValue()));
}
LOG.info("Sorted {} files into {} directories in {}", dirs.size(), dirs.keySet().size(), sw);
// 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.toPath());
} catch (IOException e) {
throw new IllegalStateException("Failed to create " + path, e);
}
});
LOG.debug("Parent directories created in {}", sw);
// Step four: submit all code generation tasks (via parallelStream()) and wait for them to complete
sw.reset().start();
final List<File> result = dirs.values().parallelStream().map(WriteTask::generateFile).collect(Collectors.toList());
LOG.debug("Generated {} files in {}", result.size(), sw);
return result;
}
use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFile in project netconf by opendaylight.
the class WadlGenTest method testListGeneration.
@Test
public void testListGeneration() {
final WadlGenerator generator = new WadlGenerator();
final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/wadl-gen");
Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> generatedWadlFiles = generator.generateFiles(context, Set.copyOf(context.getModules()), (module, representation) -> Optional.empty());
assertEquals(3, generatedWadlFiles.size());
// TODO: more asserts
}
use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFile in project mdsal by opendaylight.
the class YangModuleInfoCompilationTest method generateTestSources.
private static void generateTestSources(final String resourceDirPath, final File sourcesOutputDir) throws Exception {
final List<File> sourceFiles = getSourceFiles(resourceDirPath);
final EffectiveModelContext context = YangParserTestUtils.parseYangFiles(sourceFiles);
final Table<GeneratedFileType, GeneratedFilePath, GeneratedFile> codegen = new JavaFileGenerator(Map.of()).generateFiles(context, Set.copyOf(context.getModules()), (module, representation) -> Optional.of(resourceDirPath + File.separator + module.getName() + YangConstants.RFC6020_YANG_FILE_EXTENSION));
assertEquals(15, codegen.size());
assertEquals(14, codegen.row(GeneratedFileType.SOURCE).size());
assertEquals(1, codegen.row(GeneratedFileType.RESOURCE).size());
for (Entry<GeneratedFilePath, GeneratedFile> entry : codegen.row(GeneratedFileType.SOURCE).entrySet()) {
final Path path = new File(sourcesOutputDir, entry.getKey().getPath().replace(GeneratedFilePath.SEPARATOR, File.separatorChar)).toPath();
Files.createDirectories(path.getParent());
try (OutputStream out = Files.newOutputStream(path)) {
entry.getValue().writeBody(out);
}
}
}
Aggregations