Search in sources :

Example 16 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class RefactoringWorkspaceTest method testChangeNamespaceWithReferences.

@Test
public void testChangeNamespaceWithReferences() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Brightness.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Dimmer.type"), ModelType.Datatype).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("private.vorto.alex").execute();
    assertEquals(2, changeSet.get().size());
    assertEquals(2, changeSet.getChanges().size());
    Entity dimmer = (Entity) changeSet.get().stream().filter(m -> m.getName().equals("Dimmer")).findFirst().get();
    assertEquals(2, dimmer.getReferences().size());
    assertEquals("private.vorto.alex.iot", dimmer.getNamespace());
    assertEquals("private.vorto.alex.iot.Brightness", dimmer.getReferences().get(0).getImportedNamespace());
    assertEquals("org.eclipse.vorto.Temperature", dimmer.getReferences().get(1).getImportedNamespace());
    ObjectPropertyType type = (ObjectPropertyType) dimmer.getProperties().get(0).getType();
    assertTrue(type.getType() instanceof Enum);
    assertEquals("private.vorto.alex.iot", type.getType().getNamespace());
}
Also used : FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) BeforeClass(org.junit.BeforeClass) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Assert.assertTrue(org.junit.Assert.assertTrue) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) Test(org.junit.Test) Enum(org.eclipse.vorto.core.api.model.datatype.Enum) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) Assert.assertEquals(org.junit.Assert.assertEquals) ModelType(org.eclipse.vorto.model.ModelType) Model(org.eclipse.vorto.core.api.model.model.Model) Enum(org.eclipse.vorto.core.api.model.datatype.Enum) Entity(org.eclipse.vorto.core.api.model.datatype.Entity) ObjectPropertyType(org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 17 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class RefactoringWorkspaceTest method testChangeNamespaceWithManyReferences.

@Test
public void testChangeNamespaceWithManyReferences() {
    IModelWorkspace workspace = IModelWorkspace.newReader().addFile(getClass().getClassLoader().getResourceAsStream("dsls/Unit.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/ConnectivityStatus.type"), ModelType.Datatype).addFile(getClass().getClassLoader().getResourceAsStream("dsls/Connectivity.fbmodel"), ModelType.Functionblock).read();
    ChangeSet changeSet = RefactoringTask.from(workspace).toNamespaceForAllModels("vorto.private.alex").execute();
    assertEquals(3, changeSet.get().size());
    assertEquals(3, changeSet.getChanges().size());
    Model fbmodel = changeSet.get().stream().filter(c -> c.getName().equals("Connectivity")).findAny().get();
    assertEquals("vorto.private.alex.org.eclipse.vorto", fbmodel.getNamespace());
    assertEquals("vorto.private.alex.org.eclipse.vorto.types.ConnectivityStatus", fbmodel.getReferences().get(0).getImportedNamespace());
    assertEquals("org.eclipse.vorto.types.SomeUnit", fbmodel.getReferences().get(1).getImportedNamespace());
}
Also used : FunctionblockModel(org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel) Model(org.eclipse.vorto.core.api.model.model.Model) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Test(org.junit.Test)

Example 18 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace in project vorto by eclipse.

the class DefaultPayloadMappingService method getModelContentByModelAndMappingId.

private IModel getModelContentByModelAndMappingId(final String _modelId, @PathVariable final String mappingId) {
    final ModelId modelId = ModelId.fromPrettyFormat(_modelId);
    final ModelId mappingModelId = ModelId.fromPrettyFormat(mappingId);
    IModelRepository repository = this.modelRepositoryFactory.getRepositoryByModel(modelId);
    ModelInfo vortoModelInfo = repository.getById(modelId);
    ModelInfo mappingModelInfo = this.modelRepositoryFactory.getRepositoryByModel(mappingModelId).getById(mappingModelId);
    if (vortoModelInfo == null) {
        throw new ModelNotFoundException(String.format("Could not find vorto model with ID: %s", modelId));
    } else if (mappingModelInfo == null) {
        throw new ModelNotFoundException(String.format("Could not find mapping with ID: %s", mappingId));
    }
    IModelWorkspace mappingWorkspace = getWorkspaceForModel(mappingModelInfo.getId());
    Optional<Model> model = mappingWorkspace.get().stream().filter(_model -> ModelUtils.fromEMFModelId(ModelIdFactory.newInstance(_model)).equals(vortoModelInfo.getId())).findFirst();
    if (model.isPresent()) {
        final Model flattenedModel = ModelConversionUtils.convertToFlatHierarchy(model.get());
        return ModelDtoFactory.createResource(flattenedModel, Optional.of((MappingModel) mappingWorkspace.get().stream().filter(_model -> _model instanceof MappingModel && mappingMatchesModelId((MappingModel) _model, vortoModelInfo)).findFirst().get()));
    } else {
        return null;
    }
}
Also used : IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) PathVariable(org.springframework.web.bind.annotation.PathVariable) ModelIdFactory(org.eclipse.vorto.core.api.model.model.ModelIdFactory) DependencyManager(org.eclipse.vorto.repository.core.impl.utils.DependencyManager) IPayloadMappingService(org.eclipse.vorto.repository.mapping.IPayloadMappingService) MappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.MappingSpecification) IUserContext(org.eclipse.vorto.repository.core.IUserContext) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Infomodel(org.eclipse.vorto.model.Infomodel) ModelIdToModelContentConverter(org.eclipse.vorto.repository.conversion.ModelIdToModelContentConverter) ArrayList(java.util.ArrayList) ModelConversionUtils(org.eclipse.vorto.core.api.model.ModelConversionUtils) Model(org.eclipse.vorto.core.api.model.model.Model) IModelRepository(org.eclipse.vorto.repository.core.IModelRepository) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) HashSet(java.util.HashSet) Logger(org.apache.log4j.Logger) IWorkflowService(org.eclipse.vorto.repository.workflow.IWorkflowService) InfomodelValue(org.eclipse.vorto.model.runtime.InfomodelValue) IMappingSpecification(org.eclipse.vorto.mapping.engine.model.spec.IMappingSpecification) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityModel(org.eclipse.vorto.model.EntityModel) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) Service(org.springframework.stereotype.Service) Map(java.util.Map) IModelRepositoryFactory(org.eclipse.vorto.repository.core.IModelRepositoryFactory) MappingSpecificationSerializer(org.eclipse.vorto.mapping.engine.serializer.MappingSpecificationSerializer) ModelProperty(org.eclipse.vorto.model.ModelProperty) IModel(org.eclipse.vorto.model.IModel) FileContent(org.eclipse.vorto.repository.core.FileContent) MappingEngine(org.eclipse.vorto.mapping.engine.MappingEngine) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) ModelDtoFactory(org.eclipse.vorto.repository.web.core.ModelDtoFactory) ModelId(org.eclipse.vorto.model.ModelId) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) Stereotype(org.eclipse.vorto.model.Stereotype) List(java.util.List) ModelContent(org.eclipse.vorto.model.ModelContent) EnumModel(org.eclipse.vorto.model.EnumModel) ModelWorkspaceReader(org.eclipse.vorto.utilities.reader.ModelWorkspaceReader) Optional(java.util.Optional) ModelUtils(org.eclipse.vorto.repository.utils.ModelUtils) WorkflowException(org.eclipse.vorto.repository.workflow.WorkflowException) ModelInfo(org.eclipse.vorto.repository.core.ModelInfo) ModelNotFoundException(org.eclipse.vorto.repository.core.ModelNotFoundException) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel) Model(org.eclipse.vorto.core.api.model.model.Model) EntityModel(org.eclipse.vorto.model.EntityModel) IModel(org.eclipse.vorto.model.IModel) FunctionblockModel(org.eclipse.vorto.model.FunctionblockModel) EnumModel(org.eclipse.vorto.model.EnumModel) ModelId(org.eclipse.vorto.model.ModelId) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Example 19 with IModelWorkspace

