Added oparent to sdc main
[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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29
30 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
31 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
33 import org.openecomp.sdc.be.model.DataTypeDefinition;
34 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
35 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
36 import org.openecomp.sdc.be.tosca.PropertyConvertor;
37 import org.openecomp.sdc.common.log.wrappers.Logger;
38 import org.springframework.stereotype.Component;
39
40 import com.google.gson.Gson;
41
42 import fj.data.Either;
43
44 @Component
45 public class PropertyDataValueMergeBusinessLogic {
46
47     private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class);
48
49     private final PropertyValueMerger propertyValueMerger;
50     private final ApplicationDataTypeCache dataTypeCache;
51     
52     private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance();
53     private final Gson gson = new Gson();
54
55     
56     public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) {
57         this.propertyValueMerger = propertyValueMerger;
58         this.dataTypeCache = dataTypeCache;
59     }
60
61     /**
62      *
63      * @param oldProp the old property to merge value from
64      * @param newProp the new property to merge value into
65      */
66     public void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, List<String> getInputNamesToMerge) {
67         Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> dataTypesEither = dataTypeCache.getAll();
68         if (dataTypesEither.isRight()) {
69             LOGGER.debug("failed to fetch data types, skip merging of previous property values. status: {}", dataTypesEither.right().value());
70         }
71         else {
72             mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
73         }
74     }
75     
76     private void mergePropertyValue(PropertyDataDefinition oldProp, PropertyDataDefinition newProp, Map<String, DataTypeDefinition> dataTypes, List<String> getInputNamesToMerge) {
77         Object oldValAsObject = convertPropertyStrValueToObject(oldProp, dataTypes);
78         Object newValAsObject = convertPropertyStrValueToObject(newProp, dataTypes);
79         if(oldValAsObject != null){
80             Object mergedValue =  propertyValueMerger.merge(oldValAsObject, newValAsObject, getInputNamesToMerge, newProp.getType(), newProp.getSchemaType(), dataTypes);
81             newProp.setValue(convertPropertyValueObjectToString(mergedValue));
82             
83             mergePropertyGetInputsValues(oldProp, newProp);
84         }
85         
86     }
87     
88     private String convertPropertyValueObjectToString(Object mergedValue) {
89         if (PropertyValueMerger.isEmptyValue(mergedValue)) {
90             return null;
91         }
92         return mergedValue instanceof String? mergedValue.toString() : gson.toJson(mergedValue);
93     }
94
95     private Object convertPropertyStrValueToObject(PropertyDataDefinition propertyDataDefinition, Map<String, DataTypeDefinition> dataTypes) {
96         String propValue = propertyDataDefinition.getValue() == null ? "": propertyDataDefinition.getValue();
97         String propertyType = propertyDataDefinition.getType();
98         String innerType = propertyDataDefinition.getSchemaType();
99         return propertyConvertor.convertToToscaObject(propertyType, propValue, innerType, dataTypes, true);
100     }
101
102     protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
103         if (!oldProp.isGetInputProperty()) {
104             return;
105         }
106         List<GetInputValueDataDefinition> getInputsToMerge = findOldGetInputValuesToMerge(oldProp, newProp);
107         List<GetInputValueDataDefinition> newPropGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(new ArrayList<>());
108         newPropGetInputValues.addAll(getInputsToMerge);
109         newProp.setGetInputValues(newPropGetInputValues);
110     }
111
112     private List<GetInputValueDataDefinition> findOldGetInputValuesToMerge(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
113         List<GetInputValueDataDefinition> oldGetInputValues = oldProp.getGetInputValues();
114         List<GetInputValueDataDefinition> newGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(Collections.emptyList());
115         List<String> newGetInputNames = newGetInputValues.stream().map(GetInputValueDataDefinition::getInputName).collect(Collectors.toList());
116         return oldGetInputValues.stream()
117                 .filter(getInput -> !newGetInputNames.contains(getInput.getInputName()))
118                 .filter(getInput -> isValueContainsGetInput(getInput.getInputName(), newProp.getValue()))
119                 .collect(Collectors.toList());
120     }
121
122     private boolean isValueContainsGetInput(String inputName, String value) {
123         String getInputEntry = "\"%s\":\"%s\"";
124         return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName));
125     }
126 }