aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/merge/property/PropertyDataValueMergeBusinessLogic.java
blob: 6b083596c7e2c3596f6dcb9c530cfabcc008a76f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package org.openecomp.sdc.be.components.merge.property;

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 javax.annotation.Resource;

import org.openecomp.sdc.be.components.impl.ImportUtils;
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.springframework.stereotype.Component;

import com.google.gson.Gson;

import fj.data.Either;

@Component
public class PropertyDataValueMergeBusinessLogic {

    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyDataValueMergeBusinessLogic.class);

    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();

    /**
     *
     * @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);
    }

    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);
            newProp.setValue(convertPropertyValueObjectToString(mergedValue));
        }
    }

    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)) {
            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);
    }


    @SuppressWarnings("unchecked")
    private Object removeUnwantedGetInputValues(Object val, List<String> getInputNamesToMerge) {
        if (val instanceof  Map) {
            return removeUnwantedGetInputValues((Map<String, Object>) val, getInputNamesToMerge);
        }
        if (val instanceof List) {
            return removeUnwantedGetInputValues((List<Object>)val, getInputNamesToMerge);
        }
        return val;
    }

    private List<Object> removeUnwantedGetInputValues(List<Object> listVal, List<String> getInputNamesToMerge) {
        return listVal.stream().map(val -> removeUnwantedGetInputValues(val, getInputNamesToMerge)).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 boolean isGetInputToMerge(List<String> getInputNamesToMerge, Map.Entry<String, Object> entry) {
        return getInputNamesToMerge.contains(retrieveGetInputInputName(entry.getValue()));
    }

    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 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) {
        if (!oldProp.isGetInputProperty()) {
            return;
        }
        List<GetInputValueDataDefinition> getInputsToMerge = findOldGetInputValuesToMerge(oldProp, newProp);
        List<GetInputValueDataDefinition> newPropGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(new ArrayList<>());
        newPropGetInputValues.addAll(getInputsToMerge);
        newProp.setGetInputValues(newPropGetInputValues);
    }

    private List<GetInputValueDataDefinition> findOldGetInputValuesToMerge(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
        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());
    }

    private boolean isValueContainsGetInput(String inputName, String value) {
        String getInputEntry = "\"%s\":\"%s\"";
        return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName));
    }










}