private AlertDefinition injectAlertDef(InjectedTemplate injectedAlertDef, boolean replaceIfExists) {
AlertDefinition result = null;
ResourceTypeManagerLocal typeManager = LookupUtil.getResourceTypeManager();
AlertDefinitionManagerLocal alertDefManager = LookupUtil.getAlertDefinitionManager();
SubjectManagerLocal subjectManager = LookupUtil.getSubjectManager();
ResourceTypeCriteria rtc = new ResourceTypeCriteria();
rtc.addFilterPluginName(injectedAlertDef.getPluginName());
rtc.addFilterName(injectedAlertDef.getResourceTypeName());
rtc.fetchMetricDefinitions(true);
List<ResourceType> resourceTypes = typeManager.findResourceTypesByCriteria(subjectManager.getOverlord(), rtc);
if (resourceTypes.isEmpty()) {
return result;
}
assert 1 == resourceTypes.size() : "Found more than 1 resource type!";
ResourceType resourceType = resourceTypes.get(0);
AlertDefinitionCriteria adc = new AlertDefinitionCriteria();
adc.addFilterName(injectedAlertDef.getName());
adc.addFilterAlertTemplateResourceTypeId(resourceType.getId());
List<AlertDefinition> alertDefs = alertDefManager.findAlertDefinitionsByCriteria(subjectManager.getOverlord(),
adc);
if (!alertDefs.isEmpty()) {
assert 1 == alertDefs.size() : "Found more than 1 existing alert def!";
if (!replaceIfExists) {
return result;
}
int[] alertDefIdArray = new int[1];
alertDefIdArray[0] = alertDefs.get(0).getId();
alertDefManager.removeAlertDefinitions(subjectManager.getOverlord(), alertDefIdArray);
}
int newAlertDefId = 0;
if (storageNodeHighHeapTemplate.equals(injectedAlertDef)) {
newAlertDefId = injectStorageNodeHighHeapTemplate(resourceType);
} else if (storageNodeHighDiskUsageTemplate.equals(injectedAlertDef)) {
newAlertDefId = injectStorageNodeHighDiskUsageTemplate(resourceType);
} else if (storageNodeSnapshotFailureTemplate.equals(injectedAlertDef)) {
newAlertDefId = injectStorageNodeSnapshotFailureTemplate(resourceType);
} else if (storageNodeMaintenanceOperationsFailureTemplate.equals(injectedAlertDef)) {
newAlertDefId = injectStorageNodeMaintenanceOperationsFailureTemplate(resourceType);
}
adc.addFilterId(newAlertDefId);
alertDefs = alertDefManager.findAlertDefinitionsByCriteria(subjectManager.getOverlord(), adc);
assert 1 == alertDefs.size() : "Found more than 1 new alert def!";
result = alertDefs.get(0);
return result;
}