Fix 'Changing VFC version on template wipes previously assigned property values based...
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / PropertyDataValueMergeBusinessLogic.java
index c02eb28..7562dfe 100644 (file)
@@ -7,9 +7,9 @@
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-
 package org.openecomp.sdc.be.components.merge.property;
 
 import com.google.gson.Gson;
 import fj.data.Either;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
 import org.openecomp.sdc.be.model.DataTypeDefinition;
 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
-import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
 import org.openecomp.sdc.be.tosca.PropertyConvertor;
 import org.openecomp.sdc.common.log.wrappers.Logger;
+import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.stream.Collectors;
-
 @Component
 public class PropertyDataValueMergeBusinessLogic {
 
     private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class);
-
     private final PropertyValueMerger propertyValueMerger;
-    private final ApplicationDataTypeCache dataTypeCache;
-    
-    private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance();
+    private final ApplicationDataTypeCache applicationDataTypeCache;
+    private final PropertyConvertor propertyConvertor;
     private final Gson gson = new Gson();
 
-    
-    public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) {
+    @Autowired
+    public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache applicationDataTypeCache,
+                                               PropertyConvertor propertyConvertor) {
         this.propertyValueMerger = propertyValueMerger;
-        this.dataTypeCache = dataTypeCache;
+        this.applicationDataTypeCache = applicationDataTypeCache;
+        this.propertyConvertor = propertyConvertor;
     }
 
     /**
-     *
      * @param oldProp the old property to merge value from
      * @param newProp the new property to merge value into
      */
     public void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, List<String> getInputNamesToMerge) {
-        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
+        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = applicationDataTypeCache.getAll(oldProp.getModel());
         if (dataTypesEither.isRight()) {
             LOGGER.debug("failed to fetch data types, skip merging of previous property values. status: {}", dataTypesEither.right().value());
-        }
-        else {
+        } else {
             mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
         }
     }
-    
-    private void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, Map<String, DataTypeDefinition> dataTypes, List<String> getInputNamesToMerge) {
+
+    private void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, Map<String, DataTypeDefinition> dataTypes,
+                                    List<String> getInputNamesToMerge) {
         Object oldValAsObject = convertPropertyStrValueToObject(oldProp, dataTypes);
         Object newValAsObject = convertPropertyStrValueToObject(newProp, dataTypes);
-        if(oldValAsObject != null){
-            Object mergedValue =  propertyValueMerger.merge(oldValAsObject, newValAsObject, getInputNamesToMerge, newProp.getType(), newProp.getSchemaType(), dataTypes);
+        if (oldValAsObject != null) {
+            Object mergedValue = propertyValueMerger
+                .merge(oldValAsObject, newValAsObject, getInputNamesToMerge, newProp.getType(), newProp.getSchemaType(), dataTypes);
             newProp.setValue(convertPropertyValueObjectToString(mergedValue));
-            
             mergePropertyGetInputsValues(oldProp, newProp);
         }
-        
     }
-    
+
     private String convertPropertyValueObjectToString(Object mergedValue) {
         if (PropertyValueMerger.isEmptyValue(mergedValue)) {
             return null;
         }
-        return mergedValue instanceof String? mergedValue.toString() : gson.toJson(mergedValue);
+        return mergedValue instanceof String ? mergedValue.toString() : gson.toJson(mergedValue);
     }
 
     private Object convertPropertyStrValueToObject(PropertyDataDefinition propertyDataDefinition, Map<String, DataTypeDefinition> dataTypes) {
-        String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue();
-        String propertyType = propertyDataDefinition.getType();
-        String innerType = propertyDataDefinition.getSchemaType();
-        return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true);
+        final String propValue = propertyDataDefinition.getValue() == null ? "" : propertyDataDefinition.getValue();
+        return propertyConvertor.convertToToscaObject(propertyDataDefinition, propValue, dataTypes, true);
     }
 
     protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
@@ -111,14 +106,13 @@ public class PropertyDataValueMergeBusinessLogic {
         List<GetInputValueDataDefinition> oldGetInputValues = oldProp.getGetInputValues();
         List<GetInputValueDataDefinition> newGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(Collections.emptyList());
         List<String> newGetInputNames = newGetInputValues.stream().map(GetInputValueDataDefinition::getInputName).collect(Collectors.toList());
-        return oldGetInputValues.stream()
-                .filter(getInput -> !newGetInputNames.contains(getInput.getInputName()))
-                .filter(getInput -> isValueContainsGetInput(getInput.getInputName(), newProp.getValue()))
-                .collect(Collectors.toList());
+        return oldGetInputValues.stream().filter(getInput -> !newGetInputNames.contains(getInput.getInputName()))
+            .filter(getInput -> isValueContainsGetInput(getInput.getInputName(), newProp.getValue())).collect(Collectors.toList());
     }
 
     private boolean isValueContainsGetInput(String inputName, String value) {
         String getInputEntry = "\"%s\":\"%s\"";
         return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName));
     }
+
 }