Search in sources :

Example 1 with GroupSyncStarted

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;
}
Also used : Candidate(cz.metacentrum.perun.core.api.Candidate) GroupSyncFinished(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFinished) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) GroupSyncStarted(cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncStarted) RichMember(cz.metacentrum.perun.core.api.RichMember) RichUserExtSource(cz.metacentrum.perun.core.api.RichUserExtSource) ExtSource(cz.metacentrum.perun.core.api.ExtSource) UserExtSource(cz.metacentrum.perun.core.api.UserExtSource) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Aggregations

GroupSyncFinished (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncFinished)1 GroupSyncStarted (cz.metacentrum.perun.audit.events.GroupManagerEvents.GroupSyncStarted)1 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)1 Candidate (cz.metacentrum.perun.core.api.Candidate)1 ExtSource (cz.metacentrum.perun.core.api.ExtSource)1 RichMember (cz.metacentrum.perun.core.api.RichMember)1 RichUserExtSource (cz.metacentrum.perun.core.api.RichUserExtSource)1 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1