Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / group / GroupPropertiesMergeCommand.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.group;
22
23 import org.openecomp.sdc.be.components.merge.ComponentsGlobalMergeCommand;
24 import org.openecomp.sdc.be.components.merge.VspComponentsMergeCommand;
25 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
28 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.GroupDefinition;
32 import org.openecomp.sdc.be.model.InputDefinition;
33 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
34 import org.springframework.core.annotation.Order;
35
36 import java.util.List;
37 import java.util.Map;
38
39 import static java.util.Collections.emptyList;
40 import static java.util.stream.Collectors.toList;
41 import static java.util.stream.Collectors.toMap;
42 import static org.apache.commons.collections.CollectionUtils.isEmpty;
43 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
44 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
45
46 @org.springframework.stereotype.Component
47 @Order(ANY_ORDER_COMMAND)
48 public class GroupPropertiesMergeCommand implements VspComponentsMergeCommand, ComponentsGlobalMergeCommand {
49
50     private final GroupsOperation groupsOperation;
51     private final ComponentsUtils componentsUtils;
52     private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
53
54     public GroupPropertiesMergeCommand(GroupsOperation groupsOperation, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
55         this.groupsOperation = groupsOperation;
56         this.componentsUtils = componentsUtils;
57         this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
58     }
59
60     @Override
61     public String description() {
62         return "merge groups user defined properties values";
63     }
64
65     /**
66      * merge user defined group properties values from previous version into vsp defined groups in new version
67      * @param prevComponent the old component, whose group properties need to be merged from
68      * @param currentComponent the new component, whose group properties need to be merged into
69      * old and new component inputs are needed in order to determine if a "get_input" property value should be merged
70      * @return the status of the merge operation
71      */
72     @Override
73     public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
74         List<GroupDefinition> groupsToUpdate = updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(prevComponent, currentComponent);
75         return updateGroups(currentComponent, groupsToUpdate);
76     }
77
78     private List<GroupDefinition> updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(Component prevComponent, Component currentComponent) {
79         List<GroupDefinition> prevGroups = prevComponent.getGroups();
80         List<GroupDefinition> newGroups = currentComponent.getGroups();
81         if (isEmpty(prevGroups) || isEmpty(newGroups)) {
82             return emptyList();
83         }
84         return mergeGroupPropertiesValues(prevComponent, currentComponent, prevGroups, newGroups);
85     }
86
87     private List<GroupDefinition> mergeGroupPropertiesValues(Component prevComponent, Component currentComponent, List<GroupDefinition> prevGroups, List<GroupDefinition> newGroups) {
88         Map<String, GroupDefinition> prevGroupsByInvariantName = getVspGroupsMappedByInvariantName(prevGroups);
89         List<GroupDefinition> newGroupsExistInPrevVersion = getNewGroupsExistInPrevComponent(prevGroupsByInvariantName, newGroups);
90         newGroupsExistInPrevVersion.forEach(newGroup -> {
91             GroupDefinition prevGroup = prevGroupsByInvariantName.get(newGroup.getInvariantName());
92             mergeGroupProperties(prevGroup, prevComponent.safeGetInputs(), newGroup, currentComponent.safeGetInputs());
93         });
94         return newGroupsExistInPrevVersion;
95     }
96
97     private void mergeGroupProperties(GroupDefinition prevGroup, List<InputDefinition> prevInputs, GroupDefinition newGroup, List<InputDefinition> currInputs) {
98         propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(prevGroup.getProperties(), prevInputs, newGroup.getProperties(), currInputs);
99     }
100
101     private List<GroupDefinition> getNewGroupsExistInPrevComponent(Map<String, GroupDefinition> prevGroupsByInvariantName, List<GroupDefinition> newGroups) {
102         return newGroups.stream()
103                 .filter(newGroup -> prevGroupsByInvariantName.containsKey(newGroup.getInvariantName()))
104                 .filter(newGroup -> isNotEmpty(newGroup.getProperties()))
105                 .collect(toList());
106     }
107
108     private Map<String, GroupDefinition> getVspGroupsMappedByInvariantName(List<GroupDefinition> newGroups) {
109         return newGroups.stream()
110                 .filter(GroupDataDefinition::isVspOriginated)
111                 .filter(grp -> isNotEmpty(grp.getProperties()))
112                 .collect(toMap(GroupDataDefinition::getInvariantName,
113                                group -> group));
114     }
115
116     private ActionStatus updateGroups(Component currentComponent, List<GroupDefinition> groupsToUpdate) {
117         if (isEmpty(groupsToUpdate)) {
118             return ActionStatus.OK;
119         }
120         return groupsOperation.updateGroups(currentComponent, groupsToUpdate, PromoteVersionEnum.MINOR)
121                 .either(updatedGroups -> ActionStatus.OK,
122                         err -> componentsUtils.convertFromStorageResponse(err, currentComponent.getComponentType()));
123     }
124
125 }