Search in sources :

Example 6 with InformationModel

use of org.eclipse.vorto.core.api.model.informationmodel.InformationModel 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 7 with InformationModel

use of org.eclipse.vorto.core.api.model.informationmodel.InformationModel 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 8 with InformationModel

use of org.eclipse.vorto.core.api.model.informationmodel.InformationModel in project vorto by eclipse.

the class JsonGenerator method generate.

public GenerationResultZip generate(InformationModel infomodel, InvocationContext context, IVortoCodeGenProgressMonitor monitor) throws VortoCodeGeneratorException {
    GenerationResultZip output = new GenerationResultZip(infomodel, getServiceKey());
    GeneratorTaskFromFileTemplate<InformationModel> imTemplate = new GeneratorTaskFromFileTemplate<InformationModel>(new JsonIMTemplate());
    imTemplate.generate(infomodel, context, output);
    return output;
}
Also used : GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) GeneratorTaskFromFileTemplate(org.eclipse.vorto.codegen.api.GeneratorTaskFromFileTemplate) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) JsonIMTemplate(org.eclipse.vorto.codegen.json.templates.JsonIMTemplate)

Example 9 with InformationModel

use of org.eclipse.vorto.core.api.model.informationmodel.InformationModel in project vorto by eclipse.

the class CoAPGenerator method generate.

