Search in sources :

Example 1 with IGenerationResult

use of org.eclipse.vorto.codegen.api.IGenerationResult in project vorto by eclipse.

the class GeneratorMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("Executing Generator of class '" + getGeneratorClass());
    try {
        final IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(loadInformationModels()))).read();
        List<MappingModel> mappingModels = workspace.get().stream().filter(p -> p instanceof MappingModel).map(MappingModel.class::cast).collect(Collectors.toList());
        for (Model model : workspace.get().stream().filter(p -> p instanceof InformationModel).collect(Collectors.toList())) {
            InformationModel infomodel = (InformationModel) model;
            IVortoCodeGenerator codeGenerator = (IVortoCodeGenerator) Class.forName(generatorClass).newInstance();
            IGenerationResult result = codeGenerator.generate(infomodel, new InvocationContext(mappingModels, null, new HashMap<String, String>()), null);
            if (result.getMediatype().equalsIgnoreCase("application/zip")) {
                final ZipContentExtractCodeGeneratorTask task = new ZipContentExtractCodeGeneratorTask(result.getContent());
                task.generate(null, InvocationContext.simpleInvocationContext(), new IGeneratedWriter() {

                    public void write(Generated generated) {
                        if (generated.getFileName() == null) {
                            File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
                            generatedDirectory.mkdirs();
                        } else {
                            if (generated.getFileName().equals("pom.xml")) {
                                return;
                            }
                            File generatedDirectory = new File(outputPath, stripPath(generated.getFolderPath()));
                            File generatedFile = new File(generatedDirectory, generated.getFileName());
                            try {
                                FileUtils.writeByteArrayToFile(generatedFile, generated.getContent(), false);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    private String stripPath(String folderPath) {
                        final String mavenSourcePath = "src/main/java/";
                        if (folderPath.indexOf(mavenSourcePath) > -1) {
                            return folderPath.substring(folderPath.indexOf(mavenSourcePath) + mavenSourcePath.length());
                        }
                        return folderPath;
                    }
                });
            } else {
                File generatedFile = new File(outputPath, result.getFileName());
                FileUtils.writeByteArrayToFile(generatedFile, result.getContent(), false);
            }
        }
    } catch (InstantiationException e) {
        throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
    } catch (IllegalAccessException e) {
        throw new MojoExecutionException("Error during resolving code generator", e);
    } catch (ClassNotFoundException e) {
        throw new MojoExecutionException("Could not instantiate vorto code generator from given generatorClass", e);
    } catch (Exception e) {
        throw new MojoExecutionException("Problem during code generator invocation", e);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Generated(org.eclipse.vorto.codegen.api.Generated) ZipContentExtractCodeGeneratorTask(org.eclipse.vorto.codegen.api.ZipContentExtractCodeGeneratorTask) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) HashMap(java.util.HashMap) Parameter(org.apache.maven.plugins.annotations.Parameter) Model(org.eclipse.vorto.core.api.model.model.Model) Mojo(org.apache.maven.plugins.annotations.Mojo) ByteArrayInputStream(java.io.ByteArrayInputStream) MavenProject(org.apache.maven.project.MavenProject) LifecyclePhase(org.apache.maven.plugins.annotations.LifecyclePhase) Path(java.nio.file.Path) ZipEntry(java.util.zip.ZipEntry) InvocationContext(org.eclipse.vorto.codegen.api.InvocationContext) IGeneratedWriter(org.eclipse.vorto.codegen.api.IGeneratedWriter) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) FileInputStream(java.io.FileInputStream) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) Collectors(java.util.stream.Collectors) File(java.io.File) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) MojoFailureException(org.apache.maven.plugin.MojoFailureException) Consumer(java.util.function.Consumer) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ModelType(org.eclipse.vorto.core.api.model.model.ModelType) Paths(java.nio.file.Paths) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator) AbstractMojo(org.apache.maven.plugin.AbstractMojo) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator) Generated(org.eclipse.vorto.codegen.api.Generated) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IOException(java.io.IOException) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) IGeneratedWriter(org.eclipse.vorto.codegen.api.IGeneratedWriter) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZipContentExtractCodeGeneratorTask(org.eclipse.vorto.codegen.api.ZipContentExtractCodeGeneratorTask) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) InvocationContext(org.eclipse.vorto.codegen.api.InvocationContext) File(java.io.File) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 2 with IGenerationResult

use of org.eclipse.vorto.codegen.api.IGenerationResult in project vorto by eclipse.