use of org.eclipse.vorto.utilities.reader.IModelWorkspace 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)

Example 20 with IModelWorkspace

use of org.eclipse.vorto.utilities.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<String> headerAuth) {
    Optional<byte[]> mappingResources = downloadUrl(urlForMapping(generatorKey, namespace, name, version), headerAuth);
    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();
    }
}
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) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) 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) IModelWorkspace(org.eclipse.vorto.utilities.reader.IModelWorkspace) MappingModel(org.eclipse.vorto.core.api.model.mapping.MappingModel)

Aggregations

IModelWorkspace (org.eclipse.vorto.utilities.reader.IModelWorkspace)27 Test (org.junit.Test)19 FunctionblockModel (org.eclipse.vorto.core.api.model.functionblock.FunctionblockModel)16 Model (org.eclipse.vorto.core.api.model.model.Model)15 InformationModel (org.eclipse.vorto.core.api.model.informationmodel.InformationModel)13 ModelWorkspaceReader (org.eclipse.vorto.utilities.reader.ModelWorkspaceReader)11 ZipInputStream (java.util.zip.ZipInputStream)10 MappingModel (org.eclipse.vorto.core.api.model.mapping.MappingModel)8 ModelType (org.eclipse.vorto.model.ModelType)8 BeforeClass (org.junit.BeforeClass)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Entity (org.eclipse.vorto.core.api.model.datatype.Entity)7 ObjectPropertyType (org.eclipse.vorto.core.api.model.datatype.ObjectPropertyType)7 Property (org.eclipse.vorto.core.api.model.datatype.Property)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Optional (java.util.Optional)6 ModelConversionUtils (org.eclipse.vorto.core.api.model.ModelConversionUtils)6 Collectors (java.util.stream.Collectors)5 FunctionBlock (org.eclipse.vorto.core.api.model.functionblock.FunctionBlock)5