Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / input / DeclaredInputsResolver.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.input;
22
23
24 import com.google.common.base.Strings;
25 import org.openecomp.sdc.be.dao.utils.MapUtil;
26 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.InputDefinition;
30
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.stream.Collectors;
36
37 import static org.openecomp.sdc.be.utils.PropertyDefinitionUtils.resolveGetInputProperties;
38
39 @org.springframework.stereotype.Component
40 public class DeclaredInputsResolver {
41     /**
42      * @param oldComponent the old state of {@link Component} that is being updated
43      * @param newComponent the new state of {@link Component} that is being updated
44      * @param properties a list of properties
45      * @return a list of all inputs that were previously declared and need to be merged to the updating component
46      * An input needs to merged if a property was declared as an input (by the user) in previous component version and the declared input not exist in new version
47      */
48     List<InputDefinition> getPreviouslyDeclaredInputsToMerge(Component oldComponent, Component newComponent, Map<String, List<PropertyDataDefinition>> properties) {
49         List<InputDefinition> oldInputs = oldComponent.safeGetInputs();
50         return getPreviouslyDeclaredInputsToMerge(oldInputs, newComponent, properties);
51     }
52
53     public List<InputDefinition> getPreviouslyDeclaredInputsToMerge(List<InputDefinition> oldInputs, Component newComponent, Map<String, List<PropertyDataDefinition>> properties) {
54         Map<String, List<PropertyDataDefinition>> getInputProperties = resolveGetInputProperties(properties);
55         List<RedeclareInputData> inputsToRedeclareData = buildRedeclareInputData(newComponent, getInputProperties);
56         return findPrevDeclaredInputs(oldInputs, inputsToRedeclareData);
57     }
58
59     private List<RedeclareInputData> buildRedeclareInputData(Component newComponent, Map<String, List<PropertyDataDefinition>> getInputProperties) {
60         Map<String, InputDefinition> inputsById = MapUtil.toMap(newComponent.getInputs(), InputDefinition::getUniqueId);
61         List<RedeclareInputData> redeclareInputData = new ArrayList<>();
62         getInputProperties.forEach((instanceId, getInputProps) -> redeclareInputData.addAll(findInputsToRedeclare(inputsById, instanceId, getInputProps)));
63         return redeclareInputData;
64
65     }
66
67     private List<InputDefinition> findPrevDeclaredInputs(List<InputDefinition> oldInputs, List<RedeclareInputData> inputsToRedeclareData) {
68         Map<String, InputDefinition> oldInputsById = MapUtil.toMap(oldInputs, InputDefinition::getUniqueId);
69         List<InputDefinition> inputsToRedeclare = new ArrayList<>();
70         inputsToRedeclareData.forEach(redeclareInputData -> {
71             List<InputDefinition> inputDefinitions = prepareInputsForRedeclaration(oldInputsById, redeclareInputData);
72             inputsToRedeclare.addAll(inputDefinitions);
73         });
74         return inputsToRedeclare;
75     }
76
77     private List<RedeclareInputData> findInputsToRedeclare(Map<String, InputDefinition> inputsById, String instanceId, List<PropertyDataDefinition> getInputProps) {
78         List<RedeclareInputData> redeclareInputDataList = new ArrayList<>();
79         getInputProps.forEach(property -> {
80             List<String> inputsToRedeclareIds = findInputsToRedeclareIds(inputsById, property);
81             RedeclareInputData redeclareInputData = new RedeclareInputData(property.getUniqueId(), inputsToRedeclareIds, instanceId, property.getDefaultValue());
82             redeclareInputDataList.add(redeclareInputData);
83         });
84         return redeclareInputDataList;
85     }
86
87     private List<InputDefinition> prepareInputsForRedeclaration(Map<String, InputDefinition> oldInputsById, RedeclareInputData redeclareInputData) {
88         List<InputDefinition> inputsForRedeclaration = redeclareInputData.declaredInputIds.stream()
89                                             .filter(oldInputsById::containsKey)
90                                             .map(oldInputsById::get)
91                                             .filter(Objects::nonNull)
92                                             .map(InputDefinition::new)
93                                             .collect(Collectors.toList());
94         
95         inputsForRedeclaration.forEach(input -> {
96             input.setPropertyId(redeclareInputData.propertyId);
97             input.setInstanceUniqueId(redeclareInputData.propertyOwnerId);
98             
99             if(!Strings.isNullOrEmpty(redeclareInputData.value)) {
100                 input.setValue(redeclareInputData.value);
101                 input.setDefaultValue(redeclareInputData.value);
102             }
103         });
104         return inputsForRedeclaration;
105     }
106
107     private List<String> findInputsToRedeclareIds(Map<String, InputDefinition> inputsById, PropertyDataDefinition property) {
108         List<GetInputValueDataDefinition> getInputValues = property.getGetInputValues();
109         return getInputValues.stream()
110                 .filter(getInputVal -> isGetInputValueHasNoCorrespondingInput(getInputVal, inputsById))
111                 .map(GetInputValueDataDefinition::getInputId)
112                 .collect(Collectors.toList());
113     }
114
115     private boolean isGetInputValueHasNoCorrespondingInput(GetInputValueDataDefinition getInputVal, Map<String, InputDefinition> inputsById) {
116         return !inputsById.containsKey(getInputVal.getInputId());
117     }
118
119     private class RedeclareInputData {
120         private String propertyId;
121         private List<String> declaredInputIds;
122         private String propertyOwnerId;
123         private String value;
124
125         RedeclareInputData(String propertyId, List<String> declaredInputIds, String propertyOwnerId, String value) {
126             this.propertyId = propertyId;
127             this.declaredInputIds = declaredInputIds;
128             this.propertyOwnerId = propertyOwnerId;
129             this.value = value;
130         }
131
132     }
133 }