1 package org.openecomp.sdc.be.components.merge.group;
3 import org.openecomp.sdc.be.components.merge.ComponentsGlobalMergeCommand;
4 import org.openecomp.sdc.be.components.merge.VspComponentsMergeCommand;
5 import org.openecomp.sdc.be.components.merge.property.DataDefinitionsValuesMergingBusinessLogic;
6 import org.openecomp.sdc.be.dao.api.ActionStatus;
7 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
8 import org.openecomp.sdc.be.impl.ComponentsUtils;
9 import org.openecomp.sdc.be.model.Component;
10 import org.openecomp.sdc.be.model.GroupDefinition;
11 import org.openecomp.sdc.be.model.InputDefinition;
12 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
13 import org.springframework.core.annotation.Order;
15 import java.util.List;
18 import static java.util.Collections.emptyList;
19 import static java.util.stream.Collectors.toList;
20 import static java.util.stream.Collectors.toMap;
21 import static org.apache.commons.collections.CollectionUtils.isEmpty;
22 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
23 import static org.openecomp.sdc.be.components.merge.resource.ResourceDataMergeBusinessLogic.ANY_ORDER_COMMAND;
25 @org.springframework.stereotype.Component
26 @Order(ANY_ORDER_COMMAND)
27 public class GroupPropertiesMergeCommand implements VspComponentsMergeCommand, ComponentsGlobalMergeCommand {
29 private final GroupsOperation groupsOperation;
30 private final ComponentsUtils componentsUtils;
31 private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
33 public GroupPropertiesMergeCommand(GroupsOperation groupsOperation, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
34 this.groupsOperation = groupsOperation;
35 this.componentsUtils = componentsUtils;
36 this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
40 public String description() {
41 return "merge groups user defined properties values";
45 * merge user defined group properties values from previous version into vsp defined groups in new version
46 * @param prevComponent the old component, whose group properties need to be merged from
47 * @param currentComponent the new component, whose group properties need to be merged into
48 * old and new component inputs are needed in order to determine if a "get_input" property value should be merged
49 * @return the status of the merge operation
52 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
53 List<GroupDefinition> groupsToUpdate = updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(prevComponent, currentComponent);
54 return updateGroups(currentComponent, groupsToUpdate);
57 private List<GroupDefinition> updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(Component prevComponent, Component currentComponent) {
58 List<GroupDefinition> prevGroups = prevComponent.getGroups();
59 List<GroupDefinition> newGroups = currentComponent.getGroups();
60 if (isEmpty(prevGroups) || isEmpty(newGroups)) {
63 return mergeGroupPropertiesValues(prevComponent, currentComponent, prevGroups, newGroups);
66 private List<GroupDefinition> mergeGroupPropertiesValues(Component prevComponent, Component currentComponent, List<GroupDefinition> prevGroups, List<GroupDefinition> newGroups) {
67 Map<String, GroupDefinition> prevGroupsByInvariantName = getVspGroupsMappedByInvariantName(prevGroups);
68 List<GroupDefinition> newGroupsExistInPrevVersion = getNewGroupsExistInPrevComponent(prevGroupsByInvariantName, newGroups);
69 newGroupsExistInPrevVersion.forEach(newGroup -> {
70 GroupDefinition prevGroup = prevGroupsByInvariantName.get(newGroup.getInvariantName());
71 mergeGroupProperties(prevGroup, prevComponent.safeGetInputs(), newGroup, currentComponent.safeGetInputs());
73 return newGroupsExistInPrevVersion;
76 private void mergeGroupProperties(GroupDefinition prevGroup, List<InputDefinition> prevInputs, GroupDefinition newGroup, List<InputDefinition> currInputs) {
77 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(prevGroup.getProperties(), prevInputs, newGroup.getProperties(), currInputs);
80 private List<GroupDefinition> getNewGroupsExistInPrevComponent(Map<String, GroupDefinition> prevGroupsByInvariantName, List<GroupDefinition> newGroups) {
81 return newGroups.stream()
82 .filter(newGroup -> prevGroupsByInvariantName.containsKey(newGroup.getInvariantName()))
83 .filter(newGroup -> isNotEmpty(newGroup.getProperties()))
87 private Map<String, GroupDefinition> getVspGroupsMappedByInvariantName(List<GroupDefinition> newGroups) {
88 return newGroups.stream()
89 .filter(GroupDataDefinition::isVspOriginated)
90 .filter(grp -> isNotEmpty(grp.getProperties()))
91 .collect(toMap(GroupDataDefinition::getInvariantName,
95 private ActionStatus updateGroups(Component currentComponent, List<GroupDefinition> groupsToUpdate) {
96 if (isEmpty(groupsToUpdate)) {
97 return ActionStatus.OK;
99 return groupsOperation.updateGroups(currentComponent, groupsToUpdate, false)
100 .either(updatedGroups -> ActionStatus.OK,
101 err -> componentsUtils.convertFromStorageResponse(err, currentComponent.getComponentType()));