Search in sources :

Example 1 with GeneratedFileType

use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType 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);
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) YangTextSchemaSource(org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) ArrayList(java.util.ArrayList) Module(org.opendaylight.yangtools.yang.model.api.Module) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)

Example 2 with GeneratedFileType

use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType 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;
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) Type(org.opendaylight.mdsal.binding.model.api.Type) GeneratedType(org.opendaylight.mdsal.binding.model.api.GeneratedType) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) GeneratedTransferObject(org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject) GeneratedFileLifecycle(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileLifecycle) CodeGenerator(org.opendaylight.mdsal.binding.model.api.CodeGenerator) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with GeneratedFileType

use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType 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;
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) FileGenerator(org.opendaylight.yangtools.plugin.generator.api.FileGenerator) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) File(java.io.File) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)

Example 4 with GeneratedFileType

use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType 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
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext) Test(org.junit.Test)

Example 5 with GeneratedFileType

use of org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType 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);
        }
    }
}
Also used : GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) Path(java.nio.file.Path) GeneratedFilePath(org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath) GeneratedFileType(org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType) OutputStream(java.io.OutputStream) File(java.io.File) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) GeneratedFile(org.opendaylight.yangtools.plugin.generator.api.GeneratedFile) EffectiveModelContext(org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)

Aggregations

GeneratedFile (org.opendaylight.yangtools.plugin.generator.api.GeneratedFile)6 GeneratedFilePath (org.opendaylight.yangtools.plugin.generator.api.GeneratedFilePath)6 GeneratedFileType (org.opendaylight.yangtools.plugin.generator.api.GeneratedFileType)6 File (java.io.File)3 EffectiveModelContext (org.opendaylight.yangtools.yang.model.api.EffectiveModelContext)3 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 CodeGenerator (org.opendaylight.mdsal.binding.model.api.CodeGenerator)1 GeneratedTransferObject (org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject)1 GeneratedType (org.opendaylight.mdsal.binding.model.api.GeneratedType)1 Type (org.opendaylight.mdsal.binding.model.api.Type)1 FileGenerator (org.opendaylight.yangtools.plugin.generator.api.FileGenerator)1 GeneratedFileLifecycle (org.opendaylight.yangtools.plugin.generator.api.GeneratedFileLifecycle)1 Module (org.opendaylight.yangtools.yang.model.api.Module)1 YangTextSchemaSource (org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource)1