Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / PropertyDataValueMergeBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.property;
22
23 import com.google.gson.Gson;
24 import fj.data.Either;
25 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
26 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.model.DataTypeDefinition;
29 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
30 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
31 import org.openecomp.sdc.be.tosca.PropertyConvertor;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.springframework.stereotype.Component;
34
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41
42 @Component
43 public class PropertyDataValueMergeBusinessLogic {
44
45     private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class);
46
47     private final PropertyValueMerger propertyValueMerger;
48     private final ApplicationDataTypeCache dataTypeCache;
49     
50     private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance();
51     private final Gson gson = new Gson();
52
53     
54     public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) {
55         this.propertyValueMerger = propertyValueMerger;
56         this.dataTypeCache = dataTypeCache;
57     }
58
59     /**
60      *
61      * @param oldProp the old property to merge value from
62      * @param newProp the new property to merge value into
63      */
64     public void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, List<String> getInputNamesToMerge) {
65         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
66         if (dataTypesEither.isRight()) {
67             LOGGER.debug("failed to fetch data types, skip merging of previous property values. status: {}", dataTypesEither.right().value());
68         }
69         else {
70             mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
71         }
72     }
73     
74     private void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, Map<String, DataTypeDefinition> dataTypes, List<String> getInputNamesToMerge) {
75         Object oldValAsObject = convertPropertyStrValueToObject(oldProp, dataTypes);
76         Object newValAsObject = convertPropertyStrValueToObject(newProp, dataTypes);
77         if(oldValAsObject != null){
78             Object mergedValue =  propertyValueMerger.merge(oldValAsObject, newValAsObject, getInputNamesToMerge, newProp.getType(), newProp.getSchemaType(), dataTypes);
79             newProp.setValue(convertPropertyValueObjectToString(mergedValue));
80             
81             mergePropertyGetInputsValues(oldProp, newProp);
82         }
83         
84     }
85     
86     private String convertPropertyValueObjectToString(Object mergedValue) {
87         if (PropertyValueMerger.isEmptyValue(mergedValue)) {
88             return null;
89         }
90         return mergedValue instanceof String? mergedValue.toString() : gson.toJson(mergedValue);
91     }
92
93     private Object convertPropertyStrValueToObject(PropertyDataDefinition propertyDataDefinition, Map<String, DataTypeDefinition> dataTypes) {
94         String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue();
95         String propertyType = propertyDataDefinition.getType();
96         String innerType = propertyDataDefinition.getSchemaType();
97         return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true);
98     }
99
100     protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
101         if (!oldProp.isGetInputProperty()) {
102             return;
103         }
104         List<GetInputValueDataDefinition> getInputsToMerge = findOldGetInputValuesToMerge(oldProp, newProp);
105         List<GetInputValueDataDefinition> newPropGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(new ArrayList<>());
106         newPropGetInputValues.addAll(getInputsToMerge);
107         newProp.setGetInputValues(newPropGetInputValues);
108     }
109
110     private List<GetInputValueDataDefinition> findOldGetInputValuesToMerge(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
111         List<GetInputValueDataDefinition> oldGetInputValues = oldProp.getGetInputValues();
112         List<GetInputValueDataDefinition> newGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(Collections.emptyList());
113         List<String> newGetInputNames = newGetInputValues.stream().map(GetInputValueDataDefinition::getInputName).collect(Collectors.toList());
114         return oldGetInputValues.stream()
115                 .filter(getInput -> !newGetInputNames.contains(getInput.getInputName()))
116                 .filter(getInput -> isValueContainsGetInput(getInput.getInputName(), newProp.getValue()))
117                 .collect(Collectors.toList());
118     }
119
120     private boolean isValueContainsGetInput(String inputName, String value) {
121         String getInputEntry = "\"%s\":\"%s\"";
122         return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName));
123     }
124 }