the class MQTTPlatformGenerator method generate.

@Override
public IGenerationResult generate(InformationModel context, InvocationContext invocationContext, IVortoCodeGenProgressMonitor monitor) throws VortoCodeGeneratorException {
    GenerationResultZip outputter = new GenerationResultZip(context, getServiceKey());
    for (FunctionblockProperty property : context.getProperties()) {
        ChainedCodeGeneratorTask<FunctionblockModel> generator = new ChainedCodeGeneratorTask<FunctionblockModel>();
        if (property.getType().getFunctionblock().getStatus() != null) {
            generator.addTask(new GeneratorTaskFromFileTemplate<>(new IClientHandlerTemplate()));
            generator.addTask(new GeneratorTaskFromFileTemplate<>(new MqttConfigurationTemplate()));
        }
        generator.addTask(new GeneratorTaskFromFileTemplate<>(new PomTemplate()));
        generator.generate(property.getType(), invocationContext, outputter);
    }
    IGenerationResult javaResult = invocationContext.lookupGenerator(JavabeanGenerator.KEY).generate(context, invocationContext, monitor);
    return GenerationResultBuilder.from(outputter).append(javaResult).build();
}
Also used : PomTemplate(org.eclipse.vorto.codegen.mqtt.templates.PomTemplate) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) ChainedCodeGeneratorTask(org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask) IClientHandlerTemplate(org.eclipse.vorto.codegen.mqtt.templates.IClientHandlerTemplate) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) MqttConfigurationTemplate(org.eclipse.vorto.codegen.mqtt.templates.MqttConfigurationTemplate) FunctionblockProperty(org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty)

Example 3 with IGenerationResult

use of org.eclipse.vorto.codegen.api.IGenerationResult in project vorto by eclipse.

the class CodeGenerationController method generate.

@RequestMapping(value = "/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public ResponseEntity<InputStreamResource> generate(@PathVariable String namespace, @PathVariable String name, @PathVariable String version, final HttpServletRequest request) {
    byte[] modelResources = downloadModelWithReferences(namespace, name, version);
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelResources))).read();
    Model model = workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get();
    InformationModel infomodel = null;
    if (model instanceof InformationModel) {
        infomodel = (InformationModel) model;
    } else if (model instanceof FunctionblockModel) {
        infomodel = Utils.wrapFunctionBlock((FunctionblockModel) model);
    }
    IGenerationResult result = null;
    try {
        Map<String, String> requestParams = new HashMap<>();
        request.getParameterMap().entrySet().stream().forEach(x -> requestParams.put(x.getKey(), x.getValue()[0]));
        result = vortoGenerator.generate(infomodel, createInvocationContext(infomodel, vortoGenerator.getServiceKey(), requestParams), null);
    } catch (Exception e) {
        GenerationResultZip output = new GenerationResultZip(infomodel, vortoGenerator.getServiceKey());
        Generated generated = new Generated("generation_error.log", "/generated", e.getMessage());
        output.write(generated);
        result = output;
    }
    return ResponseEntity.ok().contentLength(result.getContent().length).header("content-disposition", "attachment; filename = " + result.getFileName()).contentType(MediaType.parseMediaType(result.getMediatype())).body(new InputStreamResource(new ByteArrayInputStream(result.getContent())));
}
Also used : Generated(org.eclipse.vorto.codegen.api.Generated) HashMap(java.util.HashMap) GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) RestClientException(org.springframework.web.client.RestClientException) IOException(java.io.IOException) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) InputStreamResource(org.springframework.core.io.InputStreamResource) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with IGenerationResult

use of org.eclipse.vorto.codegen.api.IGenerationResult in project vorto by eclipse.

the class CodeGeneratorInvocationHandler method evaluate.

private boolean evaluate(String generatorServiceKey) {
    IModelElement selectedElement = ModelProjectFactory.getInstance().getModelElementFromSelection();
    if (selectedElement == null) {
        selectedElement = ModelProjectFactory.getInstance().getSelectedModel();
    }
    if (selectedElement == null) {
        MessageDisplayFactory.getMessageDisplay().displayWarning("Model was not properly selected. Please try again.");
        return false;
    }
    if (!hasNoErrors(selectedElement)) {
        MessageDisplayFactory.getMessageDisplay().displayError("Model has errors. Cannot generate.");
        return false;
    }
    InformationModel informationModel = getInformationModel(selectedElement.getModel());
    try {
        IVortoCodeGenerator generator = lookupService.lookupByKey(generatorServiceKey);
        IGenerationResult result = generator.generate(informationModel, createInvocationContext(selectedElement.getProject(), generator.getServiceKey()), VortoCodeGenProgressMonitorFactory.getCodeGenStatusReporter());
        CodeGenerationHelper.createEclipseProject(ModelIdFactory.newInstance(informationModel), generator.getServiceKey(), result);
        VortoProgressMonitor.getDefault().display();
    } catch (Exception e) {
        MessageDisplayFactory.getMessageDisplay().displayError(e);
    }
    return true;
}
Also used : IModelElement(org.eclipse.vorto.core.ui.model.IModelElement) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) ExecutionException(org.eclipse.core.commands.ExecutionException)

