Fix 'Changing VFC version on template wipes previously assigned property values based...
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / PropertyInstanceMergeDataBuilder.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 java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.stream.Collectors;
27 import org.openecomp.sdc.be.dao.utils.MapUtil;
28 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
30 import org.openecomp.sdc.be.model.InputDefinition;
31
32 class PropertyInstanceMergeDataBuilder {
33
34     private PropertyInstanceMergeDataBuilder() {
35     }
36
37     static <T extends PropertyDataDefinition> List<MergePropertyData> buildDataForMerging(List<T> oldProps, List<InputDefinition> oldInputs,
38                                                                                           List<T> newProps, List<InputDefinition> newInputs) {
39         Map<String, T> oldPropsByName = MapUtil.toMap(oldProps, T::getName);
40         Map<String, InputDefinition> oldInputsByName = MapUtil.toMap(oldInputs, InputDefinition::getName);
41         Map<String, T> newPropsByName = MapUtil.toMap(newProps, T::getName);
42         Map<String, InputDefinition> newInputsByName = MapUtil.toMap(newInputs, InputDefinition::getName);
43         return buildMergeData(oldPropsByName, oldInputsByName, newPropsByName, newInputsByName);
44     }
45
46     private static <T extends PropertyDataDefinition> List<MergePropertyData> buildMergeData(Map<String, T> oldPropsByName,
47                                                                                              Map<String, InputDefinition> oldInputsByName,
48                                                                                              Map<String, T> newPropsByName,
49                                                                                              Map<String, InputDefinition> newInputsByName) {
50         List<MergePropertyData> mergeData = new ArrayList<>();
51         newPropsByName.forEach((name, prop) -> {
52             if (oldPropsByName.containsKey(name)) {
53                 mergeData.add(buildMergePropertyData(oldPropsByName.get(name), oldInputsByName, prop, newInputsByName));
54             }
55         });
56         return mergeData;
57     }
58
59     private static MergePropertyData buildMergePropertyData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName,
60                                                             PropertyDataDefinition newProp, Map<String, InputDefinition> newInputsByName) {
61         MergePropertyData mergePropertyData = new MergePropertyData();
62         mergePropertyData.setOldProp(oldProp).setNewProp(newProp);
63         if (oldProp.isGetInputProperty()) {
64             setGetInputData(oldProp, oldInputsByName, newInputsByName, mergePropertyData);
65         }
66         return mergePropertyData;
67     }
68
69     private static void setGetInputData(PropertyDataDefinition oldProp, Map<String, InputDefinition> oldInputsByName,
70                                         Map<String, InputDefinition> newInputsByName, MergePropertyData mergePropertyData) {
71         List<String> oldDeclaredByUserInputNames = getOldDeclaredInputsByUser(oldProp.getGetInputValues(), oldInputsByName);
72         List<String> oldGetInputNamesWhichExistInNewVersion = getOldGetInputNamesWhichExistInNewVersion(oldProp.getGetInputValues(), newInputsByName);
73         mergePropertyData.addAddGetInputNamesToMerge(oldDeclaredByUserInputNames);
74         mergePropertyData.addAddGetInputNamesToMerge(oldGetInputNamesWhichExistInNewVersion);
75     }
76
77     private static List<String> getOldGetInputNamesWhichExistInNewVersion(List<GetInputValueDataDefinition> getInputValues,
78                                                                           Map<String, InputDefinition> newInputsByName) {
79         return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).filter(newInputsByName::containsKey)
80             .collect(Collectors.toList());
81     }
82
83     private static List<String> getOldDeclaredInputsByUser(List<GetInputValueDataDefinition> getInputValues,
84                                                            Map<String, InputDefinition> oldInputsByName) {
85         return getInputValues.stream().map(GetInputValueDataDefinition::getInputName).map(oldInputsByName::get)
86             .filter(oldInput -> Objects.nonNull(oldInput) && oldInput.getInstanceUniqueId() != null).map(PropertyDataDefinition::getName)
87             .collect(Collectors.toList());
88     }
89 }