diff options
Diffstat (limited to 'catalog-model')
3 files changed, 73 insertions, 38 deletions
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 391add9a94..f79f1501e7 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 @@ -303,8 +303,38 @@ public class DataTypeOperation extends AbstractOperation { return PropertyDefinitionDtoMapper.mapFrom(propertyData); } - public void addPropertyToAdditionalTypeDataType(DataTypeDataDefinition dataTypeDataDefinition, PropertyDefinitionDto property) { - modelOperation.addPropertyToAdditionalType(ElementTypeEnum.DATA_TYPE, property, dataTypeDataDefinition.getModel(), dataTypeDataDefinition.getName()); + public void updatePropertyInAdditionalTypeDataType(final DataTypeDataDefinition dataTypeDataDefinition, + final PropertyDefinitionDto property, + final boolean isAdd) { + modelOperation.updatePropertyInAdditionalType(ElementTypeEnum.DATA_TYPE, property, dataTypeDataDefinition.getModel(), + dataTypeDataDefinition.getName(), isAdd); + } + + public PropertyDefinitionDto deleteProperty(final DataTypeDataDefinition dataTypeDataDefinition, final String propertyId) { + final List<PropertyDefinition> propertiesData = findAllProperties(dataTypeDataDefinition.getUniqueId()); + final String dataTypeDataDefinitionName = dataTypeDataDefinition.getName(); + if (CollectionUtils.isEmpty(propertiesData)) { + throw new OperationException(ActionStatus.PROPERTY_NOT_FOUND, + String.format("Failed to find property '%s' for data type '%s'", propertyId, dataTypeDataDefinitionName)); + } + Optional<PropertyDefinition> optionalPropertyDefinition = propertiesData.stream() + .filter(propertyDataDefinition -> propertyDataDefinition.getUniqueId().equals(propertyId)).findAny(); + optionalPropertyDefinition.orElseThrow(() -> { + throw new OperationException(ActionStatus.PROPERTY_NOT_FOUND, + String.format("Failed to find property '%s' for data type '%s'", propertyId, dataTypeDataDefinitionName)); + }); + final Either<PropertyData, JanusGraphOperationStatus> statusEither = propertyOperation.deletePropertyFromGraph(propertyId); + if (statusEither.isRight()) { + throw new OperationException(ActionStatus.PROPERTY_NOT_FOUND, + String.format("Failed to delete property '%s' from data type '%s'", propertyId, dataTypeDataDefinitionName)); + } + final PropertyDefinition propertyDefinition = optionalPropertyDefinition.get(); + final PropertyData propertyData = statusEither.left().value(); + final PropertyDataDefinition propertyDataDefinition = propertyData.getPropertyDataDefinition(); + propertyDataDefinition.setName(propertyDefinition.getName()); + propertyDataDefinition.setPropertyConstraints(propertyData.getConstraints()); + propertiesData.remove(propertyDefinition); + return PropertyDefinitionDtoMapper.mapFrom(propertyDataDefinition); } } 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 311b4719bd..19289411c3 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 @@ -18,6 +18,8 @@ */ package org.openecomp.sdc.be.model.operations.impl; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DATA_TYPES; +import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES; import static org.openecomp.sdc.common.api.Constants.ADDITIONAL_TYPE_DEFINITIONS; import fj.data.Either; @@ -220,11 +222,11 @@ public class ModelOperation { public List<Model> findAllModels() { return findModelsByCriteria(Collections.emptyMap()); } - + public List<Model> findModels(final ModelTypeEnum modelType) { final Map<GraphPropertyEnum, Object> propertyCriteria = new EnumMap<>(GraphPropertyEnum.class); propertyCriteria.put(GraphPropertyEnum.MODEL_TYPE, modelType.getValue()); - + return findModelsByCriteria(propertyCriteria); } @@ -259,7 +261,7 @@ public class ModelOperation { if (optionalModelTypeEnum.isPresent()) { modelType = optionalModelTypeEnum.get(); } - + final Either<ImmutablePair<ModelData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Model), UniqueIdBuilder.buildModelUid(modelName), GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Model, ModelData.class); @@ -298,8 +300,8 @@ public class ModelOperation { } Map<String, Object> typesYamlMap = new Yaml().loadAs(typesYaml, Map.class); - if (typesYamlMap.containsKey("data_types")){ - typesYamlMap = (Map<String, Object>) typesYamlMap.get("data_types"); + if (typesYamlMap.containsKey(DATA_TYPES.getElementName())) { + typesYamlMap = (Map<String, Object>) typesYamlMap.get(DATA_TYPES.getElementName()); } removeExistingTypesFromDefaultImports(elementTypeEnum, typesYamlMap, rebuiltModelImportList); @@ -378,7 +380,7 @@ public class ModelOperation { if (deleteParentNodeByModel.isRight()) { final var janusGraphOperationStatus = deleteParentNodeByModel.right().value(); log.error(EcompLoggerErrorCode.DATA_ERROR, ModelOperation.class.getName(), - "Failed to delete model {} on JanusGraph with status {}", new Object[] {model.getName(), janusGraphOperationStatus}); + "Failed to delete model {} on JanusGraph with status {}", model.getName(), janusGraphOperationStatus); throw new OperationException(ActionStatus.COULD_NOT_DELETE_MODEL, model.getName()); } } @@ -397,28 +399,28 @@ public class ModelOperation { final Map<String, Object> existingTypeContent = getExistingTypes(elementTypeEnum, additionalTypeDefinitionsImportOptional.get()); final Set<String> existingTypeNames = existingTypeContent.keySet(); - final Map<String, Object> typesToUpate = new HashMap<>(); - Map<String, Object> newTypesYaml = new Yaml().load(typesYaml); - if (newTypesYaml.containsKey("data_types")){ - newTypesYaml = (Map<String, Object>) newTypesYaml.get("data_types"); + if (newTypesYaml.containsKey(DATA_TYPES.getElementName())) { + newTypesYaml = (Map<String, Object>) newTypesYaml.get(DATA_TYPES.getElementName()); } + final Map<String, Object> typesToUpate = new HashMap<>(); newTypesYaml.entrySet().stream().filter(entry -> existingTypeNames.contains(entry.getKey())).forEach(newTypeToUpdate -> { - final Map<String, Object> propertiesInNewDef = (Map<String, Object>) ((Map<String, Object>) newTypeToUpdate.getValue()).get("properties"); + final Map<String, Object> propertiesInNewDef = + (Map<String, Object>) ((Map<String, Object>) newTypeToUpdate.getValue()).get(PROPERTIES.getElementName()); final Map<String, Object> existingProperties = - (Map<String, Object>) ((Map<String, Object>) existingTypeContent.get(newTypeToUpdate.getKey())).get("properties"); + (Map<String, Object>) ((Map<String, Object>) existingTypeContent.get(newTypeToUpdate.getKey())).get(PROPERTIES.getElementName()); final List<Entry<String, Object>> propertiesMissingFromNewDef = MapUtils.isEmpty(existingProperties) ? Collections.emptyList() - : existingProperties.entrySet().stream() - .filter(existingPropEntry -> !propertiesInNewDef.keySet().contains(existingPropEntry.getKey())) - .collect(Collectors.toList()); + : existingProperties.entrySet().stream() + .filter(existingPropEntry -> !propertiesInNewDef.keySet().contains(existingPropEntry.getKey())) + .collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(propertiesMissingFromNewDef)) { typesToUpate.put(newTypeToUpdate.getKey(), newTypeToUpdate.getValue()); propertiesMissingFromNewDef - .forEach(existingPropToAdd -> propertiesInNewDef.put(existingPropToAdd.getKey(), existingPropToAdd.getValue())); + .forEach(existingPropToAdd -> propertiesInNewDef.put(existingPropToAdd.getKey(), existingPropToAdd.getValue())); } }); if (MapUtils.isNotEmpty(typesToUpate)) { @@ -426,48 +428,51 @@ public class ModelOperation { } } } - - private Optional<ToscaImportByModel> getAdditionalTypes(final String modelName) { + + private Optional<ToscaImportByModel> getAdditionalTypes(final String modelName) { final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName); return modelImportList.stream().filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); } private Map<String, Object> getExistingTypes(final ElementTypeEnum elementTypeEnum, final ToscaImportByModel additionalTypeDefinitionsImport) { final Map<String, Object> existingContent = new Yaml().load(additionalTypeDefinitionsImport.getContent()); - return (Map<String, Object>) existingContent.get(elementTypeEnum.getToscaEntryName()); + return (Map<String, Object>) existingContent.get(elementTypeEnum.getToscaEntryName()); } - public void addPropertyToAdditionalType(final ElementTypeEnum elementTypeEnum, final PropertyDefinitionDto property, - final String modelName, final String name) { + public void updatePropertyInAdditionalType(final ElementTypeEnum elementTypeEnum, final PropertyDefinitionDto property, + final String modelName, final String name, boolean isAdd) { final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName); final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = modelImportList.stream() - .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); - final ToscaImportByModel additionalTypeDefinitionsImport; - final List<ToscaImportByModel> rebuiltModelImportList; + .filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny(); 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 ToscaImportByModel additionalTypeDefinitionsImport = additionalTypeDefinitionsImportOptional.get(); + final List<ToscaImportByModel> rebuiltModelImportList = modelImportList.stream() + .filter(toscaImportByModel -> !ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(toscaImportByModel.getFullPath()))) + .collect(Collectors.toList()); final Map<String, Object> originalContent = new Yaml().load(additionalTypeDefinitionsImport.getContent()); - additionalTypeDefinitionsImport.setContent(buildPropertyAdditionalTypeDefinitionContent(elementTypeEnum, name, property, originalContent)); + additionalTypeDefinitionsImport.setContent( + buildPropertyAdditionalTypeDefinitionContent(elementTypeEnum, name, property, originalContent, isAdd)); rebuiltModelImportList.add(additionalTypeDefinitionsImport); toscaModelImportCassandraDao.saveAll(modelName, rebuiltModelImportList); } private String buildPropertyAdditionalTypeDefinitionContent(final ElementTypeEnum elementTypeEnum, final String name, - final PropertyDefinitionDto property, final Map<String, Object> originalContent) { + final PropertyDefinitionDto property, final Map<String, Object> originalContent, + boolean isAdd) { final Map<String, Object> originalTypeContent = (Map<String, Object>) originalContent.get(elementTypeEnum.getToscaEntryName()); Map<String, Object> typeContent = (Map<String, Object>) originalTypeContent.get(name); - Map<String, Object> typeProperties = (Map<String, Object>) typeContent.get("properties"); - if (typeProperties == null) { + Map<String, Object> typeProperties = (Map<String, Object>) typeContent.get(PROPERTIES.getElementName()); + if (MapUtils.isEmpty(typeProperties)) { typeProperties = new HashMap<>(); } - Map<String, Object> typeProp = constructProperty(property); - typeProperties.put(property.getName(), typeProp); - typeContent.put("properties", typeProperties); + if (isAdd) { + typeProperties.put(property.getName(), constructProperty(property)); + } else { + typeProperties.remove(property.getName()); + } + typeContent.put(PROPERTIES.getElementName(), typeProperties); return new YamlUtil().objectToYaml(originalContent); } diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java index 8fe89216fa..1bd4d9dbac 100644 --- a/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/operations/impl/ModelOperationTest.java @@ -613,7 +613,7 @@ class ModelOperationTest extends ModelTestBase { property.setRequired(true); String dataTypeName = "tosca.datatypes.nfv.PreviouslyExistingType1"; - modelOperation.addPropertyToAdditionalType(ElementTypeEnum.DATA_TYPE, property, modelName, dataTypeName); + modelOperation.updatePropertyInAdditionalType(ElementTypeEnum.DATA_TYPE, property, modelName, dataTypeName, true); ArgumentCaptor<List<ToscaImportByModel>> importListArgumentCaptor = ArgumentCaptor.forClass(List.class); verify(toscaModelImportCassandraDao).saveAll(eq(modelName), importListArgumentCaptor.capture()); |