public IGenerationResult generate(InformationModel infomodel, InvocationContext mappingContext, IVortoCodeGenProgressMonitor monitor) throws VortoCodeGeneratorException {
    GenerationResultZip zipOutputter = new GenerationResultZip(infomodel, getServiceKey());
    ChainedCodeGeneratorTask<InformationModel> generator = new ChainedCodeGeneratorTask<InformationModel>();
    /*
		/ Generate the client part...
		*/
    String CLIENT_PROJ = infomodel.getName() + COAP_CLIENT_PROJECT_SUFFIX;
    initPaths(CLIENT_PROJ);
    generator.addTask(new CoAPClientInformationModelGeneratorTask(JAVA_FILE_EXTENSION, imTargetPath, IM_PACKAGE, JAVA_INTERFACE_PREFIX, JAVA_IMPL_SUFFIX, GETTER_PREFIX, SETTER_PREFIX, FB_INTERFACE_PACKAGE, FB_IMPL_PACKAGE));
    generator.addTask(new JavaInformationModelInterfaceGeneratorTask(JAVA_FILE_EXTENSION, imTargetPath, IM_PACKAGE, JAVA_INTERFACE_PREFIX, GETTER_PREFIX, SETTER_PREFIX, FB_INTERFACE_PACKAGE));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new ClientTemplate(coapClientPath, COAP_CLIENT_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new CoAPMethodTemplate(coapClientPath, COAP_CLIENT_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new ClientDemoAppTemplate(coapDemoPath, COAP_DEMO_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new PomFileTemplate("artifact", COAP_DEMO_PACKAGE + ".ClientDemoApp", CLIENT_PROJ)));
    generator.generate(infomodel, mappingContext, zipOutputter);
    for (FunctionblockProperty fbp : infomodel.getProperties()) {
        FunctionBlock fb = fbp.getType().getFunctionblock();
        for (Entity entity : Utils.getReferencedEntities(fb)) {
            generateForEntity(entity, zipOutputter);
        }
        for (Enum en : Utils.getReferencedEnums(fb)) {
            generateForEnum(en, zipOutputter);
        }
        for (Operation op : fb.getOperations()) {
            generateForOperation(op, zipOutputter);
        }
        if (fb.getStatus() != null) {
            for (Property property : fb.getStatus().getProperties()) {
                generateForProperty(property, zipOutputter);
            }
        }
        this.generateForClientFunctionBlock(fbp.getType(), zipOutputter);
    }
    /*
		/ Generate the server part...
		*/
    String SERVER_PROJ = infomodel.getName() + COAP_SERVER_PROJECT_SUFFIX;
    initPaths(SERVER_PROJ);
    generator.addTask(new CoAPServerGeneratorTask(COAP_SERVER_NAME, COAP_SERVER_PACKAGE, JAVA_FILE_EXTENSION, coapServerPath, JAVA_INTERFACE_PREFIX + COAP_REQUEST_HANDLER_NAME, COAP_REQUEST_HANDLER_PACKAGE));
    generator.addTask(new CoAPServerIMRequestHandlerGeneratorTask(COAP_REQUEST_HANDLER_NAME, JAVA_FILE_EXTENSION, coapRequestHandlerPath, COAP_REQUEST_HANDLER_PACKAGE, JAVA_INTERFACE_PREFIX, COAP_PRIM_TYPE_WRAPPER_SUFFIX, IM_PACKAGE, DT_PACKAGE, getOpParamSetPackage(), getPrimitiveParamWrapperPackage()));
    generator.addTask(new JavaInformationModelGeneratorTask(JAVA_FILE_EXTENSION, imTargetPath, IM_PACKAGE, JAVA_INTERFACE_PREFIX, JAVA_IMPL_SUFFIX, GETTER_PREFIX, SETTER_PREFIX, FB_INTERFACE_PACKAGE, FB_IMPL_PACKAGE));
    generator.addTask(new JavaInformationModelInterfaceGeneratorTask(JAVA_FILE_EXTENSION, imTargetPath, IM_PACKAGE, JAVA_INTERFACE_PREFIX, GETTER_PREFIX, SETTER_PREFIX, FB_INTERFACE_PACKAGE, FB_IMPL_PACKAGE));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new ICoAPRequestHandlerTemplate(coapRequestHandlerPath, COAP_REQUEST_HANDLER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new JsonTransformerTemplate(coapRequestHandlerPath, COAP_REQUEST_HANDLER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new URIAnalyzerTemplate(coapRequestHandlerPath, COAP_REQUEST_HANDLER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new ResourceTemplate(coapServerPath, COAP_SERVER_PACKAGE, COAP_REQUEST_HANDLER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new LinkTemplate(coapServerPath, COAP_SERVER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new CoAPServerDemoAppTemplate(coapDemoPath, COAP_DEMO_PACKAGE, COAP_REQUEST_HANDLER_PACKAGE, COAP_SERVER_PACKAGE)));
    generator.addTask(new GeneratorTaskFromFileTemplate<InformationModel>(new PomFileTemplate("artifact", COAP_DEMO_PACKAGE + ".ServerDemoApp", SERVER_PROJ)));
    generator.generate(infomodel, mappingContext, zipOutputter);
    for (FunctionblockProperty fbp : infomodel.getProperties()) {
        this.generateForFunctionBlock(fbp.getType(), zipOutputter);
        FunctionBlock fb = fbp.getType().getFunctionblock();
        for (Entity entity : Utils.getReferencedEntities(fb)) {
            generateForEntity(entity, zipOutputter);
        }
        for (Enum en : Utils.getReferencedEnums(fb)) {
            generateForEnum(en, zipOutputter);
        }
        for (Operation op : fb.getOperations()) {
            generateForOperation(op, zipOutputter);
        }
    }
    return zipOutputter;
}
Also used : Enum(org.eclipse.vorto.core.api.model.datatype.Enum) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) ICoAPRequestHandlerTemplate(org.eclipse.vorto.codegen.coap.server.templates.ICoAPRequestHandlerTemplate) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) ClientTemplate(org.eclipse.vorto.codegen.coap.client.templates.ClientTemplate) ClientDemoAppTemplate(org.eclipse.vorto.codegen.coap.client.templates.ClientDemoAppTemplate) Operation(org.eclipse.vorto.core.api.model.functionblock.Operation) JavaInformationModelInterfaceGeneratorTask(org.eclipse.vorto.codegen.coap.common.tasks.JavaInformationModelInterfaceGeneratorTask) JavaInformationModelGeneratorTask(org.eclipse.vorto.codegen.coap.common.tasks.JavaInformationModelGeneratorTask) PomFileTemplate(org.eclipse.vorto.codegen.coap.common.templates.PomFileTemplate) CoAPServerDemoAppTemplate(org.eclipse.vorto.codegen.coap.server.templates.CoAPServerDemoAppTemplate) FunctionblockProperty(org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty) Property(org.eclipse.vorto.core.api.model.datatype.Property) FunctionblockProperty(org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty) FunctionBlock(org.eclipse.vorto.core.api.model.functionblock.FunctionBlock) CoAPClientInformationModelGeneratorTask(org.eclipse.vorto.codegen.coap.client.tasks.CoAPClientInformationModelGeneratorTask) CoAPServerIMRequestHandlerGeneratorTask(org.eclipse.vorto.codegen.coap.server.tasks.CoAPServerIMRequestHandlerGeneratorTask) GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) ChainedCodeGeneratorTask(org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask) CoAPMethodTemplate(org.eclipse.vorto.codegen.coap.client.templates.CoAPMethodTemplate) URIAnalyzerTemplate(org.eclipse.vorto.codegen.coap.server.templates.URIAnalyzerTemplate) ResourceTemplate(org.eclipse.vorto.codegen.coap.server.templates.ResourceTemplate) CoAPServerGeneratorTask(org.eclipse.vorto.codegen.coap.server.tasks.CoAPServerGeneratorTask) JsonTransformerTemplate(org.eclipse.vorto.codegen.coap.server.templates.JsonTransformerTemplate) LinkTemplate(org.eclipse.vorto.codegen.coap.server.templates.LinkTemplate)

Example 10 with InformationModel

use of org.eclipse.vorto.core.api.model.informationmodel.InformationModel in project vorto by eclipse.

the class DeviceImpl method setInfomodel.

/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
public void setInfomodel(InformationModel newInfomodel) {
    InformationModel oldInfomodel = infomodel;
    infomodel = newInfomodel;
    if (eNotificationRequired())
        eNotify(new ENotificationImpl(this, Notification.SET, ModelPackage.DEVICE__INFOMODEL, oldInfomodel, infomodel));
}
Also used : ENotificationImpl(org.eclipse.emf.ecore.impl.ENotificationImpl) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel)

Aggregations

InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)24 GenerationResultZip (org.eclipse.vorto.codegen.api.GenerationResultZip)13 ChainedCodeGeneratorTask (org.eclipse.vorto.codegen.api.ChainedCodeGeneratorTask)10 FunctionblockProperty (org.eclipse.vorto.core.api.model.informationmodel.FunctionblockProperty)8 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)7 IGenerationResult (org.eclipse.vorto.codegen.api.IGenerationResult)6 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)6 Model (org.eclipse.vorto.core.api.model.model.Model)6 ZipInputStream (java.util.zip.ZipInputStream)5 IModelWorkspace (org.eclipse.vorto.server.commons.reader.IModelWorkspace)5 IVortoCodeGenerator (org.eclipse.vorto.codegen.api.IVortoCodeGenerator)4 Entity (org.eclipse.vorto.core.api.model.datatype.Entity)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 HashMap (java.util.HashMap)3 Collectors (java.util.stream.Collectors)3 Generated (org.eclipse.vorto.codegen.api.Generated)3 InvocationContext (org.eclipse.vorto.codegen.api.InvocationContext)3 IOException (java.io.IOException)2 List (java.util.List)2 GeneratorTaskFromFileTemplate (org.eclipse.vorto.codegen.api.GeneratorTaskFromFileTemplate)2