Example 5 with IGenerationResult

use of org.eclipse.vorto.codegen.api.IGenerationResult in project vorto by eclipse.

the class BoschIoTThingsGenerator method generate.

public IGenerationResult generate(InformationModel infomodel, InvocationContext invocationContext, IVortoCodeGenProgressMonitor monitor) throws VortoCodeGeneratorException {
    GenerationResultZip zipOutputter = new GenerationResultZip(infomodel, getServiceKey());
    ChainedCodeGeneratorTask<InformationModel> generator = new ChainedCodeGeneratorTask<InformationModel>();
    GenerationResultBuilder result = GenerationResultBuilder.from(zipOutputter);
    if (hasNoTarget(invocationContext)) {
        result.append(generateSchema(infomodel, monitor, invocationContext));
    } else {
        if (invocationContext.getConfigurationProperties().getOrDefault(SIMULATOR, FALSE).equalsIgnoreCase(TRUE)) {
            generator.addTask(new JavaClientTask());
        }
        if (invocationContext.getConfigurationProperties().getOrDefault(SCHEMAVALIDATOR, FALSE).equalsIgnoreCase(TRUE)) {
            result.append(generateSchema(infomodel, monitor, invocationContext));
        }
    }
    generator.generate(infomodel, invocationContext, zipOutputter);
    if (invocationContext.getConfigurationProperties().getOrDefault("kura", FALSE).equalsIgnoreCase(TRUE)) {
        Map<String, String> props = new HashMap<>();
        props.put("boschcloud", "true");
        props.put("bluetooth", "true");
        IGenerationResult kuraResult = invocationContext.lookupGenerator("kura").generate(infomodel, InvocationContext.simpleInvocationContext(props), monitor);
        result.append(kuraResult);
    }
    if (invocationContext.getConfigurationProperties().getOrDefault("alexa", FALSE).equalsIgnoreCase(TRUE)) {
        Map<String, String> props = new HashMap<>();
        props.put("cloud", "bosch");
        props.put("thingId", invocationContext.getConfigurationProperties().getOrDefault("thingId", ""));
        IGenerationResult awsResult = invocationContext.lookupGenerator("aws").generate(infomodel, InvocationContext.simpleInvocationContext(props), monitor);
        result.append(awsResult);
    }
    if (invocationContext.getConfigurationProperties().getOrDefault("webui", FALSE).equalsIgnoreCase(TRUE)) {
        Map<String, String> props = new HashMap<>();
        props.put("boschcloud", "true");
        props.put("swagger", "true");
        props.put("persistence", "true");
        IGenerationResult webuiResult = invocationContext.lookupGenerator("webui").generate(infomodel, InvocationContext.simpleInvocationContext(props), monitor);
        result.append(webuiResult);
    }
    return result.build();
}
Also used : JavaClientTask(org.eclipse.vorto.codegen.bosch.things.javaclient.JavaClientTask) GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) HashMap(java.util.HashMap) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) ChainedCodeGeneratorTask(org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) GenerationResultBuilder(org.eclipse.vorto.codegen.utils.GenerationResultBuilder)

Aggregations

IGenerationResult (org.eclipse.vorto.codegen.api.IGenerationResult)6 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)5 GenerationResultZip (org.eclipse.vorto.codegen.api.GenerationResultZip)4 HashMap (java.util.HashMap)3 ChainedCodeGeneratorTask (org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask)3 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 ZipInputStream (java.util.zip.ZipInputStream)2 Generated (org.eclipse.vorto.codegen.api.Generated)2 IVortoCodeGenerator (org.eclipse.vorto.codegen.api.IVortoCodeGenerator)2 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)2 Model (org.eclipse.vorto.core.api.model.model.Model)2 IModelWorkspace (org.eclipse.vorto.server.commons.reader.IModelWorkspace)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1