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 java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Optional;
28 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.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;
40 import com.google.gson.Gson;
42 import fj.data.Either;
45 public class PropertyDataValueMergeBusinessLogic {
47 private static final Logger LOGGER = Logger.getLogger(PropertyDataValueMergeBusinessLogic.class);
49 private final PropertyValueMerger propertyValueMerger;
50 private final ApplicationDataTypeCache dataTypeCache;
52 private final PropertyConvertor propertyConvertor = PropertyConvertor.getInstance();
53 private final Gson gson = new Gson();
56 public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache dataTypeCache) {
57 this.propertyValueMerger = propertyValueMerger;
58 this.dataTypeCache = dataTypeCache;
63 * @param oldProp the old property to merge value from
64 * @param newProp the new property to merge value into
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());
72 mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
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));
83 mergePropertyGetInputsValues(oldProp, newProp);
88 private String convertPropertyValueObjectToString(Object mergedValue) {
89 if (PropertyValueMerger.isEmptyValue(mergedValue)) {
92 return mergedValue instanceof String? mergedValue.toString() : gson.toJson(mergedValue);
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);
102 protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
103 if (!oldProp.isGetInputProperty()) {
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);
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());
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));