use of cz.metacentrum.perun.audit.events.EngineIgnoreEvent in project perun by CESNET.
the class EventServiceResolverImpl method resolveEvent.
// ----- methods -------------------------------------
@Override
public Map<Facility, Set<Service>> resolveEvent(AuditEvent event) throws InvalidEventMessageException, ServiceNotExistsException, PrivilegeException {
log.info("Event - I am going to process event: {}", event);
Map<Facility, Set<Service>> result = new HashMap<Facility, Set<Service>>();
if (event instanceof EngineIgnoreEvent) {
log.info("Event ignored {} facilities will be returned", result.size());
return result;
}
// GET All Beans (only PerunBeans) from message
List<PerunBean> listOfBeans = new ArrayList<PerunBean>();
listOfBeans = AuditParser.parseLog(event.getMessage());
// Prepare variables
AttributeDefinition attributeDefinition = null;
Facility facility = null;
Resource resource = null;
Group group = null;
User user = null;
Member member = null;
Service service = null;
Host host = null;
// TODO: What about more than 1 resources, or more than 1 facilities etc. ?
for (PerunBean pb : listOfBeans) {
if (pb instanceof AttributeDefinition) {
attributeDefinition = (AttributeDefinition) 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 (attributeDefinition != null) {
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>();
if (perunSession == null) {
perunSession = perun.getPerunSession(new PerunPrincipal(dispatcherProperties.getProperty("perun.principal.name"), dispatcherProperties.getProperty("perun.principal.extSourceName"), dispatcherProperties.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.warn("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.warn("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.warn("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.warn("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.warn("Host on non-existing facility found while resolving event. Host id={}", host.getId());
} catch (HostNotExistsException ex) {
log.warn("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);
}
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.error("Non-existing resource found while resolving event. Resource={}", r);
// skip to next resource
continue;
}
for (Service s : servicesResolvedFromResource) {
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)) {
Set<Service> servicesToPut = new HashSet<Service>();
servicesToPut.add(s);
result.put(facilityResolvedFromEvent, servicesToPut);
} else {
result.get(facilityResolvedFromEvent).add(s);
}
}
}
log.info("{} facilities will be returned", result.size());
return result;
}
Aggregations