Search in sources :

Example 1 with InvocationContext

use of org.eclipse.vorto.plugin.generator.InvocationContext in project vorto by eclipse.

the class GeneratorExecutionHandler method handleRequest.

@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
    ObjectMapper mapper = ObjectMapperFactory.getInstance();
    ApiGatewayRequest request = ApiGatewayRequest.createFromJson(input);
    Optional<ICodeGenerator> generator = generators.stream().filter(gen -> gen.getMeta().getKey().equals(request.getPathParam(PLUGINKEY))).findAny();
    if (!generator.isPresent()) {
        objectMapper.writeValue(output, createHttpReponse(404));
    }
    ModelContent modelContent = mapper.readValue(request.getInput(), ModelContent.class);
    ModelContentToEcoreConverter converter = new ModelContentToEcoreConverter();
    Model converted = converter.convert(modelContent, Optional.empty());
    InvocationContext invocationContext = InvocationContext.simpleInvocationContext(request.getQueryParams());
    InformationModel infomodel = org.eclipse.vorto.plugin.utils.Utils.toInformationModel(converted);
    try {
        IGenerationResult generatorResult = generator.get().generate(infomodel, invocationContext);
        ApiGatewayResponse validResponse = createResponse(generatorResult);
        OutputStreamWriter writer = new OutputStreamWriter(output, "UTF-8");
        writer.write(objectMapper.writeValueAsString(validResponse));
        writer.close();
    } catch (GeneratorException e) {
        ApiGatewayResponse response = createHttpReponse(500);
        objectMapper.writeValue(output, response);
    }
}
Also used : RequestStreamHandler(com.amazonaws.services.lambda.runtime.RequestStreamHandler) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Context(com.amazonaws.services.lambda.runtime.Context) EclipseHonoGenerator(org.eclipse.vorto.codegen.hono.EclipseHonoGenerator) ApiGatewayRequest(org.eclipse.vorto.plugin.utils.ApiGatewayRequest) ApiGatewayResponse(org.eclipse.vorto.plugin.utils.ApiGatewayResponse) Model(org.eclipse.vorto.core.api.model.model.Model) EclipseDittoGenerator(org.eclipse.vorto.codegen.ditto.EclipseDittoGenerator) HashSet(java.util.HashSet) ObjectMapperFactory(org.eclipse.vorto.plugin.generator.adapter.ObjectMapperFactory) ICodeGenerator(org.eclipse.vorto.plugin.generator.ICodeGenerator) OutputStreamWriter(java.io.OutputStreamWriter) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) OutputStream(java.io.OutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BoschIoTSuiteGenerator(org.eclipse.vorto.codegen.bosch.BoschIoTSuiteGenerator) Set(java.util.Set) IOException(java.io.IOException) GeneratorException(org.eclipse.vorto.plugin.generator.GeneratorException) OpenAPIGenerator(org.eclipse.vorto.codegen.openapi.OpenAPIGenerator) ModelContent(org.eclipse.vorto.model.ModelContent) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) JSONSchemaGenerator(org.eclipse.vorto.codegen.jsonschema.JSONSchemaGenerator) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) Optional(java.util.Optional) InputStream(java.io.InputStream) ModelContent(org.eclipse.vorto.model.ModelContent) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) ICodeGenerator(org.eclipse.vorto.plugin.generator.ICodeGenerator) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Model(org.eclipse.vorto.core.api.model.model.Model) OutputStreamWriter(java.io.OutputStreamWriter) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) GeneratorException(org.eclipse.vorto.plugin.generator.GeneratorException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ApiGatewayResponse(org.eclipse.vorto.plugin.utils.ApiGatewayResponse) ApiGatewayRequest(org.eclipse.vorto.plugin.utils.ApiGatewayRequest)

Example 2 with InvocationContext

use of org.eclipse.vorto.plugin.generator.InvocationContext in project vorto by eclipse.

the class BoschIoTSuiteGeneratorTest method generateArduino.

/*
   * Check if cpp file is generated when language is passed as arduino to BoschIOTSuiteGenerator
   */
@Test
public void generateArduino() throws Exception {
    configProperties.put("language", "arduino");
    InvocationContext context = new InvocationContext(mappingModels, configProperties);
    IGenerationResult iGenerationResult = boschIOTSuiteGenerator.generate(modelProvider(), context);
    Generated generatedfile = zipFileReader(iGenerationResult, "StatusPropertiesFunctionBlock", ".cpp");
    assertEquals("StatusPropertiesFunctionBlock.cpp", generatedfile.getFileName());
}
Also used : Generated(org.eclipse.vorto.plugin.generator.utils.Generated) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) AbstractGeneratorTest(org.eclipse.vorto.plugin.AbstractGeneratorTest) Test(org.junit.Test)

Example 3 with InvocationContext

use of org.eclipse.vorto.plugin.generator.InvocationContext in project vorto by eclipse.

the class BoschIoTSuiteGeneratorTest method generateProvisionTrue.

/*
   * Check if provisioning script file is returned when config property is passed as provision to
   * BoschIOTSuiteGenerator
   */
