Search in sources :

Example 1 with IModelWorkspace

use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.

the class ModelRepositoryController method getModelContentByModelAndMappingId.

@ApiOperation(value = "Returns the model content including target platform specific attributes for the given model- and mapping modelID")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Wrong input"), @ApiResponse(code = 404, message = "Model not found") })
@RequestMapping(value = "/content/{modelId:.+}/mapping/{mappingId:.+}", method = RequestMethod.GET)
public AbstractModel getModelContentByModelAndMappingId(@ApiParam(value = "The model ID (prettyFormat)", required = true) @PathVariable final String modelId, @ApiParam(value = "The mapping Model ID (prettyFormat)", required = true) @PathVariable final String mappingId) {
    ModelInfo vortoModelInfo = modelRepository.getById(ModelId.fromPrettyFormat(modelId));
    ModelInfo mappingModelInfo = modelRepository.getById(ModelId.fromPrettyFormat(mappingId));
    if (vortoModelInfo == null) {
        throw new ModelNotFoundException("Could not find vorto model with ID: " + modelId);
    } else if (mappingModelInfo == null) {
        throw new ModelNotFoundException("Could not find mapping with ID: " + mappingId);
    }
    byte[] mappingContentZip = createZipWithAllDependencies(mappingModelInfo.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(vortoModelInfo.getId(), ContentType.DSL);
    workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelContent))).read();
    return ModelDtoFactory.createResource(workspace.get().stream().filter(p -> p.getName().equals(vortoModelInfo.getId().getName())).findFirst().get(), Optional.of(mappingModel));
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) RequestParam(org.springframework.web.bind.annotation.RequestParam) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URLDecoder(java.net.URLDecoder) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) ApiResponses(io.swagger.annotations.ApiResponses) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) Value(org.springframework.beans.factory.annotation.Value) Logger(org.apache.log4j.Logger) ApiOperation(io.swagger.annotations.ApiOperation) AbstractRepositoryController(org.eclipse.vorto.repository.web.AbstractRepositoryController) ByteArrayInputStream(java.io.ByteArrayInputStream) AbstractModel(org.eclipse.vorto.repository.api.AbstractModel) Api(io.swagger.annotations.Api) ZipEntry(java.util.zip.ZipEntry) ModelId(org.eclipse.vorto.repository.api.ModelId) UploadTooLargeException(org.eclipse.vorto.repository.web.core.exceptions.UploadTooLargeException) ContentType(org.eclipse.vorto.repository.core.IModelRepository.ContentType) HttpServletResponse(javax.servlet.http.HttpServletResponse) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) ApiResponse(io.swagger.annotations.ApiResponse) Optional(java.util.Optional) MultipartFile(org.springframework.web.multipart.MultipartFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ModelInfo(org.eclipse.vorto.repository.api.ModelInfo) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ModelNotFoundException(org.eclipse.vorto.repository.api.exception.ModelNotFoundException) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with IModelWorkspace

use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.

the class VortoService method getModel.

public Optional<InformationModel> getModel(String namespace, String name, String version) {
    Optional<byte[]> modelResources = downloadUrl(urlForModel(namespace, name, version));
    if (!modelResources.isPresent()) {
        return Optional.empty();
    }
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(new ByteArrayInputStream(modelResources.get()))).read();
    return toInformationModel(workspace.get().stream().filter(p -> p.getName().equals(name)).findFirst().get());
}
Also used : InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Generated(org.eclipse.vorto.codegen.api.Generated) ZipInputStream(java.util.zip.ZipInputStream) GatewayUtils(org.eclipse.vorto.codegen.gateway.utils.GatewayUtils) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DatatypePackageImpl(org.eclipse.vorto.core.api.model.datatype.impl.DatatypePackageImpl) Model(org.eclipse.vorto.core.api.model.model.Model) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Utils(org.eclipse.vorto.codegen.utils.Utils) GenerationResultZip(org.eclipse.vorto.codegen.api.GenerationResultZip) InvocationContext(org.eclipse.vorto.codegen.api.InvocationContext) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) Logger(org.slf4j.Logger) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) Throwables(com.google.common.base.Throwables) NotFoundException(org.eclipse.vorto.codegen.gateway.exception.NotFoundException) Collectors(java.util.stream.Collectors) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) EnvironmentConfig(org.eclipse.vorto.codegen.gateway.config.EnvironmentConfig) FunctionblockPackageImpl(org.eclipse.vorto.core.api.model.functionblock.impl.FunctionblockPackageImpl) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) InformationModelPackageImpl(org.eclipse.vorto.core.api.model.informationmodel.impl.InformationModelPackageImpl) List(java.util.List) HttpEntity(org.springframework.http.HttpEntity) Component(org.springframework.stereotype.Component) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) GeneratorRepository(org.eclipse.vorto.codegen.gateway.repository.GeneratorRepository) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator) Collections(java.util.Collections) Generator(org.eclipse.vorto.codegen.gateway.model.Generator) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace)

Example 3 with IModelWorkspace

use of org.eclipse.vorto.server.commons.reader.IModelWorkspace 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 4 with IModelWorkspace

use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.

the class ModelReaderTest method testMappingFromZipFile.

@Test
public void testMappingFromZipFile() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("mappings.zip"))).read();
    Model model = workspace.get().stream().filter(p -> p instanceof MappingModel).findAny().get();
    assertNotNull(model);
    assertTrue(model instanceof MappingModel);
    assertEquals("Accelerometer_Mapping", model.getName());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Test(org.junit.Test)

Example 5 with IModelWorkspace

use of org.eclipse.vorto.server.commons.reader.IModelWorkspace in project vorto by eclipse.

the class ModelReaderTest method testReadMultipleZipFiles.

@Test
public void testReadMultipleZipFiles() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("mappings.zip"))).addZip(new ZipInputStream(getClass().getClassLoader().getResourceAsStream("models.zip"))).read();
    assertEquals(10, workspace.get().size());
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) IModelWorkspace(org.eclipse.vorto.server.commons.reader.IModelWorkspace) Test(org.junit.Test)

Aggregations

ZipInputStream (java.util.zip.ZipInputStream)13 IModelWorkspace (org.eclipse.vorto.server.commons.reader.IModelWorkspace)13 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 Collectors (java.util.stream.Collectors)8 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)8 Model (org.eclipse.vorto.core.api.model.model.Model)8 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)7 List (java.util.List)6 IOException (java.io.IOException)5 Optional (java.util.Optional)5 Test (org.junit.Test)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ZipEntry (java.util.zip.ZipEntry)4 ZipOutputStream (java.util.zip.ZipOutputStream)4 IOUtils (org.apache.commons.io.IOUtils)4 InvocationContext (org.eclipse.vorto.codegen.api.InvocationContext)4 Api (io.swagger.annotations.Api)3 ApiOperation (io.swagger.annotations.ApiOperation)3