diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property')
7 files changed, 142 insertions, 174 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ComplexPropertyValueMerger.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ComplexPropertyValueMerger.java deleted file mode 100644 index a68e28024c..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ComplexPropertyValueMerger.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.openecomp.sdc.be.components.merge.property; - -import java.util.List; - -public class ComplexPropertyValueMerger extends PropertyValueMerger { - - private static final ComplexPropertyValueMerger INSTANCE = new ComplexPropertyValueMerger(); - - public static PropertyValueMerger getInstance() { - return INSTANCE; - } - - @Override - Object merge(Object oldVal, Object newVal, List<String> someStrings) { - return mergeValues(oldVal, newVal, someStrings); - } -} diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/DataDefinitionsValuesMergingBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/DataDefinitionsValuesMergingBusinessLogic.java index bc2f71e2ef..faa8ebbdec 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/DataDefinitionsValuesMergingBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/DataDefinitionsValuesMergingBusinessLogic.java @@ -1,18 +1,24 @@ package org.openecomp.sdc.be.components.merge.property; -import java.util.Collections; -import java.util.List; - import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.model.InputDefinition; import org.springframework.stereotype.Component; +import java.util.Collections; +import java.util.List; + +import static org.apache.commons.collections.CollectionUtils.isEmpty; +import static org.openecomp.sdc.be.components.merge.property.PropertyInstanceMergeDataBuilder.buildDataForMerging; + @Component public class DataDefinitionsValuesMergingBusinessLogic { - @javax.annotation.Resource private PropertyDataValueMergeBusinessLogic propertyValueMergeBL; + public DataDefinitionsValuesMergingBusinessLogic(PropertyDataValueMergeBusinessLogic propertyValueMergeBL) { + this.propertyValueMergeBL = propertyValueMergeBL; + } + /** * Merge previous version data definition values into the new version data definition. * A data definition value is merged if it had a value in previous version and has no value in the current version. @@ -23,10 +29,10 @@ public class DataDefinitionsValuesMergingBusinessLogic { * @param newInputs the new version inputs */ public <T extends PropertyDataDefinition> void mergeInstanceDataDefinitions(List<T> oldInstanceDataDefinition, List<InputDefinition> oldInputs, List<T> updatedInstanceDataDefinition, List<InputDefinition> newInputs) { - if (updatedInstanceDataDefinition == null || updatedInstanceDataDefinition.isEmpty() || oldInstanceDataDefinition == null || oldInstanceDataDefinition.isEmpty()) { + if (isEmpty(updatedInstanceDataDefinition) || isEmpty(oldInstanceDataDefinition)) { return; } - List<MergePropertyData> mergePropertyData = PropertyInstanceMergeDataBuilder.getInstance().buildDataForMerging(oldInstanceDataDefinition, oldInputs, updatedInstanceDataDefinition, newInputs); + List<MergePropertyData> mergePropertyData = buildDataForMerging(oldInstanceDataDefinition, oldInputs, updatedInstanceDataDefinition, newInputs); mergePropertyData.forEach(this::mergeInstanceDefinition); } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/MergePropertyData.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/MergePropertyData.java index 63a7a1b3a0..df65c1161f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/MergePropertyData.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/MergePropertyData.java @@ -1,10 +1,10 @@ package org.openecomp.sdc.be.components.merge.property; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; + import java.util.ArrayList; import java.util.List; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; - /** * A POJO which represents an instance property data definition (a {@link org.openecomp.sdc.be.model.ComponentInstanceProperty} or {@link org.openecomp.sdc.be.model.ComponentInstanceInput}) * that its value needs to be merged during an upgrade of a VSP. diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java index 7356b446a4..df7b877364 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java @@ -7,18 +7,14 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; -import javax.annotation.Resource; - import org.openecomp.sdc.be.dao.titan.TitanOperationStatus; 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.model.tosca.ToscaPropertyType; import org.openecomp.sdc.be.tosca.PropertyConvertor; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.stereotype.Component; import com.google.gson.Gson; @@ -28,72 +24,62 @@ import fj.data.Either; @Component public class PropertyDataValueMergeBusinessLogic { - private static final Logger LOGGER = LoggerFactory.getLogger(PropertyDataValueMergeBusinessLogic.class); + private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class); + private final PropertyValueMerger propertyValueMerger; + private final ApplicationDataTypeCache dataTypeCache; + private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance(); - - private PropertyValueMerger complexPropertyValueMerger = ComplexPropertyValueMerger.getInstance(); - - private PropertyValueMerger scalarPropertyValueMerger = ScalarPropertyValueMerger.getInstance(); - - @Resource - private ApplicationDataTypeCache dataTypeCache; - private final Gson gson = new Gson(); + + public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) { + this.propertyValueMerger = propertyValueMerger; + this.dataTypeCache = dataTypeCache; + } + /** * * @param oldProp the old property to merge value from * @param newProp the new property to merge value into - * @param getInputNamesToMerge inputs names which their corresponding get_input values are allowed to be merged */ public void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, List<String> getInputNamesToMerge) { Either<Map<String, DataTypeDefinition>, TitanOperationStatus> dataTypesEither = dataTypeCache.getAll(); if (dataTypesEither.isRight()) { LOGGER.debug("failed to fetch data types, skip merging of previous property values. status: {}", dataTypesEither.right().value()); } - mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge); - mergeComplexPropertyGetInputsValues(oldProp, newProp); + else { + mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), 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); - PropertyValueMerger propertyValueMerger = getPropertyValueMerger(newProp); if(oldValAsObject != null){ - Object mergedValue = propertyValueMerger.mergeValues(oldValAsObject, newValAsObject, getInputNamesToMerge); + Object mergedValue = propertyValueMerger.merge(oldValAsObject, newValAsObject, getInputNamesToMerge, newProp.getType(), newProp.getSchemaType(), dataTypes); newProp.setValue(convertPropertyValueObjectToString(mergedValue)); + + mergePropertyGetInputsValues(oldProp, newProp); } + } - - private PropertyValueMerger getPropertyValueMerger(PropertyDataDefinition newProp) { - if (ToscaPropertyType.isPrimitiveType(newProp.getType()) || ToscaPropertyType.isPrimitiveType(newProp.getSchemaType())) { - return scalarPropertyValueMerger; - } - return complexPropertyValueMerger; - } - + private String convertPropertyValueObjectToString(Object mergedValue) { - if (isEmptyValue(mergedValue)) { + if (PropertyValueMerger.isEmptyValue(mergedValue)) { return null; } 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); + String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue(); + String propertyType = propertyDataDefinition.getType(); + String innerType = propertyDataDefinition.getSchemaType(); + return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true); } - private boolean isEmptyValue(Object val) { - return val == null || - val instanceof Map && ((Map) val).isEmpty() || - val instanceof List && ((List) val).isEmpty(); - } - - private void mergeComplexPropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) { + protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) { if (!oldProp.isGetInputProperty()) { return; } @@ -117,14 +103,4 @@ public class PropertyDataValueMergeBusinessLogic { String getInputEntry = "\"%s\":\"%s\""; return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName)); } - - - - - - - - - - } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyInstanceMergeDataBuilder.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyInstanceMergeDataBuilder.java index 09f26a39ee..7f9bf1c370 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyInstanceMergeDataBuilder.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyInstanceMergeDataBuilder.java @@ -1,27 +1,21 @@ package org.openecomp.sdc.be.components.merge.property; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - import org.openecomp.sdc.be.dao.utils.MapUtil; import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.model.InputDefinition; -public class PropertyInstanceMergeDataBuilder { +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; - private final static PropertyInstanceMergeDataBuilder INSTANCE = new PropertyInstanceMergeDataBuilder(); +class PropertyInstanceMergeDataBuilder { private PropertyInstanceMergeDataBuilder() { } - public static PropertyInstanceMergeDataBuilder getInstance() { - return INSTANCE; - } - - public <T extends PropertyDataDefinition> List<MergePropertyData> buildDataForMerging(List<T> oldProps, + static <T extends PropertyDataDefinition> List<MergePropertyData> buildDataForMerging(List<T> oldProps, List<InputDefinition> oldInputs, List<T> newProps, List<InputDefinition> newInputs) { @@ -34,7 +28,7 @@ public class PropertyInstanceMergeDataBuilder { } - private <T extends PropertyDataDefinition> List<MergePropertyData> buildMergeData(Map<String, T> oldPropsByName, Map<String, InputDefinition> oldInputsByName, Map<String, T> newPropsByName, Map<String, InputDefinition> newInputsByName) { + private static <T extends PropertyDataDefinition> List<MergePropertyData> buildMergeData(Map<String, T> oldPropsByName, Map<String, InputDefinition> oldInputsByName, Map<String, T> newPropsByName, Map<String, InputDefinition> newInputsByName) { List<MergePropertyData> mergeData = new ArrayList<>(); newPropsByName.forEach((name, prop) -> { if (oldPropsByName.containsKey(name)) { @@ -44,7 +38,7 @@ public class PropertyInstanceMergeDataBuilder { return mergeData; } - private MergePropertyData buildMergePropertyData(PropertyDataDefinition oldProp, + private static MergePropertyData buildMergePropertyData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName, PropertyDataDefinition newProp, Map<String, InputDefinition> newInputsByName) { @@ -58,18 +52,18 @@ public class PropertyInstanceMergeDataBuilder { return mergePropertyData; } - private void setGetInputData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName, Map<String, InputDefinition> newInputsByName, MergePropertyData mergePropertyData) { + private static void setGetInputData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName, Map<String, InputDefinition> newInputsByName, MergePropertyData mergePropertyData) { List<String> oldDeclaredByUserInputNames = getOldDeclaredInputsByUser(oldProp.getGetInputValues(), oldInputsByName); List<String> oldGetInputNamesWhichExistInNewVersion = getOldGetInputNamesWhichExistInNewVersion(oldProp.getGetInputValues(), newInputsByName); mergePropertyData.addAddGetInputNamesToMerge(oldDeclaredByUserInputNames); mergePropertyData.addAddGetInputNamesToMerge(oldGetInputNamesWhichExistInNewVersion); } - private List<String> getOldGetInputNamesWhichExistInNewVersion(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> newInputsByName) { + private static List<String> getOldGetInputNamesWhichExistInNewVersion(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> newInputsByName) { return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).filter(newInputsByName::containsKey).collect(Collectors.toList()); } - private List<String> getOldDeclaredInputsByUser(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> oldInputsByName) { + private static List<String> getOldDeclaredInputsByUser(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> oldInputsByName) { return getInputValues.stream().map(GetInputValueDataDefinition::getInputName) .map(oldInputsByName::get) .filter(oldInput -> oldInput.getInstanceUniqueId() != null) diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyValueMerger.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyValueMerger.java index 88601bb521..93d39cf2ab 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyValueMerger.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyValueMerger.java @@ -3,116 +3,154 @@ package org.openecomp.sdc.be.components.merge.property; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.lang.StringUtils; -import org.openecomp.sdc.be.components.impl.ImportUtils; - -public abstract class PropertyValueMerger { - - abstract Object merge(Object oldVal, Object newVal, List<String> someStrings); - +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.be.model.tosca.ToscaPropertyType; +import org.openecomp.sdc.be.utils.TypeUtils; +import org.springframework.stereotype.Component; + +@Component +public class PropertyValueMerger { + @SuppressWarnings("unchecked") /** * merges property value oldVal into property value newVal recursively * @param oldVal - cannot be {@code Null} */ - protected Object mergeValues(Object oldVal, Object newVal, List<String> getInputNamesToMerge) { + protected Object merge(Object oldVal, Object newVal, List<String> inputNamesToMerge, String type, String innerType, Map<String, DataTypeDefinition> dataTypes) { if (isEmptyValue(newVal)) { - return removeUnwantedGetInputValues(oldVal, getInputNamesToMerge); + return removeUnwantedGetInputValues(oldVal, inputNamesToMerge); } if (isMapTypeValues(oldVal, newVal)) { - return mergeMapValue((Map<String, Object>) oldVal, (Map<String, Object>) newVal, getInputNamesToMerge); + return mergeMapValue((Map<String, Object>) oldVal, (Map<String, Object>) newVal, inputNamesToMerge, type, innerType, dataTypes); } if (isListTypeValues(oldVal, newVal)) { - return mergeListValue((List<Object>) oldVal, (List<Object>) newVal, getInputNamesToMerge); + return mergeListValue((List<Object>) oldVal, (List<Object>) newVal, inputNamesToMerge, innerType, dataTypes); } if (isSameTypeValues(oldVal, newVal)) { return mergeScalarValue(oldVal, newVal); } return newVal; - } - - private Map<String, Object> mergeMapValue(Map<String, Object> oldValMap, Map<String, Object> newValMap, List<String> getInputNamesToMerge) { - mergeEntriesExistInNewValue(oldValMap, newValMap, getInputNamesToMerge);//continue the recursion - setOldEntriesNotExistInNewValue(oldValMap, newValMap, getInputNamesToMerge); + + private Map<String, Object> mergeMapValue(Map<String, Object> oldValMap, Map<String, Object> newValMap, List<String> inputNamesToMerge, String type, String innertType, Map<String, DataTypeDefinition> dataTypes) { + mergeEntriesExistInOldValue(oldValMap, newValMap, inputNamesToMerge, type, innertType, dataTypes);//continue the recursion + if (type != null && !type.equals("map")) { + setOldEntriesNotExistInNewValue(oldValMap, newValMap, inputNamesToMerge); + } + return newValMap; } - private void mergeEntriesExistInNewValue(Map<String, Object> oldValMap, Map<String, Object> newValMap, List<String> getInputNamesToMerge) { + private void mergeEntriesExistInOldValue(Map<String, Object> oldValMap, Map<String, Object> newValMap, List<String> inputNamesToMerge, String type, String innerType, Map<String, DataTypeDefinition> dataTypes) { for (Map.Entry<String, Object> newValEntry : newValMap.entrySet()) { Object oldVal = oldValMap.get(newValEntry.getKey()); if (oldVal != null) { - newValMap.put(newValEntry.getKey(), merge(oldVal, newValEntry.getValue(), getInputNamesToMerge)); + ImmutablePair<String, String> types = getTypeAndInnerTypePair(newValEntry.getKey(), type, innerType, dataTypes); + newValMap.put(newValEntry.getKey(), merge(oldVal, newValEntry.getValue(), inputNamesToMerge, types.getLeft(), types.getRight(), dataTypes)); } } } - + private void setOldEntriesNotExistInNewValue(Map<String, Object> oldVal, Map<String, Object> newVal, List<String> getInputNamesToMerge) { for (Map.Entry<String, Object> oldValEntry : oldVal.entrySet()) { - if (!isGetInputEntry(oldValEntry) || isGetInputToMerge(getInputNamesToMerge, oldValEntry)) { + if (!isInputEntry(oldValEntry) || isInputToMerge(getInputNamesToMerge, oldValEntry)) { Object oldValObj = oldValEntry.getValue(); newVal.computeIfAbsent(oldValEntry.getKey(), key -> removeUnwantedGetInputValues(oldValObj, getInputNamesToMerge)); } } } - - private List<Object> mergeListValue(List<Object> oldVal, List<Object> newVal, List<String> getInputNamesToMerge) { - List<Object> mergedList = mergeLists(oldVal, newVal, getInputNamesToMerge); - copyRestOfBiggerList(oldVal, newVal, getInputNamesToMerge, mergedList); - return mergedList; + + private ImmutablePair<String, String> getTypeAndInnerTypePair(String propName, String type, String innerType, Map<String, DataTypeDefinition> dataTypes) { + if (type == null || (ToscaPropertyType.isScalarType(type) && ToscaPropertyType.isScalarType(innerType))) { + return ImmutablePair.of(innerType, null); + } + + String newInnerType = null; + DataTypeDefinition innerTypeDef = dataTypes.get(type); + if (innerTypeDef != null) { + List<PropertyDefinition> properties = innerTypeDef.getProperties(); + if (properties!= null) { + Optional<PropertyDefinition> optionalProperty = findProperty(properties, propName); + + innerType = optionalProperty.map(PropertyDefinition::getType) + .orElse(innerType); + + newInnerType = optionalProperty.map(PropertyDefinition::getSchemaType) + .orElse(null); + } + } + + return ImmutablePair.of(innerType, newInnerType); + } + + private Optional<PropertyDefinition> findProperty(List<PropertyDefinition> properties, String propName) { + return properties.stream() + .filter(p -> propName.equals(p.getName())) + .findFirst(); } - private void copyRestOfBiggerList(List<Object> oldVal, List<Object> newVal, List<String> getInputNamesToMerge, List<Object> mergedList) { + private List<Object> mergeListValue(List<Object> oldVal, List<Object> newVal, List<String> inputNamesToMerge, String innerType, Map<String, DataTypeDefinition> dataTypes) { + List<Object> mergedList = newVal; + if (oldVal.size() == newVal.size()) { - return; - } - int maxListSize = Math.max(oldVal.size(), newVal.size()); - List<Object> greaterList = newVal.size() == maxListSize ? newVal : oldVal; - for (int i = mergedList.size(); i < maxListSize; i ++) { - Object listVal = greaterList.get(i); - Object listValToMerge = greaterList == oldVal ? removeUnwantedGetInputValues(listVal, getInputNamesToMerge) : listVal; - mergedList.add(listValToMerge); + mergedList = mergeLists(oldVal, newVal, inputNamesToMerge, innerType, dataTypes); } + + return mergedList; } + - private List<Object> mergeLists(List<Object> oldVal, List<Object> newVal, List<String> getInputNamesToMerge) { + private List<Object> mergeLists(List<Object> oldVal, List<Object> newVal, List<String> inputNamesToMerge, String innerType, Map<String, DataTypeDefinition> dataTypes) { int minListSize = Math.min(oldVal.size(), newVal.size()); List<Object> mergedList = new ArrayList<>(); for (int i = 0; i < minListSize; i++) { - Object mergedVal = merge(oldVal.get(i), newVal.get(i), getInputNamesToMerge); + Object mergedVal = merge(oldVal.get(i), newVal.get(i), inputNamesToMerge, innerType, null, dataTypes); mergedList.add(mergedVal); } return mergedList; } - - Object mergeScalarValue(Object oldVal, Object newVal) { + + + private Object mergeScalarValue(Object oldVal, Object newVal) { return isEmptyValue(newVal) ? oldVal : newVal; } + + static boolean isEmptyValue(Object val) { + return val == null || + val instanceof String && StringUtils.isEmpty((String)val) || + val instanceof Map && ((Map<?,?>) val).isEmpty() || + val instanceof List && ((List<?>) val).isEmpty(); + } + @SuppressWarnings("unchecked") - Object removeUnwantedGetInputValues(Object val, List<String> getInputNamesToMerge) { + Object removeUnwantedGetInputValues(Object val, List<String> inputNamesToMerge) { if (val instanceof Map) { - return removeUnwantedGetInputValues((Map<String, Object>) val, getInputNamesToMerge); + return removeUnwantedGetInputValues((Map<String, Object>) val, inputNamesToMerge); } if (val instanceof List) { - return removeUnwantedGetInputValues((List<Object>)val, getInputNamesToMerge); + return removeUnwantedGetInputValues((List<Object>)val, inputNamesToMerge); } return val; } - private List<Object> removeUnwantedGetInputValues(List<Object> listVal, List<String> getInputNamesToMerge) { - return listVal.stream().map(val -> removeUnwantedGetInputValues(val, getInputNamesToMerge)).collect(Collectors.toList()); + private List<Object> removeUnwantedGetInputValues(List<Object> listVal, List<String> inputNamesToMerge) { + return listVal.stream().map(val -> removeUnwantedGetInputValues(val, inputNamesToMerge)).collect(Collectors.toList()); } - private Map<String, Object> removeUnwantedGetInputValues(Map<String, Object> val, List<String> getInputNamesToMerge) { - return val.entrySet().stream().filter(entry -> !isGetInputEntry(entry) || isGetInputToMerge(getInputNamesToMerge, entry)) - .collect(Collectors.toMap(Map.Entry::getKey, entry -> removeUnwantedGetInputValues(entry.getValue(), getInputNamesToMerge))); + private Map<String, Object> removeUnwantedGetInputValues(Map<String, Object> val, List<String> inputNamesToMerge) { + return val.entrySet().stream().filter(entry -> !isInputEntry(entry) || isInputToMerge(inputNamesToMerge, entry)) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> removeUnwantedGetInputValues(entry.getValue(), inputNamesToMerge))); } - private boolean isGetInputToMerge(List<String> getInputNamesToMerge, Map.Entry<String, Object> entry) { - return getInputNamesToMerge.contains(retrieveGetInputInputName(entry.getValue())); + private boolean isInputToMerge(List<String> inputNamesToMerge, Map.Entry<String, Object> entry) { + return inputNamesToMerge.contains(retrieveInputName(entry.getValue())); } private boolean isMapTypeValues(Object oldVal, Object newVal) { @@ -127,22 +165,13 @@ public abstract class PropertyValueMerger { return oldVal.getClass().equals(newVal.getClass()); } - private String retrieveGetInputInputName(Object getInputValue) { - return getInputValue instanceof List ? (String)((List) getInputValue).get(0) : (String)getInputValue; - } - - private boolean isGetInputEntry(Map.Entry<String, Object> oldValEntry) { - return oldValEntry.getKey().equals(ImportUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); + private String retrieveInputName(Object inputValue) { + return inputValue instanceof List ? (String)((List<?>) inputValue).get(0) : (String)inputValue; } - private boolean isEmptyValue(Object val) { - return val == null || - val instanceof String && StringUtils.isEmpty((String)val) || - val instanceof Map && ((Map) val).isEmpty() || - val instanceof List && ((List) val).isEmpty(); - - + protected boolean isInputEntry(Map.Entry<String, Object> oldValEntry) { + return oldValEntry.getKey().equals(TypeUtils.ToscaTagNamesEnum.GET_INPUT.getElementName()); } - - + + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ScalarPropertyValueMerger.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ScalarPropertyValueMerger.java deleted file mode 100644 index 39381e7322..0000000000 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/ScalarPropertyValueMerger.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.openecomp.sdc.be.components.merge.property; - -import java.util.List; - -import org.springframework.stereotype.Component; - -@Component("scalar-prop-value-merger") -public class ScalarPropertyValueMerger extends PropertyValueMerger { - - private final static ScalarPropertyValueMerger INSTANCE = new ScalarPropertyValueMerger(); - - public static PropertyValueMerger getInstance() { - return INSTANCE; - } - - @Override - Object merge(Object oldVal, Object newVal, List<String> getInputNamesToMerge) { - return mergeScalarValue(removeUnwantedGetInputValues(oldVal, getInputNamesToMerge), newVal); - } -} |