Search in sources :

Example 11 with ModelContent

use of org.eclipse.vorto.model.ModelContent in project vorto by eclipse.

the class DefaultGeneratorPluginService method doGenerateWithApiVersion2.

private GeneratedOutput doGenerateWithApiVersion2(ModelId modelId, String serviceKey, Map<String, String> requestParams, String baseUrl) {
    ModelIdToModelContentConverter converter = new ModelIdToModelContentConverter(this.modelRepositoryFactory);
    ModelContent content = converter.convert(modelId, Optional.of(serviceKey));
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Generating with V2. Sending following json content {}", new ObjectMapper().writeValueAsString(content));
        }
    } catch (JsonProcessingException e) {
        LOGGER.trace("Error processing JSON for logging", e);
    }
    ResponseEntity<byte[]> response = restTemplate.exchange(baseUrl + "/api/2/plugins/generators/{pluginkey}" + attachRequestParams(requestParams), HttpMethod.PUT, new HttpEntity<>(content), byte[].class, serviceKey);
    return new GeneratedOutput(response.getBody(), extractFileNameFromHeader(response), response.getHeaders().getContentLength());
}
Also used : ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ModelContent(org.eclipse.vorto.model.ModelContent) GeneratedOutput(org.eclipse.vorto.repository.plugin.generator.GeneratedOutput) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 12 with ModelContent

use of org.eclipse.vorto.model.ModelContent in project vorto by eclipse.

the class ModelIdToModelContentConverter method convert.

@Override
public ModelContent convert(ModelId modelId, Optional<String> platformKey) {
    modelId = repositoryFactory.getRepositoryByNamespace(modelId.getNamespace()).getLatestModelVersionIfLatestTagIsSet(modelId);
    if (!repositoryFactory.getRepositoryByModel(modelId).exists(modelId)) {
        throw new ModelNotFoundException(String.format("Model [%s] does not exist", modelId.getPrettyFormat()), null);
    }
    ModelWorkspaceReader workspaceReader = getWorkspaceForModel(modelId);
    ModelContent result = new ModelContent();
    result.setRoot(modelId);
    if (platformKey.isPresent()) {
        final List<ModelInfo> mappingResources = repositoryFactory.getRepositoryByModel(modelId).getMappingModelsForTargetPlatform(modelId, platformKey.get(), Optional.empty());
        if (!mappingResources.isEmpty()) {
            // adding to workspace reader in order to resolve cross linking between mapping models correctly
            mappingResources.forEach(mapping -> workspaceReader.addFile(new ByteArrayInputStream(repositoryFactory.getRepositoryByModel(mapping.getId()).getFileContent(mapping.getId(), Optional.empty()).get().getContent()), org.eclipse.vorto.model.ModelType.Mapping));
            final IModelWorkspace workspace = workspaceReader.read();
            workspace.get().forEach(model -> {
                Optional<MappingModel> mappingModel = getMappingModelForModel(mappingResources, model);
                if (mappingModel.isPresent()) {
                    AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), mappingModel);
                    createdModel.setTargetPlatformKey(platformKey.get());
                    result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
                } else {
                    result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty()));
                }
            });
        } else {
            final IModelWorkspace workspace = workspaceReader.read();
            workspace.get().forEach(model -> {
                AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty());
                createdModel.setTargetPlatformKey(platformKey.get());
                result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
            });
        }
    } else {
        final IModelWorkspace workspace = workspaceReader.read();
        workspace.get().forEach(model -> {
            AbstractModel createdModel = ModelDtoFactory.createResource(flattenHierarchy(model), Optional.empty());
            result.getModels().put(new ModelId(model.getName(), model.getNamespace(), model.getVersion()), createdModel);
        });
    }
    return result;
}
Also used : ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ModelContent(org.eclipse.vorto.model.ModelContent) AbstractModel(org.eclipse.vorto.model.AbstractModel) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ModelId(org.eclipse.vorto.model.ModelId)

Aggregations

ModelContent (org.eclipse.vorto.model.ModelContent)12 Test (org.junit.Test)6 EntityModel (org.eclipse.vorto.model.EntityModel)3 ModelContentToEcoreConverter (org.eclipse.vorto.model.conversion.ModelContentToEcoreConverter)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Optional (java.util.Optional)2 DictionaryPropertyType (org.eclipse.vorto.core.api.model.datatype.DictionaryPropertyType)2 PrimitivePropertyType (org.eclipse.vorto.core.api.model.datatype.PrimitivePropertyType)2 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)2 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)2 Model (org.eclipse.vorto.core.api.model.model.Model)2 FunctionblockModel (org.eclipse.vorto.model.FunctionblockModel)2 Infomodel (org.eclipse.vorto.model.Infomodel)2 ModelId (org.eclipse.vorto.model.ModelId)2 ClassPathResource (org.springframework.core.io.ClassPathResource)2 Context (com.amazonaws.services.lambda.runtime.Context)1 RequestStreamHandler (com.amazonaws.services.lambda.runtime.RequestStreamHandler)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1