6f1a1e1ca8e65d8437b890e315b538453d9b9e83
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / DataDefinitionsValuesMergingBusinessLogic.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 package org.openecomp.sdc.be.components.merge.property;
21
22 import static org.apache.commons.collections.CollectionUtils.isEmpty;
23 import static org.openecomp.sdc.be.components.merge.property.PropertyInstanceMergeDataBuilder.buildDataForMerging;
24
25 import java.util.Collections;
26 import java.util.List;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.model.InputDefinition;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class DataDefinitionsValuesMergingBusinessLogic {
33
34     private PropertyDataValueMergeBusinessLogic propertyValueMergeBL;
35
36     public DataDefinitionsValuesMergingBusinessLogic(PropertyDataValueMergeBusinessLogic propertyValueMergeBL) {
37         this.propertyValueMergeBL = propertyValueMergeBL;
38     }
39
40     /**
41      * Merge previous version data definition values into the new version data definition. A data definition value is merged if it had a value in
42      * previous version and has no value in the current version. in case a property get input value has no corresponding input in the current version
43      * its value will not be merged
44      *
45      * @param oldInstanceDataDefinition     the currently persisted instance data definitions
46      * @param oldInputs                     the previous version inputs
47      * @param updatedInstanceDataDefinition the currently being update instance data definitions
48      * @param newInputs                     the new version inputs
49      */
50     public <T extends PropertyDataDefinition> void mergeInstanceDataDefinitions(List<T> oldInstanceDataDefinition, List<InputDefinition> oldInputs,
51                                                                                 List<T> updatedInstanceDataDefinition,
52                                                                                 List<InputDefinition> newInputs) {
53         if (isEmpty(updatedInstanceDataDefinition) || isEmpty(oldInstanceDataDefinition)) {
54             return;
55         }
56         List<MergePropertyData> mergePropertyData = buildDataForMerging(oldInstanceDataDefinition, oldInputs, updatedInstanceDataDefinition,
57             newInputs);
58         mergePropertyData.forEach(this::mergeInstanceDefinition);
59     }
60
61     public <T extends PropertyDataDefinition> void mergeInstanceDataDefinitions(List<T> oldInstanceDataDefinition,
62                                                                                 List<T> updatedInstanceDataDefinition) {
63         List<InputDefinition> emptyInputsList = Collections.emptyList();
64         mergeInstanceDataDefinitions(oldInstanceDataDefinition, emptyInputsList, updatedInstanceDataDefinition, emptyInputsList);
65     }
66
67     private void mergeInstanceDefinition(MergePropertyData mergeData) {
68         if (isSameType(mergeData.getOldProp(), mergeData.getNewProp())) {
69             propertyValueMergeBL.mergePropertyValue(mergeData.getOldProp(), mergeData.getNewProp(), mergeData.getGetInputNamesToMerge());
70         }
71     }
72
73     private boolean isSameType(PropertyDataDefinition oldDataDefinition, PropertyDataDefinition updatedDataDefinition) {
74         return oldDataDefinition.typeEquals(updatedDataDefinition);
75     }
76 }