Skip to content

Commit

Permalink
Fixes #2010
Browse files Browse the repository at this point in the history
  • Loading branch information
aedelmann committed Oct 10, 2019
1 parent 70291c0 commit 91eab5b
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 130 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.vorto.codegen.bosch.templates.BoschGeneratorConfigUI;
import org.eclipse.vorto.codegen.bosch.templates.ProvisionDeviceScriptTemplate;
import org.eclipse.vorto.codegen.bosch.templates.ProvisioningAPIRequestTemplate;
import org.eclipse.vorto.codegen.hono.EclipseHonoGenerator;
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel;
import org.eclipse.vorto.plugin.generator.GeneratorException;
Expand All @@ -24,16 +25,21 @@
import org.eclipse.vorto.plugin.generator.utils.GenerationResultBuilder;
import org.eclipse.vorto.plugin.generator.utils.GenerationResultZip;
import org.eclipse.vorto.plugin.generator.utils.GeneratorTaskFromFileTemplate;
import org.eclipse.vorto.plugin.generator.utils.SingleGenerationResult;

public class BoschIoTSuiteGenerator implements ICodeGenerator {

private static final String KEY_PROVISION = "provision";

private static final String KEY_BODY_TEMPLATE = "requestBodyOnly";

private static final String KEY_LANGUAGE = "language";

private static final String KEY = "boschiotsuite";

private static final BoschGeneratorConfigUI CONFIG_TEMPLATE = new BoschGeneratorConfigUI();

private static final ProvisioningAPIRequestTemplate REQUEST_TEMPLATE = new ProvisioningAPIRequestTemplate();

public IGenerationResult generate(InformationModel infomodel, InvocationContext invocationContext) throws GeneratorException {

Expand All @@ -42,19 +48,23 @@ public IGenerationResult generate(InformationModel infomodel, InvocationContext
GenerationResultBuilder result = GenerationResultBuilder.from(output);

String platform = invocationContext.getConfigurationProperties().getOrDefault(KEY_LANGUAGE, "");

if (platform.equalsIgnoreCase("arduino")) {
result.append(generateArduino(infomodel, invocationContext));
} else if (platform.equalsIgnoreCase("python")) {
result.append(generatePython(infomodel, invocationContext));
} else if (platform.equalsIgnoreCase("java")) {
result.append(generateJava(infomodel, invocationContext));
}

String provisionScript = invocationContext.getConfigurationProperties().getOrDefault(KEY_PROVISION, "false");
if ("true".equalsIgnoreCase(provisionScript)) {
new GeneratorTaskFromFileTemplate<>(new ProvisionDeviceScriptTemplate()).generate(infomodel, invocationContext, output);
} else if (invocationContext.getConfigurationProperties().getOrDefault(KEY_PROVISION, "false").equals("true")) {
SingleGenerationResult singleOutput = new SingleGenerationResult("application/json");
if (invocationContext.getConfigurationProperties().getOrDefault(KEY_BODY_TEMPLATE, "false").equals("true")) {
new GeneratorTaskFromFileTemplate<>(REQUEST_TEMPLATE).generate(infomodel, invocationContext, singleOutput);
} else {
new GeneratorTaskFromFileTemplate<>(new ProvisionDeviceScriptTemplate(REQUEST_TEMPLATE)).generate(infomodel, invocationContext, singleOutput);
}
return singleOutput;
}

return output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
*/
package org.eclipse.vorto.codegen.bosch.templates

import org.eclipse.vorto.core.api.model.datatype.Entity
import org.eclipse.vorto.core.api.model.datatype.Enum
import org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType
import org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType
import org.eclipse.vorto.core.api.model.datatype.PrimitiveType
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel
import org.eclipse.vorto.plugin.generator.InvocationContext
import org.eclipse.vorto.plugin.generator.utils.IFileTemplate
import org.eclipse.xtext.util.Strings
import org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType

/**
* Template that creates a Postman Script (collection) containing the requests
* to provision the Vorto modelled device in the Bosch IoT Suite
*/
class ProvisionDeviceScriptTemplate implements IFileTemplate<InformationModel> {


ProvisioningAPIRequestTemplate requestTemplate = null;

new (ProvisioningAPIRequestTemplate template) {
requestTemplate = template;
}

override getFileName(InformationModel context) {
return "Provisioning_" + context.name + ".postman.json";
}
Expand Down Expand Up @@ -80,7 +80,7 @@ class ProvisionDeviceScriptTemplate implements IFileTemplate<InformationModel> {
],
"body": {
"mode": "raw",
"raw": "«Strings.convertToJavaString(getJson(element).toString,true)»"
"raw": "«Strings.convertToJavaString(requestTemplate.getContent(element,context).toString,true)»"
},
"description": "Provisions the «element.name» in the Bosch IoT Suite"
},
Expand All @@ -90,114 +90,4 @@ class ProvisionDeviceScriptTemplate implements IFileTemplate<InformationModel> {
}
'''
}

def getJson(InformationModel model) {
'''
{
"id": "{{device-id}}",
"hub": {
"device": {
"enabled": true
},
"credentials": {
"type": "hashed-password",
"secrets": [
{
"password": "{{device-password}}"
}
]
}
},
"things": {
"thing": {
"attributes": {
"thingName": "«model.displayname»",
"definition": "«model.namespace»:«model.name»:«model.version»"
},
"features": {
«FOR fbProperty : model.properties SEPARATOR ","»
"«fbProperty.name»" : {
"definition": [
"«fbProperty.type.namespace»:«fbProperty.type.name»:«fbProperty.type.version»"
],
"properties": {
«IF fbProperty.type.functionblock.status !== null && !fbProperty.type.functionblock.status.properties.isEmpty»
"status": {
«FOR statusProperty : fbProperty.type.functionblock.status.properties SEPARATOR ","»
"«statusProperty.name»" : «IF statusProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(statusProperty.type as PrimitivePropertyType)»«ELSEIF statusProperty.type instanceof ObjectPropertyType»«getJsonObjectType(statusProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(statusProperty.type as DictionaryPropertyType)»«ENDIF»
«ENDFOR»
}«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty»,«ENDIF»
«ENDIF»
«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty»
"configuration": {
«FOR configProperty : fbProperty.type.functionblock.configuration.properties SEPARATOR ","»
"«configProperty.name»" : «IF configProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(configProperty.type as PrimitivePropertyType)»«ELSEIF configProperty.type instanceof ObjectPropertyType»«getJsonObjectType(configProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(configProperty.type as DictionaryPropertyType)»«ENDIF»
«ENDFOR»
}
«ENDIF»
}
}
«ENDFOR»
}
}
}
}
'''
}

def getJsonDictionaryType(DictionaryPropertyType propertyType) {
'''
{
"key" : "value"
}
'''
}

def String getJsonObjectType(ObjectPropertyType propertyType) {
if (propertyType.type instanceof Enum) {
var literals = (propertyType.type as Enum).enums;
if (literals.empty) {
return "\"\""
} else {
return "\"" + literals.get(0).name + "\"";
}
} else {
return getEntityJson(propertyType.type as Entity).toString();
}
}

def getEntityJson(Entity entity) {
'''
{
«FOR property : entity.properties SEPARATOR ","»
"«property.name»" : «IF property.type instanceof PrimitivePropertyType»«getJsonPrimitive(property.type as PrimitivePropertyType)»«ELSE»«getJsonObjectType(property.type as ObjectPropertyType)»«ENDIF»
«ENDFOR»
}
'''
}

def getJsonPrimitive(PrimitivePropertyType propertyType) {
if (propertyType.type === PrimitiveType.BASE64_BINARY) {
return "\"\""
} else if (propertyType.type === PrimitiveType.BOOLEAN) {
return false
} else if (propertyType.type === PrimitiveType.BYTE) {
return "\"\""
} else if (propertyType.type === PrimitiveType.DATETIME) {
return "\"2019-04-01T18:25:43-00:00\""
} else if (propertyType.type === PrimitiveType.DOUBLE) {
return 0.0
} else if (propertyType.type === PrimitiveType.FLOAT) {
return 0.0
} else if (propertyType.type === PrimitiveType.INT) {
return 0
} else if (propertyType.type === PrimitiveType.LONG) {
return 0
} else if (propertyType.type === PrimitiveType.SHORT) {
return 0
} else {
return "\"\""
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package org.eclipse.vorto.codegen.bosch.templates

import org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType
import org.eclipse.vorto.core.api.model.datatype.Entity
import org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType
import org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType
import org.eclipse.vorto.core.api.model.datatype.PrimitiveType
import org.eclipse.vorto.core.api.model.informationmodel.InformationModel
import org.eclipse.vorto.plugin.generator.InvocationContext
import org.eclipse.vorto.plugin.generator.utils.IFileTemplate

class ProvisioningAPIRequestTemplate implements IFileTemplate<InformationModel> {

override getFileName(InformationModel context) {
return "provisioningRequest.json";
}

override getPath(InformationModel context) {
return null;
}

override getContent(InformationModel model, InvocationContext context) {
'''
{
"id": "{{device-id}}",
"hub": {
"device": {
"enabled": true
},
"credentials": {
"type": "hashed-password",
"secrets": [
{
"password": "{{device-password}}"
}
]
}
},
"things": {
"thing": {
"attributes": {
"thingName": "«model.displayname»",
"definition": "«model.namespace»:«model.name»:«model.version»"
},
"features": {
«FOR fbProperty : model.properties SEPARATOR ","»
"«fbProperty.name»" : {
"definition": [
"«fbProperty.type.namespace»:«fbProperty.type.name»:«fbProperty.type.version»"
],
"properties": {
«IF fbProperty.type.functionblock.status !== null && !fbProperty.type.functionblock.status.properties.isEmpty»
"status": {
«FOR statusProperty : fbProperty.type.functionblock.status.properties SEPARATOR ","»
"«statusProperty.name»" : «IF statusProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(statusProperty.type as PrimitivePropertyType)»«ELSEIF statusProperty.type instanceof ObjectPropertyType»«getJsonObjectType(statusProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(statusProperty.type as DictionaryPropertyType)»«ENDIF»
«ENDFOR»
}«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty»,«ENDIF»
«ENDIF»
«IF fbProperty.type.functionblock.configuration !== null && !fbProperty.type.functionblock.configuration.properties.isEmpty»
"configuration": {
«FOR configProperty : fbProperty.type.functionblock.configuration.properties SEPARATOR ","»
"«configProperty.name»" : «IF configProperty.type instanceof PrimitivePropertyType»«getJsonPrimitive(configProperty.type as PrimitivePropertyType)»«ELSEIF configProperty.type instanceof ObjectPropertyType»«getJsonObjectType(configProperty.type as ObjectPropertyType)»«ELSE»«getJsonDictionaryType(configProperty.type as DictionaryPropertyType)»«ENDIF»
«ENDFOR»
}
«ENDIF»
}
}
«ENDFOR»
}
}
}
}
'''
}

def getJsonDictionaryType(DictionaryPropertyType propertyType) {
'''
{
"key" : "value"
}
'''
}

def String getJsonObjectType(ObjectPropertyType propertyType) {
if (propertyType.type instanceof org.eclipse.vorto.core.api.model.datatype.Enum) {
var literals = (propertyType.type as org.eclipse.vorto.core.api.model.datatype.Enum).enums;
if (literals.empty) {
return "\"\""
} else {
return "\"" + literals.get(0).name + "\"";
}
} else {
return getEntityJson(propertyType.type as Entity).toString();
}
}

def getEntityJson(Entity entity) {
'''
{
«FOR property : entity.properties SEPARATOR ","»
"«property.name»" : «IF property.type instanceof PrimitivePropertyType»«getJsonPrimitive(property.type as PrimitivePropertyType)»«ELSE»«getJsonObjectType(property.type as ObjectPropertyType)»«ENDIF»
«ENDFOR»
}
'''
}

def getJsonPrimitive(PrimitivePropertyType propertyType) {
if (propertyType.type === PrimitiveType.BASE64_BINARY) {
return "\"\""
} else if (propertyType.type === PrimitiveType.BOOLEAN) {
return false
} else if (propertyType.type === PrimitiveType.BYTE) {
return "\"\""
} else if (propertyType.type === PrimitiveType.DATETIME) {
return "\"2019-04-01T18:25:43-00:00\""
} else if (propertyType.type === PrimitiveType.DOUBLE) {
return 0.0
} else if (propertyType.type === PrimitiveType.FLOAT) {
return 0.0
} else if (propertyType.type === PrimitiveType.INT) {
return 0
} else if (propertyType.type === PrimitiveType.LONG) {
return 0
} else if (propertyType.type === PrimitiveType.SHORT) {
return 0
} else {
return "\"\""
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ProvisionDeviceScriptTemplateTest {

@Test
def void testCreateScriptWithSingleFb() {
var template = new ProvisionDeviceScriptTemplate();
var template = new ProvisionDeviceScriptTemplate(new ProvisioningAPIRequestTemplate);

var fbm = BuilderUtils.newFunctionblock(
new ModelId(ModelType.Functionblock, "Temperature", "org.eclipse.vorto", "1.0.0")).
Expand Down Expand Up @@ -96,7 +96,7 @@ class ProvisionDeviceScriptTemplateTest {

@Test
def void testCreateScriptWithMultipleFbs() {
var template = new ProvisionDeviceScriptTemplate();
var template = new ProvisionDeviceScriptTemplate(new ProvisioningAPIRequestTemplate);

var fbm = BuilderUtils.newFunctionblock(
new ModelId(ModelType.Functionblock, "Temperature", "org.eclipse.vorto", "1.0.0")).
Expand Down Expand Up @@ -172,7 +172,7 @@ class ProvisionDeviceScriptTemplateTest {

@Test
def void testCreateScriptWithFbContainingNestedDatatypes() {
var template = new ProvisionDeviceScriptTemplate();
var template = new ProvisionDeviceScriptTemplate(new ProvisioningAPIRequestTemplate);

var _enum = BuilderUtils.newEnum(new ModelId(ModelType.Datatype, "Units", "org.eclipse.vorto.types", "1.0.0"))
_enum.withLiterals("F", "C")
Expand Down Expand Up @@ -251,7 +251,7 @@ class ProvisionDeviceScriptTemplateTest {

@Test
def void testCreateScriptWithFbContainingEnum() {
var template = new ProvisionDeviceScriptTemplate();
var template = new ProvisionDeviceScriptTemplate(new ProvisioningAPIRequestTemplate);

var _enum = BuilderUtils.newEnum(new ModelId(ModelType.Datatype, "Units", "org.eclipse.vorto.types", "1.0.0"))
_enum.withLiterals("F", "C")
Expand Down
Loading

0 comments on commit 91eab5b

Please sign in to comment.