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.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;
35 import java.util.List;
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;
45 @org.springframework.stereotype.Component
46 @Order(ANY_ORDER_COMMAND)
47 public class GroupPropertiesMergeCommand implements VspComponentsMergeCommand, ComponentsGlobalMergeCommand {
49 private final GroupsOperation groupsOperation;
50 private final ComponentsUtils componentsUtils;
51 private final DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic;
53 public GroupPropertiesMergeCommand(GroupsOperation groupsOperation, ComponentsUtils componentsUtils, DataDefinitionsValuesMergingBusinessLogic propertyValuesMergingBusinessLogic) {
54 this.groupsOperation = groupsOperation;
55 this.componentsUtils = componentsUtils;
56 this.propertyValuesMergingBusinessLogic = propertyValuesMergingBusinessLogic;
60 public String description() {
61 return "merge groups user defined properties values";
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
72 public ActionStatus mergeComponents(Component prevComponent, Component currentComponent) {
73 List<GroupDefinition> groupsToUpdate = updateOldGrpsPropertiesValuesIntoNewVspGroupsProps(prevComponent, currentComponent);
74 return updateGroups(currentComponent, groupsToUpdate);
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)) {
83 return mergeGroupPropertiesValues(prevComponent, currentComponent, prevGroups, newGroups);
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());
93 return newGroupsExistInPrevVersion;
96 private void mergeGroupProperties(GroupDefinition prevGroup, List<InputDefinition> prevInputs, GroupDefinition newGroup, List<InputDefinition> currInputs) {
97 propertyValuesMergingBusinessLogic.mergeInstanceDataDefinitions(prevGroup.getProperties(), prevInputs, newGroup.getProperties(), currInputs);
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()))
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,
115 private ActionStatus updateGroups(Component currentComponent, List<GroupDefinition> groupsToUpdate) {
116 if (isEmpty(groupsToUpdate)) {
117 return ActionStatus.OK;
119 return groupsOperation.updateGroups(currentComponent, groupsToUpdate, false)
120 .either(updatedGroups -> ActionStatus.OK,
121 err -> componentsUtils.convertFromStorageResponse(err, currentComponent.getComponentType()));