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=========================================================
20 package org.openecomp.sdc.be.components.merge.property;
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;
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;
42 public class PropertyDataValueMergeBusinessLogic {
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();
51 public PropertyDataValueMergeBusinessLogic(PropertyValueMerger propertyValueMerger, ApplicationDataTypeCache applicationDataTypeCache,
52 PropertyConvertor propertyConvertor) {
53 this.propertyValueMerger = propertyValueMerger;
54 this.applicationDataTypeCache = applicationDataTypeCache;
55 this.propertyConvertor = propertyConvertor;
59 * @param oldProp the old property to merge value from
60 * @param newProp the new property to merge value into
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());
67 mergePropertyValue(oldProp, newProp, dataTypesEither.left().value(), getInputNamesToMerge);
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);
83 private String convertPropertyValueObjectToString(Object mergedValue) {
84 if (PropertyValueMerger.isEmptyValue(mergedValue)) {
87 return mergedValue instanceof String ? mergedValue.toString() : gson.toJson(mergedValue);
90 private Object convertPropertyStrValueToObject(PropertyDataDefinition propertyDataDefinition, Map<String, DataTypeDefinition> dataTypes) {
91 final String propValue = propertyDataDefinition.getValue() == null ? "" : propertyDataDefinition.getValue();
92 return propertyConvertor.convertToToscaObject(propertyDataDefinition, propValue, dataTypes, true);
95 protected void mergePropertyGetInputsValues(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
96 if (!oldProp.isGetInputProperty()) {
99 List<GetInputValueDataDefinition> getInputsToMerge = findOldGetInputValuesToMerge(oldProp, newProp);
100 List<GetInputValueDataDefinition> newPropGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(new ArrayList<>());
101 newPropGetInputValues.addAll(getInputsToMerge);
102 newProp.setGetInputValues(newPropGetInputValues);
105 private List<GetInputValueDataDefinition> findOldGetInputValuesToMerge(PropertyDataDefinition oldProp, PropertyDataDefinition newProp) {
106 List<GetInputValueDataDefinition> oldGetInputValues = oldProp.getGetInputValues();
107 List<GetInputValueDataDefinition> newGetInputValues = Optional.ofNullable(newProp.getGetInputValues()).orElse(Collections.emptyList());
108 List<String> newGetInputNames = newGetInputValues.stream().map(GetInputValueDataDefinition::getInputName).collect(Collectors.toList());
109 return oldGetInputValues.stream().filter(getInput -> !newGetInputNames.contains(getInput.getInputName()))
110 .filter(getInput -> isValueContainsGetInput(getInput.getInputName(), newProp.getValue())).collect(Collectors.toList());
113 private boolean isValueContainsGetInput(String inputName, String value) {
114 String getInputEntry = "\"%s\":\"%s\"";
115 return value != null && value.contains(String.format(getInputEntry, ToscaFunctions.GET_INPUT.getFunctionName(), inputName));