use of eu.bcvsolutions.idm.acc.domain.ProvisioningEventType in project CzechIdMng by bcvsolutions.
the class ProvisioningBreakProcessor method process.
@Override
public EventResult<SysProvisioningOperationDto> process(EntityEvent<SysProvisioningOperationDto> event) {
try {
SysProvisioningOperationDto provisioningOperation = event.getContent();
ProvisioningEventType operationType = provisioningOperation.getOperationType();
SysSystemDto system = systemService.get(provisioningOperation.getSystem());
// system may be blocked
boolean blocked = isSystemBlockedOperation(operationType, system);
//
if (blocked) {
// system is already blocked
provisioningOperation = blockOperation(provisioningOperation, system);
event.setContent(provisioningOperation);
return new DefaultEventResult<>(event, this, blocked);
}
//
// try found provisioning break configuration
SysProvisioningBreakConfigDto breakConfig = breakConfigService.getConfig(operationType, system.getId());
if (breakConfig == null) {
LOG.debug("Provisioning break configuration for system name: [{}] and operation: [{}] not found. Global configuration will be used.", system.getCode(), operationType.toString());
breakConfig = breakConfigService.getGlobalBreakConfiguration(operationType);
}
//
if (breakConfig == null) {
LOG.debug("Global configuration for provisioning break isn't found.");
return new DefaultEventResult<>(event, this, blocked);
}
//
if (breakConfig.isDisabled()) {
LOG.debug("Provisioning break configuration id: [{}] for system name: [{}] and operation: [{}] is disabled.", breakConfig.getId(), system.getCode(), operationType.toString());
// break configuration is disable continue
return new DefaultEventResult<>(event, this, blocked);
}
Long currentTimeMillis = System.currentTimeMillis();
//
// get cache for system
SysProvisioningBreakItems cache = breakConfigService.getCacheProcessedItems(system.getId());
// calculate timestamp without period
Long timestampWithoutPeriod = currentTimeMillis - breakConfig.getPeriod(TimeUnit.MILLISECONDS);
// remove older records
cache.removeOlderRecordsThan(operationType, timestampWithoutPeriod);
// get actual count - processed items from timestampWithoutPeriod
int actualCount = cache.getSizeRecordsNewerThan(operationType, timestampWithoutPeriod);
// operation count is sum all previous operation except this operation
if (breakConfig.getWarningLimit() != null && breakConfig.getWarningLimit().equals(actualCount)) {
// disabled may be null
if (breakConfig.getDisableLimit() == null) {
LOG.warn("Block for the system id [{}] and operation [{}] is not set. Operation counter [{}].", provisioningOperation.getSystem(), provisioningOperation.getOperationType().toString(), actualCount);
} else {
LOG.warn("To block the system id [{}] for operation [{}] remains [{}] operations + send message.", provisioningOperation.getSystem(), provisioningOperation.getOperationType().toString(), breakConfig.getDisableLimit() - actualCount);
}
IdmNotificationTemplateDto template = null;
if (breakConfig.getWarningTemplate() == null) {
LOG.debug("Warning template for provisioning break id [{}] missing.", breakConfig.getId());
} else {
template = DtoUtils.getEmbedded(breakConfig, SysProvisioningBreakConfig_.warningTemplate, IdmNotificationTemplateDto.class);
}
//
sendMessage(AccModuleDescriptor.TOPIC_PROVISIONING_BREAK_WARNING, system, actualCount, template, breakConfig, operationType, cache.getDiffBetweenActualAndLast(operationType, currentTimeMillis));
} else if (breakConfig.getDisableLimit() != null && actualCount >= breakConfig.getDisableLimit()) {
// check count is higher than disable limit
// block system for operation
blockSystemForOperation(operationType, system);
//
IdmNotificationTemplateDto template = null;
if (breakConfig.getWarningTemplate() == null) {
LOG.debug("Warning template for provisioning break id [{}] missing.", breakConfig.getId());
} else {
template = DtoUtils.getEmbedded(breakConfig, SysProvisioningBreakConfig_.disableTemplate, IdmNotificationTemplateDto.class);
}
//
sendMessage(AccModuleDescriptor.TOPIC_PROVISIONING_BREAK_DISABLE, system, actualCount, template, breakConfig, operationType, cache.getDiffBetweenActualAndLast(operationType, currentTimeMillis));
//
LOG.warn("System id: [{}] will be blocked for operation: [{}].", provisioningOperation.getSystem(), operationType.toString());
provisioningOperation = blockOperation(provisioningOperation, system);
blocked = true;
} else if (breakConfig.getWarningLimit() != null && actualCount > breakConfig.getWarningLimit()) {
// after overrun warning limit, isn't send any another notification - add at least log
if (breakConfig.getDisableLimit() == null) {
LOG.warn("Block for the system id [{}] and operation [{}] is not set. Operation counter [{}].", provisioningOperation.getSystem(), provisioningOperation.getOperationType().toString(), actualCount);
} else {
LOG.warn("To block the system id [{}] for operation [{}] remains [{}] operations.", provisioningOperation.getSystem(), provisioningOperation.getOperationType().toString(), breakConfig.getDisableLimit() - actualCount);
}
}
// remove all unless items in cache
cache.addItem(operationType, currentTimeMillis);
breakConfigService.saveCacheProcessedItems(provisioningOperation.getSystem(), cache);
//
event.setContent(provisioningOperation);
return new DefaultEventResult<>(event, this, blocked);
} catch (Exception ex) {
LOG.error("Unexpect error while evaluate provisioning break.", ex);
throw new ProvisioningException(AccResultCode.PROVISIONING_FAILED, ex);
}
}
use of eu.bcvsolutions.idm.acc.domain.ProvisioningEventType in project CzechIdMng by bcvsolutions.
the class ProvisioningBreakProcessorTest method testGlobalConfigurationSettings.
@Test
public void testGlobalConfigurationSettings() {
IdmIdentityDto recipient = testHelper.createIdentity();
IdmIdentityDto recipient2 = testHelper.createIdentity();
IdmRoleDto roleRecipient = testHelper.createRole();
//
createGlobalConfiguration(ProvisioningBreakConfiguration.GLOBAL_BREAK_DELETE_OPERATION, false, 2, 3, 20l, null, roleRecipient);
String prefix = ProvisioningBreakConfiguration.GLOBAL_BREAK_PREFIX + ProvisioningBreakConfiguration.GLOBAL_BREAK_DELETE_OPERATION;
configurationService.setValue(prefix + ProvisioningBreakConfiguration.PROPERTY_IDENTITY_RECIPIENTS, recipient.getUsername() + ", " + recipient2.getUsername());
//
// check non existing configuration
ProvisioningEventType eventType = ProvisioningEventType.UPDATE;
Object value = provisioningBreakConfiguration.getDisabled(eventType);
assertNull(value);
value = provisioningBreakConfiguration.getDisableLimit(eventType);
assertNull(value);
List<IdmIdentityDto> recipients = provisioningBreakConfiguration.getIdentityRecipients(eventType);
assertTrue(recipients.isEmpty());
value = provisioningBreakConfiguration.getPeriod(eventType);
assertNull(value);
//
// check existing
eventType = ProvisioningEventType.DELETE;
Boolean disabled = provisioningBreakConfiguration.getDisabled(eventType);
Integer warningLimit = provisioningBreakConfiguration.getWarningLimit(eventType);
Integer disableLimit = provisioningBreakConfiguration.getDisableLimit(eventType);
Long period = provisioningBreakConfiguration.getPeriod(eventType);
List<IdmIdentityDto> identityRecipients = provisioningBreakConfiguration.getIdentityRecipients(eventType);
List<IdmRoleDto> roleRecipients = provisioningBreakConfiguration.getRoleRecipients(eventType);
//
assertEquals(Boolean.FALSE, disabled);
assertEquals(Integer.valueOf(2), warningLimit);
assertEquals(Integer.valueOf(3), disableLimit);
assertEquals(Long.valueOf(20l), period);
assertEquals(2, identityRecipients.size());
assertEquals(1, roleRecipients.size());
//
IdmIdentityDto foundedRecipient = identityRecipients.stream().filter(rec -> rec.getUsername().equals(recipient.getUsername())).findFirst().get();
assertNotNull(foundedRecipient);
assertEquals(recipient.getId(), foundedRecipient.getId());
//
foundedRecipient = identityRecipients.stream().filter(rec -> rec.getUsername().equals(recipient2.getUsername())).findFirst().get();
assertNotNull(foundedRecipient);
assertEquals(recipient2.getId(), foundedRecipient.getId());
//
IdmRoleDto foundedRole = roleRecipients.get(0);
assertNotNull(foundedRole);
assertEquals(roleRecipient.getId(), foundedRole.getId());
}
Aggregations