From db3e2ef238fa29e06cec3cb3f5b11fb407b161ce Mon Sep 17 00:00:00 2001 From: imamSidero Date: Wed, 21 Dec 2022 17:51:43 +0000 Subject: Provide tosca function to custom datatypes of list and map Providing the capability to add tosca function as the custom datatypes of list and map Issue-ID: SDC-4311 Signed-off-by: Imam hussain Change-Id: I7ec8943d8008440b091fc4eaa2aba49cdadcda8d --- .../csar/YamlTemplateParsingHandler.java | 24 +++++++-------- .../impl/ComponentInstanceBusinessLogic.java | 12 ++++---- .../PropertyValueConstraintValidationUtil.java | 35 ++++++++++++++++++---- 3 files changed, 45 insertions(+), 26 deletions(-) (limited to 'catalog-be') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java index 150007408a..5a6a7c91e4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/YamlTemplateParsingHandler.java @@ -1378,25 +1378,23 @@ public class YamlTemplateParsingHandler { for (Object objValue : propValueList) { if (objValue instanceof Map) { Map objMap = (Map) objValue; + Map propValueMap = new HashMap(); + propValueMap.put(String.valueOf(index),objValue); + final Collection subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>()); + if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) { + Collection existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions(); + if (existingSubPropertyToscaFunctions == null) { + propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions); + } else { + propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions); + } + } if (objMap.containsKey(GET_INPUT.getElementName())) { fillInputRecursively(propertyDef.getName(), objMap, propertyDef); } else { Set keys = objMap.keySet(); findAndFillInputsListRecursively(propertyDef, objMap, keys); } - if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(objValue)) { - Map propValueMap = new HashMap(); - propValueMap.put(String.valueOf(index),objValue); - final Collection subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>()); - if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) { - Collection existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions(); - if (existingSubPropertyToscaFunctions == null) { - propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions); - } else { - propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions); - } - } - } } else if (objValue instanceof List) { List propSubValueList = (List) objValue; fillInputsListRecursively(propertyDef, propSubValueList); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java index 3797fb9fae..fe6ea03562 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ComponentInstanceBusinessLogic.java @@ -2071,14 +2071,12 @@ public class ComponentInstanceBusinessLogic extends BaseBusinessLogic { if (jsonObject.has(path.get(0))) { objectForPath = jsonObject.get(path.get(0)); } else { - if (path.size() > 1) { - if (StringUtils.isNumeric(path.get(1))) { - objectForPath = new JSONArray(); - } else { - objectForPath = new JSONObject(); - } - jsonObject.put(path.get(0), objectForPath); + if (StringUtils.isNumeric(path.get(0))) { + objectForPath = new JSONArray(); + } else { + objectForPath = new JSONObject(); } + jsonObject.put(path.get(0), objectForPath); } if (path.size() == 1) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java index 6aa59e4844..19fe69e421 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/datamodel/utils/PropertyValueConstraintValidationUtil.java @@ -34,6 +34,7 @@ import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.components.impl.ResponseFormatManager; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction; import org.openecomp.sdc.be.model.ComponentInstanceInput; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InputDefinition; @@ -186,6 +187,21 @@ public class PropertyValueConstraintValidationUtil { try { PropertyDefinition newPropertyWithValue; if (valueMap.containsKey(prop.getName())) { + if (propertyDefinition.getSubPropertyToscaFunctions() != null) { + for (SubPropertyToscaFunction subPropertyToscaFunction : propertyDefinition.getSubPropertyToscaFunctions()) { + final List path = subPropertyToscaFunction.getSubPropertyPath(); + if (path.size() == 1) { + if (path.get(0).equals(prop.getName())) { + return; + } + } + if (path.size() > 1) { + if (path.get(0).equals(propertyDefinition.getToscaSubPath()) && path.get(1).equals(prop.getName())) { + return; + } + } + } + } if (ToscaType.isPrimitiveType(prop.getType())) { newPropertyWithValue = copyPropertyWithNewValue(prop, String.valueOf(valueMap.get(prop.getName()))); if (isPropertyToEvaluate(newPropertyWithValue)) { @@ -297,9 +313,14 @@ public class PropertyValueConstraintValidationUtil { if (propertyDefinition.getSchemaType() == null) { propertyDefinition.setSchema(createStringSchema()); } - Collection list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() { - }); - evaluateCollectionType(propertyDefinition, list); + Collection list = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() {}); + final Map map = new HashMap<>(); + int index = 0; + for (Object obj : list) { + map.put(String.valueOf(index),obj); + index++; + } + evaluateCollectionType(propertyDefinition, map); } catch (ConstraintValueDoNotMatchPropertyTypeException e) { logger.debug(e.getMessage(), e); errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition))); @@ -321,7 +342,7 @@ public class PropertyValueConstraintValidationUtil { } final Map map = ConstraintUtil.parseToCollection(propertyDefinition.getValue(), new TypeReference<>() { }); - evaluateCollectionType(propertyDefinition, map.values()); + evaluateCollectionType(propertyDefinition, map); } catch (ConstraintValueDoNotMatchPropertyTypeException e) { logger.debug(e.getMessage(), e); errorMessages.add(String.format(VALUE_PROVIDED_IN_INVALID_FORMAT_FOR_PROPERTY, getCompletePropertyName(propertyDefinition))); @@ -338,12 +359,14 @@ public class PropertyValueConstraintValidationUtil { } } - private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Collection valueList) { + private void evaluateCollectionType(final PropertyDefinition propertyDefinition, final Map valueMap) { final String schemaType = propertyDefinition.getSchemaType(); - for (final Object value : valueList) { + for (String mapKey : valueMap.keySet()) { + final Object value = valueMap.get(mapKey); try { final PropertyDefinition propertyCopyWithNewValue = copyPropertyWithNewValue(propertyDefinition, objectMapper.writeValueAsString(value)); + propertyCopyWithNewValue.setToscaSubPath(mapKey); if (ToscaType.isPrimitiveType(schemaType)) { evaluateCollectionPrimitiveSchemaType(propertyCopyWithNewValue, schemaType); } else if (ToscaType.isCollectionType(schemaType)) { -- cgit 1.2.3-korg