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