Added oparent to sdc main
[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 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.Component;
28 import org.openecomp.sdc.be.model.InputDefinition;
29
30 import com.google.common.base.Strings;
31
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Map;
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                                             .map(InputDefinition::new)
92                                             .collect(Collectors.toList());
93         
94         inputsForRedeclaration.forEach(input -> {
95             input.setPropertyId(redeclareInputData.propertyId);
96             input.setInstanceUniqueId(redeclareInputData.propertyOwnerId);
97             
98             if(!Strings.isNullOrEmpty(redeclareInputData.value)) {
99                 input.setValue(redeclareInputData.value);
100                 input.setDefaultValue(redeclareInputData.value);
101             }
102         });
103         return inputsForRedeclaration;
104     }
105
106     private List<String> findInputsToRedeclareIds(Map<String, InputDefinition> inputsById, PropertyDataDefinition property) {
107         List<GetInputValueDataDefinition> getInputValues = property.getGetInputValues();
108         return getInputValues.stream()
109                 .filter(getInputVal -> isGetInputValueHasNoCorrespondingInput(getInputVal, inputsById))
110                 .map(GetInputValueDataDefinition::getInputId)
111                 .collect(Collectors.toList());
112     }
113
114     private boolean isGetInputValueHasNoCorrespondingInput(GetInputValueDataDefinition getInputVal, Map<String, InputDefinition> inputsById) {
115         return !inputsById.containsKey(getInputVal.getInputId());
116     }
117
118     private class RedeclareInputData {
119         private String propertyId;
120         private List<String> declaredInputIds;
121         private String propertyOwnerId;
122         private String value;
123
124         RedeclareInputData(String propertyId, List<String> declaredInputIds, String propertyOwnerId, String value) {
125             this.propertyId = propertyId;
126             this.declaredInputIds = declaredInputIds;
127             this.propertyOwnerId = propertyOwnerId;
128             this.value = value;
129         }
130
131     }
132 }