Fix formatting error for operation input of complex type
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / InterfacesOperationsConverter.java
index 8f5f271..e56472a 100644 (file)
@@ -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();
@@ -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<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.
      *
@@ -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,