throw new BadArgumentException("Field 'category' [" + conditionRest.getCategory() + "] is invalid. Allowed values "+
"are : " + allowedValues);
}
int measurementDefinition = conditionRest.getMeasurementDefinition();
MeasurementDefinition md;
if (measurementDefinition!=0) {
md = entityManager.find(MeasurementDefinition.class, measurementDefinition);
if (md==null) {
throw new StuffNotFoundException("measurementDefinition with id " + measurementDefinition);
}
// Validate that the definition belongs to the resource, if passed
if (resource!=null) {
ResourceType type = resource.getResourceType();
Set<MeasurementDefinition> definitions = type.getMetricDefinitions();
if (!definitions.contains(md)) {
throw new BadArgumentException("MeasurementDefinition does not apply to resource");
}
}
// Validate that the definition belongs to the passed resource type
if (resourceType!=null) {
Set<MeasurementDefinition> definitions = resourceType.getMetricDefinitions();
if (!definitions.contains(md)) {
throw new BadArgumentException("MeasurementDefinition does not apply to resource type");
}
}
}
String optionValue = conditionRest.getOption();
String conditionName = conditionRest.getName();
// Set the name for all cases and allow it to be overridden later.
condition.setName(conditionName);
AlertConditionCategory category = condition.getCategory();
switch (category) {
case ALERT:
// Looks internal -- noting to do.
break;
case AVAIL_DURATION:
if (optionValue ==null) {
throw new BadArgumentException("Option needs to be provided as duration in seconds");
}
try {
Integer.parseInt(optionValue);
} catch (NumberFormatException nfe) {
throw new BadArgumentException("Option provided [" + optionValue + "] was bad. Must be duration in seconds");
}
checkForAllowedValues("name", conditionName, "AVAIL_DURATION_DOWN", "AVAIL_DURATION_NOT_UP");
break;
case AVAILABILITY:
checkForAllowedValues("name", conditionName, "AVAIL_GOES_DOWN", "AVAIL_GOES_DISABLED",
"AVAIL_GOES_UNKNOWN", "AVAIL_GOES_NOT_UP", "AVAIL_GOES_UP");
break;
case BASELINE:
if (measurementDefinition ==0) {
throw new BadArgumentException("You need to provide a measurementDefinition for category BASELINE");
}
md = entityManager.find(MeasurementDefinition.class,
measurementDefinition);
if (md==null) {
throw new StuffNotFoundException("measurementDefinition with id " + measurementDefinition);
}
condition.setMeasurementDefinition(md);
condition.setName(md.getDisplayName());
checkForAllowedValues("option", optionValue, "min", "max", "mean");
checkForAllowedValues("comparator", conditionRest.getComparator(), "<", "=", ">");
break;
case CHANGE:
md = getMeasurementDefinition(measurementDefinition, category);
condition.setMeasurementDefinition(md);
condition.setName(md.getDisplayName());
if (md.getDataType()== DataType.CALLTIME) {
checkForAllowedValues("option", optionValue, "MIN", "MAX", "AVG");
}
break;
case CONTROL:
checkForAllowedValues("option",optionValue,"INPROGRESS", "SUCCESS", "FAILURE", "CANCELED");
if (conditionName ==null) {
throw new BadArgumentException("name must be the name (not display name) of a valid operation.");
}
// TODO check for valid operation -- only on the resource or type itself (still hard enough)
break;
case DRIFT:
// option and name are optional, so nothing to do
break;
case EVENT:
checkForAllowedValues("name", conditionName,"DEBUG", "INFO", "WARN", "ERROR", "FATAL");
// option is an optional regular expression
break;
case RANGE:
checkForAllowedValues("comparator", conditionRest.getComparator(), "<", "=", ">","<=",">=");
if (optionValue==null) {
throw new BadArgumentException("You need to supply an upper threshold in 'option' as numeric value");
}
try {
Double.parseDouble(optionValue);
}
catch (NumberFormatException nfe) {
throw new BadArgumentException("You need to supply an upper threshold in 'option' as numeric value");
}
md = getMeasurementDefinition(measurementDefinition, category);
condition.setMeasurementDefinition(md);
condition.setName(md.getDisplayName());
break;
case RESOURCE_CONFIG:
// Nothing to do
break;
case THRESHOLD:
checkForAllowedValues("comparator", conditionRest.getComparator(), "<", "=", ">");
md = getMeasurementDefinition(measurementDefinition, category);
condition.setMeasurementDefinition(md);
condition.setName(md.getDisplayName());
if (md.getDataType()== DataType.CALLTIME) {
checkForAllowedValues("option", optionValue, "MIN", "MAX", "AVG");
}
break;
case TRAIT:
md = getMeasurementDefinition(measurementDefinition, category);
condition.setMeasurementDefinition(md);
condition.setName(md.getDisplayName());
// No need to check options - they are optional
break;
}