@Test
public void generateProvisionTrue() throws Exception {
    configProperties.put("provision", "true");
    InvocationContext context = new InvocationContext(mappingModels, configProperties);
    SingleGenerationResult singleGenerationResult = (SingleGenerationResult) boschIOTSuiteGenerator.generate(modelProvider(), context);
    assertEquals("Provisioning_MySensor.postman.json", singleGenerationResult.getFileName());
}
Also used : InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) SingleGenerationResult(org.eclipse.vorto.plugin.generator.utils.SingleGenerationResult) AbstractGeneratorTest(org.eclipse.vorto.plugin.AbstractGeneratorTest) Test(org.junit.Test)

Example 4 with InvocationContext

use of org.eclipse.vorto.plugin.generator.InvocationContext in project vorto by eclipse.

the class BoschIoTSuiteGeneratorTest method generateJava.

/*
   * Check if java file is generated when language is passed as java to BoschIOTSuiteGenerator
   */
@Test
public void generateJava() throws Exception {
    configProperties.put("language", "python");
    InvocationContext context = new InvocationContext(mappingModels, configProperties);
    IGenerationResult iGenerationResult = boschIOTSuiteGenerator.generate(modelProvider(), context);
    Generated generatedfile = zipFileReader(iGenerationResult, "StatusPropertiesFunctionBlock", ".java");
    assertEquals("StatusPropertiesFunctionBlock.java", generatedfile.getFileName());
}
Also used : Generated(org.eclipse.vorto.plugin.generator.utils.Generated) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) IGenerationResult(org.eclipse.vorto.plugin.generator.IGenerationResult) AbstractGeneratorTest(org.eclipse.vorto.plugin.AbstractGeneratorTest) Test(org.junit.Test)

Example 5 with InvocationContext

use of org.eclipse.vorto.plugin.generator.InvocationContext in project vorto by eclipse.

the class HelloWorldGeneratorTest method getMappedElementEntity.

/*
	 * Test case for checking the attribute mapping and corresponding property value for an entity
	 * 
	 * 
	 */
@Test
public void getMappedElementEntity() throws Exception {
    MappingBuilder mapping = BuilderUtils.newMapping(new ModelId(ModelType.Mapping, "", "", ""), "helloworld");
    EntityMappingRule entityMappingRule = MappingFactory.eINSTANCE.createEntityMappingRule();
    EntityAttributeSource entityAttributeSource = MappingFactory.eINSTANCE.createEntityAttributeSource();
    entityAttributeSource.setModel(_entity);
    entityMappingRule.getSources().add(entityAttributeSource);
    StereoTypeTarget stereotypeTarget = MappingFactory.eINSTANCE.createStereoTypeTarget();
    stereotypeTarget.setName("OBJECT_ID");
    Attribute objectIDattribute = MappingFactory.eINSTANCE.createAttribute();
    objectIDattribute.setName("ID");
    objectIDattribute.setValue("entityAttributID");
    stereotypeTarget.getAttributes().add(objectIDattribute);
    entityMappingRule.setTarget(stereotypeTarget);
    mapping.addRule(entityMappingRule);
    InvocationContext ctx = new InvocationContext(createMappingList(mapping.build()), Collections.emptyMap());
    Property prop = functionBlock.getFunctionblock().getStatus().getProperties().get(0);
    assertNotNull(ctx.getMappedElement(prop, "OBJECT_ID"));
    assertEquals("entityAttributID", ctx.getMappedElement(prop, "OBJECT_ID").getAttributeValue("ID", "entityAttributID"));
}
Also used : EntityMappingRule(org.eclipse.vorto.core.api.model.mapping.EntityMappingRule) MappingBuilder(org.eclipse.vorto.core.api.model.BuilderUtils.MappingBuilder) Attribute(org.eclipse.vorto.core.api.model.mapping.Attribute) EntityAttributeSource(org.eclipse.vorto.core.api.model.mapping.EntityAttributeSource) InvocationContext(org.eclipse.vorto.plugin.generator.InvocationContext) StereoTypeTarget(org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget) Property(org.eclipse.vorto.core.api.model.datatype.Property) ModelId(org.eclipse.vorto.core.api.model.model.ModelId) Test(org.junit.Test)

Aggregations

InvocationContext (org.eclipse.vorto.plugin.generator.InvocationContext)7 Test (org.junit.Test)6 AbstractGeneratorTest (org.eclipse.vorto.plugin.AbstractGeneratorTest)4 IGenerationResult (org.eclipse.vorto.plugin.generator.IGenerationResult)4 Generated (org.eclipse.vorto.plugin.generator.utils.Generated)3 MappingBuilder (org.eclipse.vorto.core.api.model.BuilderUtils.MappingBuilder)2 Property (org.eclipse.vorto.core.api.model.datatype.Property)2 Attribute (org.eclipse.vorto.core.api.model.mapping.Attribute)2 StereoTypeTarget (org.eclipse.vorto.core.api.model.mapping.StereoTypeTarget)2 ModelId (org.eclipse.vorto.core.api.model.model.ModelId)2 Context (com.amazonaws.services.lambda.runtime.Context)1 RequestStreamHandler (com.amazonaws.services.lambda.runtime.RequestStreamHandler)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 Set (java.util.Set)1