From bfd727385b9c55d079a5c213b425e88b88a67b7f Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Tue, 30 May 2023 14:57:47 +0100 Subject: Fix formatting error for operation input of complex type Issue-ID: SDC-4523 Signed-off-by: JvD_Ericsson Change-Id: I3dff94cee1d1dbcb26099d02505a679249a6b46e --- .../csar/YamlTemplateParsingHandler.java | 11 ++ .../be/tosca/InterfacesOperationsConverter.java | 129 ++++++++++++++------- 2 files changed, 96 insertions(+), 44 deletions(-) (limited to 'catalog-be/src/main') 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 bdb659b259..b90e3bb0b8 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 @@ -1288,12 +1288,23 @@ public class YamlTemplateParsingHandler { final OperationInputDefinition operationInput ) { if (value instanceof Map) { + final Map valueMap = (Map) value; log.debug("Creating interface operation input '{}'", inputName); Type type = new TypeToken>() { }.getType(); String stringValue = gson.toJson(value, type); if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) { toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) value).ifPresent(operationInput::setToscaFunction); + } else { + final Collection subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>()); + if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) { + Collection existingSubPropertyToscaFunctions = operationInput.getSubPropertyToscaFunctions(); + if (existingSubPropertyToscaFunctions == null) { + operationInput.setSubPropertyToscaFunctions(subPropertyToscaFunctions); + } else { + operationInput.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions); + } + } } operationInput.setValue(stringValue); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java index 8f5f271407..e56472a426 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.openecomp.sdc.be.tosca; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT; @@ -20,28 +21,35 @@ import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS; import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS; import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.gson.Gson; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.collections.MapUtils; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; +import org.json.JSONObject; +import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.DataTypeDefinition; @@ -82,42 +90,6 @@ public class InterfacesOperationsConverter { this.propertyConvertor = propertyConvertor; } - /** - * Creates the interface_types element. - * - * @param component to work on - * @return the added element - */ - public Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { - if (component instanceof Product) { - return null; - } - final Map interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return null; - } - Map toscaInterfaceTypes = new HashMap<>(); - for (InterfaceDefinition interfaceDefinition : interfaces.values()) { - boolean isInterfaceTypeExistInGlobalType = allInterfaceTypes.stream() - .anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); - if (!isInterfaceTypeExistInGlobalType) { - ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); - toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); - final Map operations = interfaceDefinition.getOperations(); - Map toscaOperations = new HashMap<>(); - for (Map.Entry operationEntry : operations.entrySet()) { - toscaOperations.put(operationEntry.getValue().getName(), null); - } - toscaInterfaceType.setOperations(toscaOperations); - Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); - Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS.getElementName()); - interfacesAsMap.putAll(operationsMap); - toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); - } - } - return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; - } - private static Object getDefaultValue(Map inputValueMap) { Object defaultValue = null; for (Map.Entry operationEntry : inputValueMap.entrySet()) { @@ -153,11 +125,6 @@ public class InterfacesOperationsConverter { return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); } - private boolean isArtifactPresent(final OperationDataDefinition operationDataDefinition) { - return operationDataDefinition.getImplementation() != null - && StringUtils.isNotEmpty(operationDataDefinition.getImplementation().getArtifactName()); - } - private static String getInputValue(final OperationInputDefinition input) { if (null != input.getToscaFunction()) { return input.getToscaFunction().getJsonObjectValue().toString(); @@ -173,10 +140,42 @@ public class InterfacesOperationsConverter { mappedOutputValue.remove(1); mappedOutputValue.add(1, interfaceName); inputValue = gson.toJson(consumptionValue); + } else { + String finalInputValue = inputValue; + if (inputValue != null && + Arrays.stream(ToscaFunctionType.values()).anyMatch(toscaType -> finalInputValue.contains(toscaType.getName().toUpperCase()))) { + inputValue = getInputValuesAsToscaFunction(inputValue); + } } return inputValue; } + private static String getInputValuesAsToscaFunction(String inputValue) { + Gson gson = new Gson(); + Map mapOfValues = gson.fromJson(inputValue, Map.class); + for (Entry entry : mapOfValues.entrySet()) { + Object value = entry.getValue(); + if (value instanceof Map) { + Map valueMap = (Map) value; + if (valueMap.containsKey("type")) { + String type = (String) valueMap.get("type"); + Optional toscaType = ToscaFunctionType.findType(type); + if (toscaType.isPresent()) { + ObjectMapper mapper = new ObjectMapper(); + try { + String json = gson.toJson(value); + ToscaFunction toscaFunction = mapper.readValue(json, ToscaFunction.class); + entry.setValue(toscaFunction.getJsonObjectValue()); + } catch (JsonProcessingException e) { + throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); + } + } + } + } + } + return String.valueOf(new JSONObject(mapOfValues)); + } + private static String getInterfaceType(Component component, String interfaceType) { if (LOCAL_INTERFACE_TYPE.equals(interfaceType)) { return DERIVED_FROM_BASE_DEFAULT + component.getComponentMetadataDefinition().getMetadataDataDefinition().getSystemName(); @@ -207,6 +206,47 @@ public class InterfacesOperationsConverter { return objectAsMap; } + /** + * Creates the interface_types element. + * + * @param component to work on + * @return the added element + */ + public Map addInterfaceTypeElement(Component component, List allInterfaceTypes) { + if (component instanceof Product) { + return null; + } + final Map interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return null; + } + Map toscaInterfaceTypes = new HashMap<>(); + for (InterfaceDefinition interfaceDefinition : interfaces.values()) { + boolean isInterfaceTypeExistInGlobalType = allInterfaceTypes.stream() + .anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType())); + if (!isInterfaceTypeExistInGlobalType) { + ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType(); + toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE); + final Map operations = interfaceDefinition.getOperations(); + Map toscaOperations = new HashMap<>(); + for (Map.Entry operationEntry : operations.entrySet()) { + toscaOperations.put(operationEntry.getValue().getName(), null); + } + toscaInterfaceType.setOperations(toscaOperations); + Map interfacesAsMap = getObjectAsMap(toscaInterfaceType); + Map operationsMap = (Map) interfacesAsMap.remove(OPERATIONS.getElementName()); + interfacesAsMap.putAll(operationsMap); + toscaInterfaceTypes.put(getInterfaceType(component, LOCAL_INTERFACE_TYPE), interfacesAsMap); + } + } + return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null; + } + + private boolean isArtifactPresent(final OperationDataDefinition operationDataDefinition) { + return operationDataDefinition.getImplementation() != null + && StringUtils.isNotEmpty(operationDataDefinition.getImplementation().getArtifactName()); + } + /** * Adds the 'interfaces' element to the node type provided. * @@ -302,9 +342,10 @@ public class InterfacesOperationsConverter { interfaceDefinitionAsMap.putAll(operationsMap); toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefinitionAsMap); } - + private boolean operationHasAnImplementation(OperationDataDefinition operation) { - return operation.getImplementation() != null && StringUtils.isNotEmpty(operation.getImplementation().getArtifactName()) && !operation.getImplementation().getArtifactName().equals("''"); + return operation.getImplementation() != null && StringUtils.isNotEmpty(operation.getImplementation().getArtifactName()) && + !operation.getImplementation().getArtifactName().equals("''"); } private void handleInterfaceOperationImplementation(final Component component, final ComponentInstance componentInstance, -- cgit 1.2.3-korg