use of org.eclipse.vorto.server.commons.reader.IModelWorkspace 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())));
}
use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.
the class ModelRepositoryController method getModelContentForTargetPlatform.
@ApiOperation(value = "Returns the model content including target platform specific attributes")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}/mapping/{targetplatformKey}", method = RequestMethod.GET)
public AbstractModel getModelContentForTargetPlatform(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version, @ApiParam(value = "The key of the targetplatform, e.g. lwm2m", required = true) @PathVariable final String targetplatformKey) {
List<ModelInfo> mappingResource = modelRepository.getMappingModelsForTargetPlatform(new ModelId(name, namespace, version), targetplatformKey);
if (!mappingResource.isEmpty()) {
byte[] mappingContentZip = createZipWithAllDependencies(mappingResource.get(0).getId(), ContentType.DSL);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingContentZip))).read();
MappingModel mappingModel = (MappingModel) workspace.get().stream().filter(p -> p instanceof MappingModel).findFirst().get();
byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.of(mappingModel));
} else {
return getModelContent(namespace, name, version);
}
}
use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.
the class ModelRepositoryController method getModelContent.
@ApiOperation(value = "Returns the model content")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{namespace}/{name}/{version:.+}", method = RequestMethod.GET)
public AbstractModel getModelContent(@ApiParam(value = "The namespace of vorto model, e.g. com.mycompany", required = true) @PathVariable final String namespace, @ApiParam(value = "The name of vorto model, e.g. NewInfomodel", required = true) @PathVariable final String name, @ApiParam(value = "The version of vorto model, e.g. 1.0.0", required = true) @PathVariable final String version) {
byte[] modelContent = createZipWithAllDependencies(new ModelId(name, namespace, version), ContentType.DSL);
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get(), Optional.empty());
}
use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.
the class VortoService method getMappings.
public List<MappingModel> getMappings(String generatorKey, String namespace, String name, String version) {
Optional<byte[]> mappingResources = downloadUrl(urlForMapping(generatorKey, namespace, name, version));
if (mappingResources.isPresent()) {
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(mappingResources.get()))).read();
List<Model> models = workspace.get().stream().filter(p -> p instanceof MappingModel).collect(Collectors.toList());
return models.stream().map(MappingModel.class::cast).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.
the class ModelReaderTest method testReadInfomodelFromZipFile.
@Test
public void testReadInfomodelFromZipFile() throws Exception {
IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("models.zip"))).read();
Model model = workspace.get().stream().filter(p -> p.getName().equals("TI_SensorTag_CC2650")).findAny().get();
assertNotNull(model);
assertTrue(model instanceof InformationModel);
}
Aggregations