2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.input;
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;
31 import java.util.ArrayList;
32 import java.util.List;
34 import java.util.Objects;
35 import java.util.stream.Collectors;
37 import static org.openecomp.sdc.be.utils.PropertyDefinitionUtils.resolveGetInputProperties;
39 @org.springframework.stereotype.Component
40 public class DeclaredInputsResolver {
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
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);
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);
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;
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);
74 return inputsToRedeclare;
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);
84 return redeclareInputDataList;
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());
95 inputsForRedeclaration.forEach(input -> {
96 input.setPropertyId(redeclareInputData.propertyId);
97 input.setInstanceUniqueId(redeclareInputData.propertyOwnerId);
99 if(!Strings.isNullOrEmpty(redeclareInputData.value)) {
100 input.setValue(redeclareInputData.value);
101 input.setDefaultValue(redeclareInputData.value);
104 return inputsForRedeclaration;
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());
115 private boolean isGetInputValueHasNoCorrespondingInput(GetInputValueDataDefinition getInputVal, Map<String, InputDefinition> inputsById) {
116 return !inputsById.containsKey(getInputVal.getInputId());
119 private class RedeclareInputData {
120 private String propertyId;
121 private List<String> declaredInputIds;
122 private String propertyOwnerId;
123 private String value;
125 RedeclareInputData(String propertyId, List<String> declaredInputIds, String propertyOwnerId, String value) {
126 this.propertyId = propertyId;
127 this.declaredInputIds = declaredInputIds;
128 this.propertyOwnerId = propertyOwnerId;