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.impl.instance;
23 import org.openecomp.sdc.be.components.impl.group.GroupMembersUpdater;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.ComponentInstance;
29 import org.openecomp.sdc.be.model.GroupDefinition;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import java.util.List;
34 import java.util.function.Consumer;
36 import static org.apache.commons.collections.CollectionUtils.isEmpty;
37 @org.springframework.stereotype.Component
38 public class GroupMembersUpdateOperation implements OnComponentInstanceChangeOperation {
40 private static final Logger log = Logger.getLogger(GroupMembersUpdateOperation.class);
41 private final GroupsOperation groupsOperation;
42 private final ComponentsUtils componentsUtils;
43 private final GroupMembersUpdater groupMembersUpdater;
45 public GroupMembersUpdateOperation(GroupsOperation groupsOperation, ComponentsUtils componentsUtils, GroupMembersUpdater groupMembersUpdater) {
46 this.groupsOperation = groupsOperation;
47 this.componentsUtils = componentsUtils;
48 this.groupMembersUpdater = groupMembersUpdater;
52 public ActionStatus onChangeVersion(Component container, ComponentInstance prevVersion, ComponentInstance newVersion) {
53 log.debug("#onChangeVersion - replacing all group members for component instance {} with new component instance on component", prevVersion.getUniqueId(), newVersion.getUniqueId(), container.getUniqueId());
54 Consumer<List<GroupDefinition>> replaceGroupMemberTask = (groups) -> groupMembersUpdater.replaceMember(groups, prevVersion.getUniqueId(), newVersion.getUniqueId());
55 return doUpdateGroupMembers(container, prevVersion.getUniqueId(), replaceGroupMemberTask);
59 public ActionStatus onDelete(Component container, String deletedEntityId) {
60 log.debug("#onDelete - deleting group member referencing component instance {} on component {}.", deletedEntityId, container.getUniqueId());
61 Consumer<List<GroupDefinition>> deleteGroupMemberTask = (groups) -> groupMembersUpdater.removeMember(groups, deletedEntityId);
62 return doUpdateGroupMembers(container, deletedEntityId, deleteGroupMemberTask);
65 private ActionStatus doUpdateGroupMembers(Component container, String memberId, Consumer<List<GroupDefinition>> updateGroupMemberTask) {
66 List<GroupDefinition> groupsWithPrevInstAsMember = container.resolveGroupsByMember(memberId);
67 if (isEmpty(groupsWithPrevInstAsMember)) {
68 log.debug("#doUpdateGroupMembers - container {} has no groups with component instance {} as member.", container.getUniqueId(), memberId);
69 return ActionStatus.OK;
71 updateGroupMemberTask.accept(groupsWithPrevInstAsMember);
72 return updateGroups(container, groupsWithPrevInstAsMember);
75 private ActionStatus updateGroups(Component container, List<GroupDefinition> groupsToUpdate) {
76 log.debug("#updateGroups - updating {} groups for container {}", groupsToUpdate.size(), container.getUniqueId());
77 return groupsOperation.updateGroups(container, groupsToUpdate, PromoteVersionEnum.MINOR)
78 .either(groupsUpdated -> ActionStatus.OK,
79 err -> componentsUtils.convertFromStorageResponse(err, container.getComponentType()));