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