2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.property;
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;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
43 public class PropertyDataValueMergeBusinessLogic {
45 private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class);
47 private final PropertyValueMerger propertyValueMerger;
48 private final ApplicationDataTypeCache dataTypeCache;
50 private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance();
51 private final Gson gson = new Gson();
54 public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) {
55 this.propertyValueMerger = propertyValueMerger;
56 this.dataTypeCache = dataTypeCache;
61 * @param oldProp the old property to merge value from
62 * @param newProp the new property to merge value into
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());
70 mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
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));
81 mergePropertyGetInputsValues(oldProp, newProp);
86 private String convertPropertyValueObjectToString(Object mergedValue) {
87 if (PropertyValueMerger.isEmptyValue(mergedValue)) {
90 return mergedValue instanceof String? mergedValue.toString() : gson.toJson(mergedValue);
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);
100 protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
101 if (!oldProp.isGetInputProperty()) {
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);
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());
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));