use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.
the class GroupsManagerBlImpl method saveInformationAboutGroupSynchronization.
private void saveInformationAboutGroupSynchronization(PerunSession sess, Group group, long startTime, boolean failedDueToException, String exceptionMessage) throws AttributeNotExistsException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException, WrongAttributeValueException {
// get current timestamp of this synchronization
Date currentTimestamp = new Date();
String originalExceptionMessage = exceptionMessage;
// If session is null, throw an exception
if (sess == null) {
throw new InternalErrorException("Session is null when trying to save information about synchronization. Group: " + group + ", timestamp: " + currentTimestamp + ",message: " + exceptionMessage);
}
// If group is null, throw an exception
if (group == null) {
throw new InternalErrorException("Object group is null when trying to save information about synchronization. Timestamp: " + currentTimestamp + ", message: " + exceptionMessage);
}
// if exceptionMessage is empty, use "Empty message" instead
if (exceptionMessage != null && exceptionMessage.isEmpty()) {
exceptionMessage = "Empty message.";
// else trim the message on 1000 characters if not null
} else if (exceptionMessage != null && exceptionMessage.length() > 1000) {
exceptionMessage = exceptionMessage.substring(0, 1000) + " ... message is too long, other info is in perun log file. If needed, please ask perun administrators.";
}
// Set correct format of currentTimestamp
String correctTimestampString = BeansUtils.getDateFormatter().format(currentTimestamp);
// Get both attribute definition lastSynchroTimestamp and lastSynchroState
// Get definitions and values, set values
Attribute lastSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationTimestamp"));
Attribute lastSynchronizationState = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationState"));
lastSynchronizationTimestamp.setValue(correctTimestampString);
// if exception is null, set null to value => remove attribute instead of setting in method setAttributes
lastSynchronizationState.setValue(exceptionMessage);
// attributes to set
List<Attribute> attrsToSet = new ArrayList<>();
// Set lastSuccessSynchronizationTimestamp if this one is success
if (exceptionMessage == null) {
String attrName = AttributesManager.NS_GROUP_ATTR_DEF + ":lastSuccessSynchronizationTimestamp";
try {
Attribute lastSuccessSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, attrName));
lastSuccessSynchronizationTimestamp.setValue(correctTimestampString);
attrsToSet.add(lastSuccessSynchronizationTimestamp);
} catch (AttributeNotExistsException ex) {
log.error("Can't save lastSuccessSynchronizationTimestamp, because there is missing attribute with name {}", attrName);
}
// Make string from start of synchronization in correct format
String startOfSyncString = BeansUtils.getDateFormatter().format(new Date(startTime));
try {
// Create attribute with start of last success synchronization timestamp
Attribute startOfLastSuccessSynchronization = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, GroupsManager.GROUP_START_OF_LAST_SUCCESSFUL_SYNC_ATTRNAME));
startOfLastSuccessSynchronization.setValue(startOfSyncString);
attrsToSet.add(startOfLastSuccessSynchronization);
} catch (AttributeNotExistsException ex) {
log.error("Can't save startOfLastSuccessfulSynchronization, because there is missing attribute with name {}", GroupsManager.GROUP_START_OF_LAST_SUCCESSFUL_SYNC_ATTRNAME);
}
} else {
// Log info about synchronization problems to audit log and to the perun system log
if (failedDueToException) {
getPerunBl().getAuditer().log(sess, new GroupSyncFailed(group));
log.debug("{} synchronization failed because of {}", group, originalExceptionMessage);
} else {
getPerunBl().getAuditer().log(sess, new GroupSyncFinishedWithErrors(group));
log.debug("{} synchronization finished with errors: {}", group, originalExceptionMessage);
}
}
// set lastSynchronizationState and lastSynchronizationTimestamp
attrsToSet.add(lastSynchronizationState);
attrsToSet.add(lastSynchronizationTimestamp);
((PerunBl) sess.getPerun()).getAttributesManagerBl().setAttributes(sess, group, attrsToSet);
}
use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.
the class UserSynchronizer method synchronizeUsers.
public void synchronizeUsers() {
PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
ThreadPoolTaskExecutor syncExecutor = new ThreadPoolTaskExecutor();
int poolIndex;
boolean shouldWriteExceptionLog = true;
for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
perunUser[poolIndex] = context.getBean("perunUser", PerunUser.class);
}
try {
log.debug("Getting list of users");
List<User> users = perun.getUsersManagerBl().getUsers(ldapcManager.getPerunSession());
Set<Name> presentUsers = new HashSet<Name>(users.size());
syncExecutor.setCorePoolSize(5);
syncExecutor.setMaxPoolSize(8);
// syncExecutor.setQueueCapacity(30);
syncExecutor.initialize();
poolIndex = 0;
taskCount = new AtomicInteger(0);
for (User user : users) {
presentUsers.add(perunUser[0].getEntryDN(String.valueOf(user.getId())));
log.debug("Getting list of attributes for user {}", user.getId());
List<Attribute> attrs = new ArrayList<Attribute>();
List<String> attrNames = fillPerunAttributeNames(perunUser[poolIndex].getPerunAttributeNames());
try {
// log.debug("Getting attribute {} for user {}", attrName, user.getId());
attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), user, attrNames));
/* very chatty
if(attr == null) {
log.debug("Got null for attribute {}", attrName);
} else if (attr.getValue() == null) {
log.debug("Got attribute {} with null value", attrName);
} else {
log.debug("Got attribute {} with value {}", attrName, attr.getValue().toString());
}
*/
} catch (PerunRuntimeException e) {
log.warn("Couldn't get attributes {} for user {}: {}", attrNames, user.getId(), e.getMessage());
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
log.debug("Got attributes {}", attrNames.toString());
try {
// log.debug("Synchronizing user {} with {} attrs", user, attrs.size());
// perunUser.synchronizeEntry(user, attrs);
log.debug("Getting list of member groups for user {}", user.getId());
Set<Integer> voIds = new HashSet<>();
List<Member> members = perun.getMembersManagerBl().getMembersByUser(ldapcManager.getPerunSession(), user);
List<Group> groups = new ArrayList<Group>();
for (Member member : members) {
if (member.getStatus().equals(Status.VALID)) {
voIds.add(member.getVoId());
groups.addAll(perun.getGroupsManagerBl().getAllGroupsWhereMemberIsActive(ldapcManager.getPerunSession(), member));
}
}
// log.debug("Synchronizing user {} with {} VOs and {} groups", user.getId(), voIds.size(), groups.size());
// perunUser.synchronizeMembership(user, voIds, groups);
log.debug("Getting list of extSources for user {}", user.getId());
List<UserExtSource> userExtSources = perun.getUsersManagerBl().getUserExtSources(ldapcManager.getPerunSession(), user);
List<Group> admin_groups = perun.getUsersManagerBl().getGroupsWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
List<Vo> admin_vos = perun.getUsersManagerBl().getVosWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
List<Facility> admin_facilities = perun.getFacilitiesManagerBl().getFacilitiesWhereUserIsAdmin(ldapcManager.getPerunSession(), user);
// log.debug("Synchronizing user {} with {} extSources", user.getId(), userExtSources.size());
// perunUser.synchronizePrincipals(user, userExtSources);
syncExecutor.execute(new SyncUsersWorker(poolIndex, user, attrs, voIds, groups, userExtSources, admin_groups, admin_vos, admin_facilities));
taskCount.incrementAndGet();
} catch (PerunRuntimeException e) {
log.error("Error synchronizing user", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
poolIndex = (poolIndex + 1) % perunUser.length;
}
try {
removeOldEntries(perunUser[0], presentUsers, log);
} catch (InternalErrorException e) {
log.error("Error removing old user entries", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
} catch (PerunRuntimeException e) {
if (shouldWriteExceptionLog) {
log.error("Error synchronizing users", e);
}
throw new InternalErrorException(e);
} finally {
// wait for all the tasks to get executed
while (!syncExecutor.getThreadPoolExecutor().getQueue().isEmpty()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
// wait for all the tasks to complete (for at most 10 seconds)
for (int i = 0; i < 10 && taskCount.get() > 0; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
syncExecutor.shutdown();
for (poolIndex = 0; poolIndex < perunUser.length; poolIndex++) {
perunUser[poolIndex] = null;
}
}
if (wasThreadException) {
throw new InternalErrorException("Error synchronizing user in executed thread");
}
}
use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.
the class VOSynchronizer method synchronizeVOs.
public void synchronizeVOs() {
PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
boolean shouldWriteExceptionLog = true;
try {
log.debug("Getting list of VOs");
// List<Vo> vos = Rpc.VosManager.getVos(ldapcManager.getRpcCaller());
List<Vo> vos = perun.getVosManagerBl().getVos(ldapcManager.getPerunSession());
Set<Name> presentVos = new HashSet<Name>(vos.size());
for (Vo vo : vos) {
// Map<String, Object> params = new HashMap<String, Object>();
// params.put("vo", new Integer(vo.getId()));
presentVos.add(perunVO.getEntryDN(String.valueOf(vo.getId())));
log.debug("Synchronizing VO entry {}", vo);
log.debug("Getting list of attributes for vo {}", vo.getId());
List<Attribute> attrs = new ArrayList<Attribute>();
List<String> attrNames = fillPerunAttributeNames(perunVO.getPerunAttributeNames());
try {
attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), vo, attrNames));
} catch (PerunRuntimeException e) {
log.warn("Couldn't get attributes {} for vo {}: {}", attrNames, vo.getId(), e.getMessage());
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
log.debug("Got attributes {}", attrNames.toString());
try {
log.debug("Getting list of VO {} members", vo.getId());
// List<Member> members = ldapcManager.getRpcCaller().call("membersManager", "getMembers", params).readList(Member.class);
List<Member> members = perun.getMembersManager().getMembers(ldapcManager.getPerunSession(), vo, Status.VALID);
log.debug("Synchronizing {} members of VO {}", members.size(), vo.getId());
perunVO.synchronizeVo(vo, attrs, members);
} catch (PerunException e) {
log.error("Error synchronizing VO " + vo.getId(), e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
}
// search VO entries in LDAP and remove the ones not present in Perun
try {
removeOldEntries(perunVO, presentVos, log);
} catch (InternalErrorException e) {
log.error("Error removing old VO entries", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
} catch (InternalErrorException e) {
if (shouldWriteExceptionLog) {
log.error("Error getting list of VOs", e);
}
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.
the class GroupSynchronizer method synchronizeGroups.
public void synchronizeGroups() {
PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
boolean shouldWriteExceptionLog = true;
try {
log.debug("Group synchronization - getting list of VOs");
List<Vo> vos = perun.getVosManagerBl().getVos(ldapcManager.getPerunSession());
Set<Name> presentGroups = new HashSet<Name>();
for (Vo vo : vos) {
try {
log.debug("Getting list of groups for VO {}", vo);
List<Group> groups = perun.getGroupsManagerBl().getAllGroups(ldapcManager.getPerunSession(), vo);
for (Group group : groups) {
presentGroups.add(perunGroup.getEntryDN(String.valueOf(vo.getId()), String.valueOf(group.getId())));
log.debug("Synchronizing group {}", group);
log.debug("Getting list of attributes for group {}", group.getId());
List<Attribute> attrs = new ArrayList<Attribute>();
List<String> attrNames = fillPerunAttributeNames(perunGroup.getPerunAttributeNames());
try {
attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), group, attrNames));
} catch (PerunRuntimeException e) {
log.warn("Couldn't get attributes {} for group {}: {}", attrNames, group.getId(), e.getMessage());
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
log.debug("Got attributes {}", attrNames.toString());
try {
log.debug("Getting list of members for group {}", group.getId());
// List<Member> members = ldapcManager.getRpcCaller().call("groupsManager", "getGroupMembers", params).readList(Member.class);
List<Member> members = perun.getGroupsManagerBl().getActiveGroupMembers(ldapcManager.getPerunSession(), group, Status.VALID);
log.debug("Synchronizing {} members of group {}", members.size(), group.getId());
// perunGroup.synchronizeMembers(group, members);
log.debug("Getting list of resources assigned to group {}", group.getId());
// List<Resource> resources = Rpc.ResourcesManager.getAssignedResources(ldapcManager.getRpcCaller(), group);
List<Resource> resources = perun.getResourcesManagerBl().getAssignedResources(ldapcManager.getPerunSession(), group);
log.debug("Synchronizing {} resources assigned to group {}", resources.size(), group.getId());
// perunGroup.synchronizeResources(group, resources);
GroupsManagerBl groupsManager = perun.getGroupsManagerBl();
List<Group> admin_groups = groupsManager.getGroupsWhereGroupIsAdmin(ldapcManager.getPerunSession(), group);
List<Vo> admin_vos = groupsManager.getVosWhereGroupIsAdmin(ldapcManager.getPerunSession(), group);
List<Facility> admin_facilities = groupsManager.getFacilitiesWhereGroupIsAdmin(ldapcManager.getPerunSession(), group);
log.debug("Synchronizing group {} as admin of {} groups, {} VOs and {} facilities", group.getId(), admin_groups.size(), admin_vos.size(), admin_facilities.size());
perunGroup.synchronizeGroup(group, attrs, members, resources, admin_groups, admin_vos, admin_facilities);
} catch (PerunRuntimeException e) {
log.error("Error synchronizing group", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
}
} catch (PerunRuntimeException e) {
if (shouldWriteExceptionLog) {
log.error("Error synchronizing groups", e);
}
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
}
try {
removeOldEntries(perunGroup, presentGroups, log);
} catch (InternalErrorException e) {
log.error("Error removing old group entries", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
} catch (InternalErrorException e) {
if (shouldWriteExceptionLog) {
log.error("Error reading list of VOs", e);
}
throw new InternalErrorException(e);
}
}
use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.
the class ResourceSynchronizer method synchronizeResources.
public void synchronizeResources() {
PerunBl perun = (PerunBl) ldapcManager.getPerunBl();
boolean shouldWriteExceptionLog = true;
try {
log.debug("Resource synchronization - getting list of VOs");
// List<Vo> vos = Rpc.VosManager.getVos(ldapcManager.getRpcCaller());
List<Vo> vos = perun.getVosManagerBl().getVos(ldapcManager.getPerunSession());
Set<Name> presentResources = new HashSet<Name>();
for (Vo vo : vos) {
try {
log.debug("Getting list of resources for VO {}", vo);
// List<Resource> resources = ldapcManager.getRpcCaller().call("resourceManager", "getResources", params).readList(Resource.class);
List<Resource> resources = perun.getResourcesManagerBl().getResources(ldapcManager.getPerunSession(), vo);
for (Resource resource : resources) {
presentResources.add(perunResource.getEntryDN(String.valueOf(vo.getId()), String.valueOf(resource.getId())));
try {
log.debug("Getting list of attributes for resource {}", resource.getId());
List<Attribute> attrs = new ArrayList<Attribute>();
/*
* replaced with single call
*
for(String attrName: fillPerunAttributeNames(perunResource.getPerunAttributeNames())) {
try {
//log.debug("Getting attribute {} for resource {}", attrName, resource.getId());
attrs.add(perun.getAttributesManager().getAttribute(ldapcManager.getPerunSession(), facility, attrName));
} catch (PerunException e) {
log.warn("No attribute {} found for resource {}: {}", attrName, resource.getId(), e.getMessage());
}
}
*/
List<String> attrNames = fillPerunAttributeNames(perunResource.getPerunAttributeNames());
try {
// log.debug("Getting attribute {} for resource {}", attrName, resource.getId());
attrs.addAll(perun.getAttributesManagerBl().getAttributes(ldapcManager.getPerunSession(), resource, attrNames));
} catch (PerunRuntimeException e) {
log.warn("No attributes {} found for resource {}: {}", attrNames, resource.getId(), e.getMessage());
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
log.debug("Got attributes {}", attrs.toString());
log.debug("Synchronizing resource {} with {} attrs", resource, attrs.size());
// perunResource.synchronizeEntry(resource, attrs);
log.debug("Getting list of assigned group for resource {}", resource.getId());
List<Group> assignedGroups = perun.getResourcesManagerBl().getAssignedGroups(ldapcManager.getPerunSession(), resource);
log.debug("Synchronizing {} groups for resource {}", assignedGroups.size(), resource.getId());
// perunResource.synchronizeGroups(resource, assignedGroups);
perunResource.synchronizeResource(resource, attrs, assignedGroups);
} catch (PerunRuntimeException e) {
if (shouldWriteExceptionLog) {
log.error("Error synchronizing resource", e);
}
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
}
} catch (PerunRuntimeException e) {
if (shouldWriteExceptionLog) {
log.error("Error synchronizing resources", e);
}
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
}
try {
removeOldEntries(perunResource, presentResources, log);
} catch (InternalErrorException e) {
log.error("Error removing old resource entries", e);
shouldWriteExceptionLog = false;
throw new InternalErrorException(e);
}
} catch (InternalErrorException e) {
if (shouldWriteExceptionLog) {
log.error("Error getting VO list", e);
}
throw new InternalErrorException(e);
}
}
Aggregations