1 package org.openecomp.sdc.be.components.merge.input;
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;
10 import com.google.common.base.Strings;
12 import java.util.ArrayList;
13 import java.util.List;
15 import java.util.stream.Collectors;
17 import static org.openecomp.sdc.be.utils.PropertyDefinitionUtils.resolveGetInputProperties;
19 @org.springframework.stereotype.Component
20 public class DeclaredInputsResolver {
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
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);
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);
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;
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);
54 return inputsToRedeclare;
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);
64 return redeclareInputDataList;
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());
73 inputsForRedeclaration.forEach(input -> {
74 input.setPropertyId(redeclareInputData.propertyId);
75 input.setInstanceUniqueId(redeclareInputData.propertyOwnerId);
77 if(!Strings.isNullOrEmpty(redeclareInputData.value)) {
78 input.setValue(redeclareInputData.value);
79 input.setDefaultValue(redeclareInputData.value);
82 return inputsForRedeclaration;
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());
93 private boolean isGetInputValueHasNoCorrespondingInput(GetInputValueDataDefinition getInputVal, Map<String, InputDefinition> inputsById) {
94 return !inputsById.containsKey(getInputVal.getInputId());
97 private class RedeclareInputData {
98 private String propertyId;
99 private List<String> declaredInputIds;
100 private String propertyOwnerId;
101 private String value;
103 RedeclareInputData(String propertyId, List<String> declaredInputIds, String propertyOwnerId, String value) {
104 this.propertyId = propertyId;
105 this.declaredInputIds = declaredInputIds;
106 this.propertyOwnerId = propertyOwnerId;