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