From aa14db6b062be61ae8cba2bda1bb1aae6737d790 Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Thu, 26 Jan 2023 15:53:57 +0000 Subject: Fix added datatype property not include in model definitions Issue-ID: SDC-4349 Signed-off-by: JvD_Ericsson Change-Id: Ie94d697a64816f83be4783f1e52657b7fdd77b8c --- .../model/operations/impl/DataTypeOperation.java | 5 ++ .../be/model/operations/impl/ModelOperation.java | 64 +++++++++++++++++++++- 2 files changed, 67 insertions(+), 2 deletions(-) (limited to 'catalog-model/src/main/java') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java index 7d01f3f273..103221cb76 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/DataTypeOperation.java @@ -50,6 +50,7 @@ import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; import org.openecomp.sdc.be.model.mapper.PropertyDefinitionDtoMapper; +import org.openecomp.sdc.be.model.normatives.ElementTypeEnum; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.DataTypeData; import org.openecomp.sdc.be.resources.data.PropertyData; @@ -279,4 +280,8 @@ public class DataTypeOperation extends AbstractOperation { return PropertyDefinitionDtoMapper.mapFrom(propertyDataDefinition); } + public void addPropertyToAdditionalTypeDataType(DataTypeDataDefinition dataTypeDataDefinition, PropertyDefinitionDto property) { + modelOperation.addPropertyToAdditionalType(ElementTypeEnum.DATA_TYPE, property, dataTypeDataDefinition.getModel(), dataTypeDataDefinition.getName()); + } + } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java index 8baa9a73c0..2e35c30e06 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java @@ -54,14 +54,15 @@ import org.openecomp.sdc.be.data.model.ToscaImportByModel; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; -import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.Model; +import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException; import org.openecomp.sdc.be.model.normatives.ElementTypeEnum; import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.resources.data.ModelData; +import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Autowired; @@ -424,10 +425,69 @@ public class ModelOperation { final List modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName); return modelImportList.stream().filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); } - + private Map getExistingTypes(final ElementTypeEnum elementTypeEnum, final ToscaImportByModel additionalTypeDefinitionsImport) { final Map existingContent = new Yaml().load(additionalTypeDefinitionsImport.getContent()); return (Map) existingContent.get(elementTypeEnum.getToscaEntryName()); } + public void addPropertyToAdditionalType(final ElementTypeEnum elementTypeEnum, final PropertyDefinitionDto property, + final String modelName, final String name) { + final List modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName); + final Optional additionalTypeDefinitionsImportOptional = modelImportList.stream() + .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); + final ToscaImportByModel additionalTypeDefinitionsImport; + final List rebuiltModelImportList; + if (additionalTypeDefinitionsImportOptional.isEmpty()) { + return; + } + additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get(); + rebuiltModelImportList = modelImportList.stream() + .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath()))) + .collect(Collectors.toList()); + final Map originalContent = new Yaml().load(additionalTypeDefinitionsImport.getContent()); + additionalTypeDefinitionsImport.setContent(buildPropertyAdditionalTypeDefinitionContent(elementTypeEnum, name, property, originalContent)); + rebuiltModelImportList.add(additionalTypeDefinitionsImport); + toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList); + } + + private String buildPropertyAdditionalTypeDefinitionContent(final ElementTypeEnum elementTypeEnum, final String name, + final PropertyDefinitionDto property, final Map originalContent) { + final Map originalTypeContent = (Map) originalContent.get(elementTypeEnum.getToscaEntryName()); + Map typeContent = (Map) originalTypeContent.get(name); + Map typeProperties = (Map) typeContent.get("properties"); + if (typeProperties == null) { + typeProperties = new HashMap<>(); + } + Map typeProp = constructProperty(property); + typeProperties.put(property.getName(), typeProp); + typeContent.put("properties", typeProperties); + return new YamlUtil().objectToYaml(originalContent); + } + + private Map constructProperty(final PropertyDefinitionDto property) { + Map typeProp = new HashMap<>(); + if (property.getType() != null) { + typeProp.put(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName(), property.getType()); + } + if (property.getDescription() != null) { + typeProp.put(TypeUtils.ToscaTagNamesEnum.DESCRIPTION.getElementName(), property.getDescription()); + } + Map schema = new HashMap<>(); + if (property.getSchemaType() != null) { + schema.put(TypeUtils.ToscaTagNamesEnum.TYPE.getElementName(), property.getSchemaType()); + typeProp.put(TypeUtils.ToscaTagNamesEnum.ENTRY_SCHEMA.getElementName(), schema); + } + if (property.getDefaultValue() != null) { + typeProp.put(TypeUtils.ToscaTagNamesEnum.DEFAULT.getElementName(), property.getDefaultValue()); + } + if (property.getRequired() != null) { + typeProp.put(TypeUtils.ToscaTagNamesEnum.REQUIRED.getElementName(), property.getRequired()); + } + if (property.getConstraints() != null) { + typeProp.put(TypeUtils.ToscaTagNamesEnum.CONSTRAINTS.getElementName(), property.getConstraints()); + } + return typeProp; + } + } -- cgit 1.2.3-korg