Added oparent to sdc main
[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
21 package org.openecomp.sdc.be.components.merge.property;
22
23 import java.util.Objects;
24 import org.openecomp.sdc.be.dao.utils.MapUtil;
25 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
27 import org.openecomp.sdc.be.model.InputDefinition;
28
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.stream.Collectors;
33
34 class PropertyInstanceMergeDataBuilder {
35
36     private PropertyInstanceMergeDataBuilder() {
37     }
38
39     static <T extends PropertyDataDefinition> List<MergePropertyData> buildDataForMerging(List<T> oldProps,
40                                                                                           List<InputDefinition> oldInputs,
41                                                                                           List<T> newProps,
42                                                                                           List<InputDefinition> newInputs) {
43
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);
49
50     }
51
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));
57             }
58         });
59         return mergeData;
60     }
61
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)
68                          .setNewProp(newProp);
69         if (oldProp.isGetInputProperty()) {
70             setGetInputData(oldProp, oldInputsByName, newInputsByName, mergePropertyData);
71
72         }
73         return mergePropertyData;
74     }
75
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);
81     }
82
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());
85     }
86
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());
93     }
94 }