use of cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncStarted in project perun by CESNET.
the class GroupsManagerBlImpl method synchronizeGroup.
@Override
public List<String> synchronizeGroup(PerunSession sess, Group group) throws AttributeNotExistsException, WrongAttributeAssignmentException, ExtSourceNotExistsException, GroupNotExistsException {
// needed variables for whole method
List<String> skippedMembers = new ArrayList<>();
ExtSource source = null;
ExtSource membersSource = null;
try {
// set Logback's Mapped Diagnostic Context key for the current thread
MDC.put(MDC_LOG_FILE_NAME, "groupsync/group_" + group.getId());
long startTime = System.nanoTime();
getPerunBl().getAuditer().log(sess, new GroupSyncStarted(group));
log.debug("Group synchronization for {} has been started.", group);
// Initialization of group extSource
source = getGroupExtSourceForSynchronization(sess, group);
// Initialization of groupMembers extSource (if it is set), in other case set membersSource = source
membersSource = getGroupMembersExtSourceForSynchronization(sess, group, source);
// Prepare info about userAttributes which need to be overwritten (not just updated) and memberAttributes which need to be merged not overwritten
List<String> overwriteUserAttributesList = getAttributesListFromExtSource(membersSource, OVERWRITE_USER_ATTRIBUTES);
List<String> mergeMemberAttributesList = getAttributesListFromExtSource(membersSource, MERGE_MEMBER_ATTRIBUTES);
// Get info about type of synchronization (with or without update)
boolean lightweightSynchronization = isThisLightweightSynchronization(sess, group);
log.debug("Group synchronization {}: using configuration extSource for membership {}, extSource for members {}", group, membersSource, membersSource.getName());
// Prepare containers for work with group members
List<Candidate> candidatesToAdd = new ArrayList<>();
Map<Candidate, RichMember> membersToUpdate = new HashMap<>();
List<RichMember> membersToRemove = new ArrayList<>();
// get all direct members of synchronized group (only direct, because we want to set direct membership with this group by synchronization)
List<RichMember> actualGroupMembers = getPerunBl().getGroupsManagerBl().getGroupDirectRichMembers(sess, group);
if (lightweightSynchronization) {
categorizeMembersForLightweightSynchronization(sess, group, source, membersSource, actualGroupMembers, candidatesToAdd, membersToRemove, skippedMembers);
} else {
// Get subjects from extSource
List<Map<String, String>> subjects = getSubjectsFromExtSource(sess, source, group);
// Convert subjects to candidates
List<Candidate> candidates = convertSubjectsToCandidates(sess, subjects, membersSource, source, actualGroupMembers, skippedMembers);
categorizeMembersForSynchronization(sess, actualGroupMembers, candidates, candidatesToAdd, membersToUpdate, membersToRemove);
}
// Remove members from group who are not present in synchronized ExtSource
boolean isAuthoritative = isAuthoritative(sess, group);
Collections.sort(membersToRemove);
for (RichMember memberToRemove : membersToRemove) {
removeFormerMemberWhileSynchronization(sess, group, memberToRemove, isAuthoritative);
}
List<AttributeDefinition> attrDefs = new ArrayList<>();
// Update members already presented in group
for (Candidate candidate : membersToUpdate.keySet()) {
RichMember memberToUpdate = membersToUpdate.get(candidate);
// Load attrDefinitions just once for first candidate
if (!candidate.getAttributes().isEmpty() && attrDefs.isEmpty()) {
attrDefs = getAttributesToSynchronizeFromCandidates(sess, group, candidate);
}
updateExistingMemberWhileSynchronization(sess, group, candidate, memberToUpdate, overwriteUserAttributesList, mergeMemberAttributesList, attrDefs);
}
// Add not presented candidates to group
Collections.sort(candidatesToAdd);
for (Candidate candidateToAdd : candidatesToAdd) {
addMissingMemberWhileSynchronization(sess, group, candidateToAdd, overwriteUserAttributesList, mergeMemberAttributesList, skippedMembers);
}
long endTime = System.nanoTime();
getPerunBl().getAuditer().log(sess, new GroupSyncFinished(group, startTime, endTime));
log.info("Group synchronization for {} has been finished.", group);
} finally {
closeExtSourcesAfterSynchronization(membersSource, source);
MDC.remove(MDC_LOG_FILE_NAME);
}
return skippedMembers;
}
Aggregations