re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / input / DeclaredInputsResolver.java
1 package org.openecomp.sdc.be.components.merge.input;
2
3
4 import org.openecomp.sdc.be.dao.utils.MapUtil;
5 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
6 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
7 import org.openecomp.sdc.be.model.Component;
8 import org.openecomp.sdc.be.model.InputDefinition;
9
10 import com.google.common.base.Strings;
11
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.stream.Collectors;
16
17 import static org.openecomp.sdc.be.utils.PropertyDefinitionUtils.resolveGetInputProperties;
18
19 @org.springframework.stereotype.Component
20 public class DeclaredInputsResolver {
21     /**
22      * @param oldComponent the old state of {@link Component} that is being updated
23      * @param newComponent the new state of {@link Component} that is being updated
24      * @param properties a list of properties
25      * @return a list of all inputs that were previously declared and need to be merged to the updating component
26      * 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
27      */
28     List<InputDefinition> getPreviouslyDeclaredInputsToMerge(Component oldComponent, Component newComponent, Map<String, List<PropertyDataDefinition>> properties) {
29         List<InputDefinition> oldInputs = oldComponent.safeGetInputs();
30         return getPreviouslyDeclaredInputsToMerge(oldInputs, newComponent, properties);
31     }
32
33     public List<InputDefinition> getPreviouslyDeclaredInputsToMerge(List<InputDefinition> oldInputs, Component newComponent, Map<String, List<PropertyDataDefinition>> properties) {
34         Map<String, List<PropertyDataDefinition>> getInputProperties = resolveGetInputProperties(properties);
35         List<RedeclareInputData> inputsToRedeclareData = buildRedeclareInputData(newComponent, getInputProperties);
36         return findPrevDeclaredInputs(oldInputs, inputsToRedeclareData);
37     }
38
39     private List<RedeclareInputData> buildRedeclareInputData(Component newComponent, Map<String, List<PropertyDataDefinition>> getInputProperties) {
40         Map<String, InputDefinition> inputsById = MapUtil.toMap(newComponent.getInputs(), InputDefinition::getUniqueId);
41         List<RedeclareInputData> redeclareInputData = new ArrayList<>();
42         getInputProperties.forEach((instanceId, getInputProps) -> redeclareInputData.addAll(findInputsToRedeclare(inputsById, instanceId, getInputProps)));
43         return redeclareInputData;
44
45     }
46
47     private List<InputDefinition> findPrevDeclaredInputs(List<InputDefinition> oldInputs, List<RedeclareInputData> inputsToRedeclareData) {
48         Map<String, InputDefinition> oldInputsById = MapUtil.toMap(oldInputs, InputDefinition::getUniqueId);
49         List<InputDefinition> inputsToRedeclare = new ArrayList<>();
50         inputsToRedeclareData.forEach(redeclareInputData -> {
51             List<InputDefinition> inputDefinitions = prepareInputsForRedeclaration(oldInputsById, redeclareInputData);
52             inputsToRedeclare.addAll(inputDefinitions);
53         });
54         return inputsToRedeclare;
55     }
56
57     private List<RedeclareInputData> findInputsToRedeclare(Map<String, InputDefinition> inputsById, String instanceId, List<PropertyDataDefinition> getInputProps) {
58         List<RedeclareInputData> redeclareInputDataList = new ArrayList<>();
59         getInputProps.forEach(property -> {
60             List<String> inputsToRedeclareIds = findInputsToRedeclareIds(inputsById, property);
61             RedeclareInputData redeclareInputData = new RedeclareInputData(property.getUniqueId(), inputsToRedeclareIds, instanceId, property.getDefaultValue());
62             redeclareInputDataList.add(redeclareInputData);
63         });
64         return redeclareInputDataList;
65     }
66
67     private List<InputDefinition> prepareInputsForRedeclaration(Map<String, InputDefinition> oldInputsById, RedeclareInputData redeclareInputData) {
68         List<InputDefinition> inputsForRedeclaration = redeclareInputData.declaredInputIds.stream()
69                                             .map(oldInputsById::get)
70                                             .map(InputDefinition::new)
71                                             .collect(Collectors.toList());
72         
73         inputsForRedeclaration.forEach(input -> {
74             input.setPropertyId(redeclareInputData.propertyId);
75             input.setInstanceUniqueId(redeclareInputData.propertyOwnerId);
76             
77             if(!Strings.isNullOrEmpty(redeclareInputData.value)) {
78                 input.setValue(redeclareInputData.value);
79                 input.setDefaultValue(redeclareInputData.value);
80             }
81         });
82         return inputsForRedeclaration;
83     }
84
85     private List<String> findInputsToRedeclareIds(Map<String, InputDefinition> inputsById, PropertyDataDefinition property) {
86         List<GetInputValueDataDefinition> getInputValues = property.getGetInputValues();
87         return getInputValues.stream()
88                 .filter(getInputVal -> isGetInputValueHasNoCorrespondingInput(getInputVal, inputsById))
89                 .map(GetInputValueDataDefinition::getInputId)
90                 .collect(Collectors.toList());
91     }
92
93     private boolean isGetInputValueHasNoCorrespondingInput(GetInputValueDataDefinition getInputVal, Map<String, InputDefinition> inputsById) {
94         return !inputsById.containsKey(getInputVal.getInputId());
95     }
96
97     private class RedeclareInputData {
98         private String propertyId;
99         private List<String> declaredInputIds;
100         private String propertyOwnerId;
101         private String value;
102
103         RedeclareInputData(String propertyId, List<String> declaredInputIds, String propertyOwnerId, String value) {
104             this.propertyId = propertyId;
105             this.declaredInputIds = declaredInputIds;
106             this.propertyOwnerId = propertyOwnerId;
107             this.value = value;
108         }
109
110     }
111 }