2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.group;
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;
36 import java.util.List;
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;
46 @org.springframework.stereotype.Component
47 @Order(ANY_ORDER_COMMAND)
48 public class GroupPropertiesMergeCommand implements VspComponentsMergeCommand, ComponentsGlobalMergeCommand {
50 private final GroupsOperation groupsOperation;
51 private final ComponentsUtils componentsUtils;
52 private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
54 public GroupPropertiesMergeCommand(GroupsOperation groupsOperation, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
55 this.groupsOperation = groupsOperation;
56 this.componentsUtils = componentsUtils;
57 this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
61 public String description() {
62 return "merge groups user defined properties values";
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
73 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
74 List<GroupDefinition> groupsToUpdate = updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(prevComponent, currentComponent);
75 return updateGroups(currentComponent, groupsToUpdate);
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)) {
84 return mergeGroupPropertiesValues(prevComponent, currentComponent, prevGroups, newGroups);
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());
94 return newGroupsExistInPrevVersion;
97 private void mergeGroupProperties(GroupDefinition prevGroup, List<InputDefinition> prevInputs, GroupDefinition newGroup, List<InputDefinition> currInputs) {
98 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(prevGroup.getProperties(), prevInputs, newGroup.getProperties(), currInputs);
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()))
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,
116 private ActionStatus updateGroups(Component currentComponent, List<GroupDefinition> groupsToUpdate) {
117 if (isEmpty(groupsToUpdate)) {
118 return ActionStatus.OK;
120 return groupsOperation.updateGroups(currentComponent, groupsToUpdate, PromoteVersionEnum.MINOR)
121 .either(updatedGroups -> ActionStatus.OK,
122 err -> componentsUtils.convertFromStorageResponse(err, currentComponent.getComponentType()));