use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class Metacentrum method approveApplication.
/**
* Add all new Metacentrum members to "storage" group.
*/
@Override
public Application approveApplication(PerunSession session, Application app) throws PerunException {
// get perun from session
Perun perun = session.getPerun();
if (Application.AppType.INITIAL.equals(app.getType())) {
Vo vo = app.getVo();
User user = app.getUser();
Group group = perun.getGroupsManager().getGroupByName(session, vo, "storage");
Member mem = perun.getMembersManager().getMemberByUser(session, vo, user);
try {
perun.getGroupsManager().addMember(session, group, mem);
} catch (AlreadyMemberException ex) {
}
}
// Support statistic groups
String statisticGroupName = "";
List<ApplicationFormItemData> formData = registrar.getApplicationDataById(session, app.getId());
for (ApplicationFormItemData item : formData) {
if (Objects.equals("urn:perun:user:attribute-def:def:researchGroupStatistic", item.getFormItem().getPerunDestinationAttribute())) {
statisticGroupName = item.getValue();
break;
}
}
if (statisticGroupName != null && !statisticGroupName.isEmpty()) {
Group group;
try {
group = perun.getGroupsManager().getGroupByName(session, app.getVo(), statisticGroupName);
} catch (GroupNotExistsException ex) {
// user filled non existing group, just skip adding
return app;
} catch (InternalErrorException ex) {
// wrong group name
return app;
}
Attribute isStatisticGroup = perun.getAttributesManager().getAttribute(session, group, "urn:perun:group:attribute-def:def:statisticGroup");
Attribute isStatisticGroupAutoFill = perun.getAttributesManager().getAttribute(session, group, "urn:perun:group:attribute-def:def:statisticGroupAutoFill");
boolean statisticGroup = (isStatisticGroup.getValue() != null) ? (Boolean) isStatisticGroup.getValue() : false;
boolean statisticGroupAutoFill = (isStatisticGroupAutoFill.getValue() != null) ? (Boolean) isStatisticGroupAutoFill.getValue() : false;
if (statisticGroup && statisticGroupAutoFill) {
try {
Member mem = perun.getMembersManager().getMemberByUser(session, app.getVo(), app.getUser());
perun.getGroupsManager().addMember(session, group, mem);
} catch (AlreadyMemberException ex) {
}
}
}
return app;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class EventExecServiceResolverImpl method parseEvent.
@Override
public Map<Facility, Set<ExecService>> parseEvent(String event) throws InvalidEventMessageException, ServiceNotExistsException, InternalErrorException, PrivilegeException {
log.info("I am going to process event:" + event);
/**
* Expected string format as on:
* https://projekty.ics.muni.cz/perunv3/trac
* /wiki/PerunEngineDispatcherController event|x|[timestamp][Event
* header][Event data]
*/
String eventParsingPattern = "^\\[([a-zA-Z0-9+: ]+)\\]\\[([^\\]]+)\\]\\[(.*)\\]$";
Pattern pattern = Pattern.compile(eventParsingPattern);
Matcher matcher = pattern.matcher(event);
boolean matchFound = matcher.find();
if (matchFound) {
log.debug("Message format matched ok...");
// NOT USED ANYMORE: not applicable in dispatcher
// String thisEngineID = matcher.group(1);
// // This should indeed match the current Engine instance ID, so
// let's compare it...
// if (Integer.parseInt(thisEngineID) != Integer.parseInt((String)
// propertiesBean.get("engine.unique.id"))) {
// throw new InvalidEventMessageException("Wrong Engine ID. Was:" +
// thisEngineID + ", Expected:" +
// propertiesBean.get("engine.unique.id"));
// }
// // Not being used at the moment.
// String timeStamp = matcher.group(2);
// Header should provide information regarding the target facility.
String eventHeader = matcher.group(2);
// We expect the string to contain something like this:
// facility.id=2 ???
// String headerParsingPattern = ".*facility.id\\=([0-9]+).*";
// Pattern headerPattern = Pattern.compile(headerParsingPattern);
// Matcher headerMatcher = headerPattern.matcher(eventHeader);
/*
* boolean headerMatchFound = headerMatcher.find();
* if(!headerMatchFound) { throw new InvalidEventMessageException(
* "Invalid event header. It does not contain the expected facility.id=value..."
* ); } int facilityId = Integer.parseInt(matcher.group(1));
* PerunSession perunSession =
* engineManager.getPerunSession(propertiesBean
* .getProperty("perun.principal")); Facility facility = null; try {
* facility = facilitiesManager.getFacilityById(perunSession,
* facilityId); } catch (FacilityNotExistsException e) { throw new
* InvalidEventMessageException
* ("Facility with ID "+facilityId+"does not exist.", e); } catch
* (InternalErrorException e) { throw new
* InvalidEventMessageException("Unknown error...", e); } catch
* (PrivilegeException e) { throw new
* InvalidEventMessageException("Principal "
* +propertiesBean.getProperty
* ("perun.principal")+" is not allowed to access that facility. ",
* e); }
*/
// Data should provide information regarding the target ExecService
// (Processing rule).
String eventData = matcher.group(3);
log.debug("Event data to be parsed:" + eventData);
// GET All Beans (only PerunBeans) from message
List<PerunBean> listOfBeans = new ArrayList<PerunBean>();
listOfBeans = AuditParser.parseLog(eventData);
// Prepare variables
AttributeDefinition attributeDefinition = null;
Attribute attribute = null;
Facility facility = null;
Resource resource = null;
Group group = null;
User user = null;
Member member = null;
Service service = null;
Host host = null;
// etc. ?
for (PerunBean pb : listOfBeans) {
if (pb instanceof AttributeDefinition && pb instanceof Attribute) {
attribute = (Attribute) pb;
} else if (pb instanceof Facility) {
facility = (Facility) pb;
} else if (pb instanceof Resource) {
resource = (Resource) pb;
} else if (pb instanceof Group) {
group = (Group) pb;
} else if (pb instanceof User) {
user = (User) pb;
} else if (pb instanceof Member) {
member = (Member) pb;
} else if (pb instanceof Service) {
service = (Service) pb;
} else if (pb instanceof Host) {
host = (Host) pb;
}
}
// If there is any attribute, so create AttributeDefinition
if (attribute != null) {
attributeDefinition = new AttributeDefinition(attribute);
log.debug("Attribute found in event. {}.", attributeDefinition);
}
List<Facility> facilitiesResolvedFromEvent = new ArrayList<Facility>();
List<Resource> resourcesResolvedFromEvent = new ArrayList<Resource>();
List<Service> servicesResolvedFromEvent = new ArrayList<Service>();
// =============== Resolve facilities from event======================
PerunSession perunSession = perun.getPerunSession(new PerunPrincipal(dispatcherPropertiesBean.getProperty("perun.principal.name"), dispatcherPropertiesBean.getProperty("perun.principal.extSourceName"), dispatcherPropertiesBean.getProperty("perun.principal.extSourceType")), new PerunClient());
// Try to find FACILITY in event
if (facility != null) {
try {
log.debug("Facility found in event. {}.", facility);
facilitiesResolvedFromEvent.add(facility);
resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
} catch (FacilityNotExistsException ex) {
log.debug("Non-existing facility found while resolving event. id={}", facility.getId());
}
} else {
// Try to find RESOURCE in event
if (resource != null) {
resourcesResolvedFromEvent.add(resource);
} else {
// Try to find GROUP in event
if (group != null) {
try {
resourcesResolvedFromEvent = perun.getResourcesManager().getAssignedResources(perunSession, group);
} catch (GroupNotExistsException ex) {
log.debug("Non-existing group found while resolving event. id={}", group.getId());
}
} else {
// try to find USER in event
if (user != null) {
try {
resourcesResolvedFromEvent = perun.getUsersManager().getAllowedResources(perunSession, user);
} catch (UserNotExistsException ex) {
log.debug("Non-existing user found while resolving event. id={}", user.getId());
}
} else {
// try to find MEMBER in event
if (member != null) {
try {
resourcesResolvedFromEvent = perun.getResourcesManager().getAllowedResources(perunSession, member);
} catch (MemberNotExistsException ex) {
log.debug("Non-existing member found while resolving event. id={}", member.getId());
}
} else {
// try to find HOST in event
if (host != null) {
try {
log.debug("Host found in event.id= {}.", host.getId());
facility = perun.getFacilitiesManager().getFacilityForHost(perunSession, host);
facilitiesResolvedFromEvent.add(facility);
resourcesResolvedFromEvent.addAll(perun.getFacilitiesManager().getAssignedResources(perunSession, facility));
} catch (FacilityNotExistsException ex) {
log.debug("Host on non-existing facility found while resolving event. Host id={}", host.getId());
} catch (HostNotExistsException ex) {
log.debug("Non-existing host found while resolving event. id={}", host.getId());
}
} else {
log.warn("No match found for this event. Event={}", event);
}
}
}
}
}
}
// TODO resolve more than one service
if (service != null) {
servicesResolvedFromEvent.add(service);
}
//List<Pair<List<ExecService>, Facility>> pairs = new ArrayList<Pair<List<ExecService>, Facility>>();
Map<Facility, Set<ExecService>> result = new HashMap<Facility, Set<ExecService>>();
for (Resource r : resourcesResolvedFromEvent) {
Facility facilityResolvedFromEvent;
List<Service> servicesResolvedFromResource;
try {
facilityResolvedFromEvent = perun.getResourcesManager().getFacility(perunSession, r);
servicesResolvedFromResource = perun.getResourcesManager().getAssignedServices(perunSession, r);
// process only services resolved from event if any
if (!servicesResolvedFromEvent.isEmpty())
servicesResolvedFromResource.retainAll(servicesResolvedFromEvent);
} catch (ResourceNotExistsException ex) {
log.debug("Non-existing resource found while resolving event. Resource={}", r);
// skip to next resource
continue;
}
for (Service s : servicesResolvedFromResource) {
// TODO: Optimize with an SQL query...
List<ExecService> execServicesGenAndSend = generalServiceManager.listExecServices(perunSession, s.getId());
List<ExecService> execServices = new ArrayList<ExecService>();
for (ExecService execService : execServicesGenAndSend) {
if (execService.getExecServiceType().equals(ExecServiceType.SEND)) {
execServices.add(execService);
}
}
if (attributeDefinition != null) {
// remove from future processing services
// which don't require the found attribute
// TODO (CHECKME) This method can raise
// ServiceNotExistsException. Is it ok? Or it must be
// catch?
List<AttributeDefinition> serviceRequiredAttributes = perun.getAttributesManager().getRequiredAttributesDefinition(perunSession, s);
if (!serviceRequiredAttributes.contains(attributeDefinition))
continue;
}
if (!result.containsKey(facilityResolvedFromEvent)) {
result.put(facilityResolvedFromEvent, new HashSet<ExecService>(execServices));
} else {
result.get(facilityResolvedFromEvent).addAll(execServices);
}
}
}
log.info("I am going to return " + result.size() + " facilities.");
return result;
} else {
throw new InvalidEventMessageException("Message[" + event + "]");
}
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class GroupsManagerImpl method updateGroup.
public Group updateGroup(PerunSession sess, Group group) throws InternalErrorException {
Utils.notNull(group.getName(), "group.getName()");
// Get the group stored in the DB
Group dbGroup;
try {
dbGroup = this.getGroupById(sess, group.getId());
} catch (GroupNotExistsException e) {
throw new InternalErrorException("Group existence was checked at the higher level", e);
}
// we allow only update on shortName part of name
if (!dbGroup.getShortName().equals(group.getShortName())) {
dbGroup.setShortName(group.getShortName());
try {
jdbc.update("update groups set name=?,modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", dbGroup.getName(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), dbGroup.getId());
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
if (group.getDescription() != null && !group.getDescription().equals(dbGroup.getDescription())) {
try {
jdbc.update("update groups set dsc=?, modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", group.getDescription(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), group.getId());
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
dbGroup.setDescription(group.getDescription());
}
return dbGroup;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class GroupsManagerImpl method updateGroupName.
public Group updateGroupName(PerunSession sess, Group group) throws InternalErrorException {
Utils.notNull(group.getName(), "group.getName()");
// Get the group stored in the DB
Group dbGroup;
try {
dbGroup = this.getGroupById(sess, group.getId());
} catch (GroupNotExistsException e) {
throw new InternalErrorException("Group existence was checked at the higher level", e);
}
if (!dbGroup.getName().equals(group.getName())) {
dbGroup.setName(group.getName());
try {
jdbc.update("update groups set name=?,modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", dbGroup.getName(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), dbGroup.getId());
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
return dbGroup;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method insertToMemberGroup.
public void insertToMemberGroup(PerunSession sess, Member member, Vo vo) throws InternalErrorException, AlreadyMemberException, GroupOperationsException {
// Insert member into the members group
try {
getPerunBl().getVosManagerBl().checkVoExists(sess, vo);
Group g = getPerunBl().getGroupsManagerBl().getGroupByName(sess, vo, VosManager.MEMBERS_GROUP);
getPerunBl().getGroupsManagerBl().addMemberToMembersGroup(sess, g, member);
} catch (NotMemberOfParentGroupException ex) {
//members group is top level -> this should not happen
throw new ConsistencyErrorException(ex);
} catch (GroupNotExistsException e) {
throw new InternalErrorException(e);
} catch (VoNotExistsException e) {
throw new InternalErrorException(e);
} catch (WrongAttributeValueException e) {
//Member is not valid, so he couldn't have truly required atributes, neither he couldn't have influence on user attributes
throw new ConsistencyErrorException(e);
} catch (WrongReferenceAttributeValueException e) {
//Member is not valid, so he couldn't have truly required atributes, neither he couldn't have influence on user attributes
throw new ConsistencyErrorException(e);
}
}
Aggregations