diff options
author | JvD_Ericsson <jeff.van.dam@est.tech> | 2023-05-30 14:57:47 +0100 |
---|---|---|
committer | JEFF VAN DAM <jeff.van.dam@est.tech> | 2023-06-01 14:10:01 +0000 |
commit | bfd727385b9c55d079a5c213b425e88b88a67b7f (patch) | |
tree | 846a85428bbeff2297aafe8c6c4d33d771a16c2a /catalog-be/src/main/java | |
parent | 57dfe38027308533391d437956e57ac5de0eddc3 (diff) |
Fix formatting error for operation input of complex type
Issue-ID: SDC-4523
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: I3dff94cee1d1dbcb26099d02505a679249a6b46e
Diffstat (limited to 'catalog-be/src/main/java')
2 files changed, 96 insertions, 44 deletions
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<String, Object> valueMap = (Map<String, Object>) value; log.debug("Creating interface operation input '{}'", inputName); Type type = new TypeToken<LinkedHashMap<String, Object>>() { }.getType(); String stringValue = gson.toJson(value, type); if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) { toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value).ifPresent(operationInput::setToscaFunction); + } else { + final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>()); + if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) { + Collection<SubPropertyToscaFunction> 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<String, Object> addInterfaceTypeElement(Component component, List<String> allInterfaceTypes) { - if (component instanceof Product) { - return null; - } - final Map<String, InterfaceDefinition> interfaces = component.getInterfaces(); - if (MapUtils.isEmpty(interfaces)) { - return null; - } - Map<String, Object> 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<String, OperationDataDefinition> operations = interfaceDefinition.getOperations(); - Map<String, Object> toscaOperations = new HashMap<>(); - for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) { - toscaOperations.put(operationEntry.getValue().getName(), null); - } - toscaInterfaceType.setOperations(toscaOperations); - Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType); - Map<String, Object> operationsMap = (Map<String, Object>) 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<String, Object> inputValueMap) { Object defaultValue = null; for (Map.Entry<String, Object> 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<String, Object> mapOfValues = gson.fromJson(inputValue, Map.class); + for (Entry<String, Object> entry : mapOfValues.entrySet()) { + Object value = entry.getValue(); + if (value instanceof Map) { + Map<String, Object> valueMap = (Map<String, Object>) value; + if (valueMap.containsKey("type")) { + String type = (String) valueMap.get("type"); + Optional<ToscaFunctionType> 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(); @@ -208,6 +207,47 @@ public class InterfacesOperationsConverter { } /** + * Creates the interface_types element. + * + * @param component to work on + * @return the added element + */ + public Map<String, Object> addInterfaceTypeElement(Component component, List<String> allInterfaceTypes) { + if (component instanceof Product) { + return null; + } + final Map<String, InterfaceDefinition> interfaces = component.getInterfaces(); + if (MapUtils.isEmpty(interfaces)) { + return null; + } + Map<String, Object> 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<String, OperationDataDefinition> operations = interfaceDefinition.getOperations(); + Map<String, Object> toscaOperations = new HashMap<>(); + for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) { + toscaOperations.put(operationEntry.getValue().getName(), null); + } + toscaInterfaceType.setOperations(toscaOperations); + Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType); + Map<String, Object> operationsMap = (Map<String, Object>) 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. * * @param component to work on @@ -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, |