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