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 org.openecomp.sdc.be.dao.utils.MapUtil;
24 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
26 import org.openecomp.sdc.be.model.InputDefinition;
28 import java.util.ArrayList;
29 import java.util.List;
31 import java.util.Objects;
32 import java.util.stream.Collectors;
34 class PropertyInstanceMergeDataBuilder {
36 private PropertyInstanceMergeDataBuilder() {
39 static <T extends PropertyDataDefinition> List<MergePropertyData> buildDataForMerging(List<T> oldProps,
40 List<InputDefinition> oldInputs,
42 List<InputDefinition> newInputs) {
44 Map<String, T> oldPropsByName = MapUtil.toMap(oldProps, T::getName);
45 Map<String, InputDefinition> oldInputsByName = MapUtil.toMap(oldInputs, InputDefinition::getName);
46 Map<String, T> newPropsByName = MapUtil.toMap(newProps, T::getName);
47 Map<String, InputDefinition> newInputsByName = MapUtil.toMap(newInputs, InputDefinition::getName);
48 return buildMergeData(oldPropsByName, oldInputsByName, newPropsByName, newInputsByName);
52 private static <T extends PropertyDataDefinition> List<MergePropertyData> buildMergeData(Map<String, T> oldPropsByName, Map<String, InputDefinition> oldInputsByName, Map<String, T> newPropsByName, Map<String, InputDefinition> newInputsByName) {
53 List<MergePropertyData> mergeData = new ArrayList<>();
54 newPropsByName.forEach((name, prop) -> {
55 if (oldPropsByName.containsKey(name)) {
56 mergeData.add(buildMergePropertyData(oldPropsByName.get(name), oldInputsByName, prop, newInputsByName));
62 private static MergePropertyData buildMergePropertyData(PropertyDataDefinition oldProp,
63 Map<String, InputDefinition> oldInputsByName,
64 PropertyDataDefinition newProp,
65 Map<String, InputDefinition> newInputsByName) {
66 MergePropertyData mergePropertyData = new MergePropertyData();
67 mergePropertyData.setOldProp(oldProp)
69 if (oldProp.isGetInputProperty()) {
70 setGetInputData(oldProp, oldInputsByName, newInputsByName, mergePropertyData);
73 return mergePropertyData;
76 private static void setGetInputData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName, Map<String, InputDefinition> newInputsByName, MergePropertyData mergePropertyData) {
77 List<String> oldDeclaredByUserInputNames = getOldDeclaredInputsByUser(oldProp.getGetInputValues(), oldInputsByName);
78 List<String> oldGetInputNamesWhichExistInNewVersion = getOldGetInputNamesWhichExistInNewVersion(oldProp.getGetInputValues(), newInputsByName);
79 mergePropertyData.addAddGetInputNamesToMerge(oldDeclaredByUserInputNames);
80 mergePropertyData.addAddGetInputNamesToMerge(oldGetInputNamesWhichExistInNewVersion);
83 private static List<String> getOldGetInputNamesWhichExistInNewVersion(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> newInputsByName) {
84 return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).filter(newInputsByName::containsKey).collect(Collectors.toList());
87 private static List<String> getOldDeclaredInputsByUser(List<GetInputValueDataDefinition> getInputValues, Map<String, InputDefinition> oldInputsByName) {
88 return getInputValues.stream().map(GetInputValueDataDefinition::getInputName)
89 .map(oldInputsByName::get)
90 .filter(oldInput -> Objects.nonNull(oldInput) && oldInput.getInstanceUniqueId() != null)
91 .map(PropertyDataDefinition::getName)
92 .collect(Collectors.toList());