Search in sources :

Example 1 with Generator

use of org.eclipse.vorto.codegen.spi.model.Generator in project vorto by eclipse.

the class VortoService method generate.

public IGenerationResult generate(String key, String namespace, String name, String version, Map<String, String> parameters, Optional<String> headerAuth) {
    LOGGER.info(String.format("Generating for Platform [%s] and Model [%s.%s:%s]", key, namespace, name, version));
    Generator generator = repo.get(key).orElseThrow(GatewayUtils.notFound(String.format("[Generator %s]", key)));
    InformationModel model = getModel(namespace, name, version, headerAuth).orElseThrow(GatewayUtils.notFound(String.format("[Model %s.%s:%s]", namespace, name, version)));
    List<MappingModel> mappings = getMappings(key, namespace, name, version, headerAuth);
    InvocationContext invocationContext = new InvocationContext(mappings, repo.newGeneratorLookup(), parameters);
    try {
        final ModelId modelId = new ModelId(name, namespace, version);
        List<Attachment> attachments = modelRepository.getAttachments(modelId);
        Optional<Attachment> importedFile = attachments.stream().filter(attachment -> attachment.getTagById(Attachment.TAG_IMPORTED.getLabel()) != null).findAny();
        if (importedFile.isPresent()) {
            byte[] importedFileContent = modelRepository.downloadAttachment(modelId, importedFile.get().getFilename());
            invocationContext.setImportedFile(new FileContent(importedFile.get().getFilename(), importedFileContent));
        }
    } catch (Exception ex) {
    // do logging
    }
    return generate(generator.getInstance(), model, invocationContext);
}
Also used : InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Generated(org.eclipse.vorto.codegen.api.Generated) ZipInputStream(java.util.zip.ZipInputStream) GeneratorRepository(org.eclipse.vorto.codegen.spi.repository.GeneratorRepository) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) LoggerFactory(org.slf4j.LoggerFactory) IRepositoryClient(org.eclipse.vorto.repository.client.IRepositoryClient) Autowired(org.springframework.beans.factory.annotation.Autowired) DatatypePackageImpl(org.eclipse.vorto.core.api.model.datatype.impl.DatatypePackageImpl) Generator(org.eclipse.vorto.codegen.spi.model.Generator) AbstractGeneratorConfiguration(org.eclipse.vorto.codegen.spi.config.AbstractGeneratorConfiguration) ModelConversionUtils(org.eclipse.vorto.core.api.model.ModelConversionUtils) Model(org.eclipse.vorto.core.api.model.model.Model) Value(org.springframework.beans.factory.annotation.Value) GatewayUtils(org.eclipse.vorto.codegen.spi.utils.GatewayUtils) ByteArrayInputStream(java.io.ByteArrayInputStream) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) 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) HttpMethod(org.springframework.http.HttpMethod) Throwables(com.google.common.base.Throwables) Attachment(org.eclipse.vorto.repository.client.attachment.Attachment) Collectors(java.util.stream.Collectors) ModelId(org.eclipse.vorto.model.ModelId) IGenerationResult(org.eclipse.vorto.codegen.api.IGenerationResult) FunctionblockPackageImpl(org.eclipse.vorto.core.api.model.functionblock.impl.FunctionblockPackageImpl) InformationModelPackageImpl(org.eclipse.vorto.core.api.model.informationmodel.impl.InformationModelPackageImpl) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) HttpEntity(org.springframework.http.HttpEntity) Component(org.springframework.stereotype.Component) PostConstruct(javax.annotation.PostConstruct) InformationModelFactory(org.eclipse.vorto.core.api.model.informationmodel.InformationModelFactory) Optional(java.util.Optional) ModelContentToEcoreConverter(org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter) FileContent(org.eclipse.vorto.codegen.api.FileContent) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator) Collections(java.util.Collections) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) Attachment(org.eclipse.vorto.repository.client.attachment.Attachment) RestClientException(org.springframework.web.client.RestClientException) FileContent(org.eclipse.vorto.codegen.api.FileContent) InvocationContext(org.eclipse.vorto.codegen.api.InvocationContext) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) ModelId(org.eclipse.vorto.model.ModelId) Generator(org.eclipse.vorto.codegen.spi.model.Generator) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator)

Example 2 with Generator

use of org.eclipse.vorto.codegen.spi.model.Generator in project vorto by eclipse.

the class VortoService method generate.

public IGenerationResult generate(ModelContent model, String pluginkey, Map<String, String> params) {
    LOGGER.info(String.format("Generating for [%s]", model.getRoot().getPrettyFormat()));
    Model converted = converter.convert(model, Optional.empty());
    Generator generator = repo.get(pluginkey).orElseThrow(GatewayUtils.notFound(String.format("[Generator %s]", pluginkey)));
    InvocationContext invocationContext = InvocationContext.simpleInvocationContext(params);
    InformationModel infomodel = Utils.toInformationModel(converted);
    return generate(generator.getInstance(), infomodel, invocationContext);
}
Also used : 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) InformationModel(org.eclipse.vorto.core.api.model.informationmodel.InformationModel) InvocationContext(org.eclipse.vorto.codegen.api.InvocationContext) Generator(org.eclipse.vorto.codegen.spi.model.Generator) IVortoCodeGenerator(org.eclipse.vorto.codegen.api.IVortoCodeGenerator)

Aggregations

IVortoCodeGenerator (org.eclipse.vorto.codegen.api.IVortoCodeGenerator)2 InvocationContext (org.eclipse.vorto.codegen.api.InvocationContext)2 Generator (org.eclipse.vorto.codegen.spi.model.Generator)2 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)2 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)2 Model (org.eclipse.vorto.core.api.model.model.Model)2 Throwables (com.google.common.base.Throwables)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 ZipInputStream (java.util.zip.ZipInputStream)1 PostConstruct (javax.annotation.PostConstruct)1 FileContent (org.eclipse.vorto.codegen.api.FileContent)1 Generated (org.eclipse.vorto.codegen.api.Generated)1 GenerationResultZip (org.eclipse.vorto.codegen.api.GenerationResultZip)1 IGenerationResult (org.eclipse.vorto.codegen.api.IGenerationResult)1 AbstractGeneratorConfiguration (org.eclipse.vorto.codegen.spi.config.AbstractGeneratorConfiguration)1