use of alien4cloud.exception.InvalidArgumentException in project alien4cloud by alien4cloud.
the class SuggestionService method setSuggestionIdOnPropertyDefinition.
/**
* Add the suggestion ID of the new suggestionEntry to the appropriate propertyDefinition.
*
* @param suggestionEntry entry of suggestion
*/
public void setSuggestionIdOnPropertyDefinition(SuggestionEntry suggestionEntry) {
Class<? extends AbstractInheritableToscaType> targetClass = (Class<? extends AbstractInheritableToscaType>) alienDAO.getTypesToClasses().get(suggestionEntry.getEsType());
// FIXME what if targetClass is null ?
Object array = toscaTypeSearchService.findAll(targetClass, suggestionEntry.getTargetElementId());
if (array != null) {
int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
AbstractInheritableToscaType targetElement = ((AbstractInheritableToscaType) Array.get(array, i));
PropertyDefinition propertyDefinition = targetElement.getProperties().get(suggestionEntry.getTargetProperty());
if (propertyDefinition == null) {
throw new NotFoundException("Property [" + suggestionEntry.getTargetProperty() + "] not found for element [" + suggestionEntry.getTargetElementId() + "]");
} else {
switch(propertyDefinition.getType()) {
case ToscaTypes.VERSION:
case ToscaTypes.STRING:
propertyDefinition.setSuggestionId(suggestionEntry.getId());
alienDAO.save(targetElement);
break;
case ToscaTypes.LIST:
case ToscaTypes.MAP:
PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
if (entrySchema != null) {
entrySchema.setSuggestionId(suggestionEntry.getId());
alienDAO.save(targetElement);
} else {
throw new InvalidArgumentException("Cannot suggest a list / map type with no entry schema definition");
}
break;
default:
throw new InvalidArgumentException(propertyDefinition.getType() + " cannot be suggested, only property of type string list or map can be suggested");
}
}
}
}
}
use of alien4cloud.exception.InvalidArgumentException in project alien4cloud by alien4cloud.
the class ToscaPropertyFormDescriptorGenerator method doGenerateDescriptor.
private Map<String, Object> doGenerateDescriptor(Set<String> processedDataTypes, PropertyDefinition propertyDefinition, Set<CSARDependency> dependencies) {
Map<String, Object> dataTypeDescriptors;
if (ToscaTypes.isSimple(propertyDefinition.getType())) {
dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
} else if (ToscaTypes.LIST.equals(propertyDefinition.getType())) {
PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
if (entryDefinition == null) {
throw new InvalidArgumentException("List type without entry schema");
}
return generateDescriptorForListType(processedDataTypes, entryDefinition, dependencies);
} else if (ToscaTypes.MAP.equals(propertyDefinition.getType())) {
PropertyDefinition entryDefinition = propertyDefinition.getEntrySchema();
if (entryDefinition == null) {
throw new InvalidArgumentException("Map type without entry schema");
}
dataTypeDescriptors = generateDescriptorForMapType(processedDataTypes, entryDefinition, dependencies);
} else {
DataType dataType = csarRepositorySearchService.getElementInDependencies(DataType.class, propertyDefinition.getType(), dependencies);
if (dataType == null) {
throw new InvalidArgumentException("Data type <" + propertyDefinition.getType() + "> do not exist in dependencies " + dependencies);
}
if (processedDataTypes.add(dataType.getElementId())) {
dataTypeDescriptors = generateDescriptorForDataType(processedDataTypes, dataType, dependencies);
} else {
dataTypeDescriptors = generateDescriptorForSimpleType(propertyDefinition);
}
}
if (propertyDefinition.isRequired()) {
dataTypeDescriptors.put(NOT_NULL_KEY, true);
}
return dataTypeDescriptors;
}
use of alien4cloud.exception.InvalidArgumentException in project alien4cloud by alien4cloud.
the class PropertyService method asFunctionPropertyValue.
public static <T extends AbstractPropertyValue> T asFunctionPropertyValue(Object propertyValue) {
if (propertyValue instanceof Map) {
Map valueAsMap = (Map) propertyValue;
if (valueAsMap.keySet().contains("function") && valueAsMap.keySet().contains("parameters")) {
if (!ToscaFunctionConstants.GET_SECRET.equals(valueAsMap.get("function"))) {
throw new InvalidArgumentException("Property function " + valueAsMap.get("function") + " is invalid");
}
FunctionPropertyValue functionPropertyValue = new FunctionPropertyValue();
functionPropertyValue.setFunction((String) valueAsMap.get("function"));
functionPropertyValue.setParameters((List<String>) valueAsMap.get("parameters"));
return (T) functionPropertyValue;
}
}
return (T) new ComplexPropertyValue((Map<String, Object>) propertyValue);
}
use of alien4cloud.exception.InvalidArgumentException in project alien4cloud by alien4cloud.
the class PropertyValueService method getValueInUnit.
public static String getValueInUnit(String propertyValue, String unit, boolean ceil, String toscaType) {
IPropertyType type = ToscaTypes.fromYamlTypeName(toscaType);
if (type instanceof ScalarType) {
try {
ScalarUnit scalarUnit = ((ScalarType) type).parse(propertyValue);
double convertedValue = scalarUnit.convert(unit);
if (ceil) {
convertedValue = Math.ceil(convertedValue);
}
return format(convertedValue);
} catch (InvalidPropertyValueException e) {
log.error("e");
throw new InvalidArgumentException(e.getMessage());
}
}
throw new InvalidArgumentException("Type is not a scalar type");
}
use of alien4cloud.exception.InvalidArgumentException in project alien4cloud by alien4cloud.
the class ConstraintPropertyService method checkPropertyConstraint.
/**
* Check the constraints on an unwrapped property value (basically a string, map or list) and get events through the given consumer parameter when missing
* properties on complex data type are found.
* Note that the property value cannot be null and the required characteristic of the initial property definition will NOT be checked.
*
* @param propertyName The name of the property.
* @param propertyValue The value of the property to check.
* @param propertyDefinition The property definition that defines the property to check.
* @param missingPropertyConsumer A consumer to receive events when a required property is not defined on a complex type sub-field.
* @throws ConstraintValueDoNotMatchPropertyTypeException In case the value type doesn't match the type of the property as defined.
* @throws ConstraintViolationException In case the value doesn't match one of the constraints defined on the property.
*/
public static void checkPropertyConstraint(String propertyName, Object propertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
Object value = propertyValue;
if (propertyValue instanceof PropertyValue) {
value = ((PropertyValue) propertyValue).getValue();
}
boolean isTypeDerivedFromPrimitive = false;
DataType dataType = null;
String typeName = propertyDefinition.getType();
if (!ToscaTypes.isPrimitive(typeName)) {
dataType = ToscaContext.get(DataType.class, typeName);
if (dataType instanceof PrimitiveDataType) {
// the type is derived from a primitive type
isTypeDerivedFromPrimitive = true;
}
}
if (value instanceof String) {
if (ToscaTypes.isSimple(typeName)) {
checkSimplePropertyConstraint(propertyName, (String) value, propertyDefinition);
} else if (isTypeDerivedFromPrimitive) {
checkComplexPropertyDerivedFromPrimitiveTypeConstraints(propertyName, (String) value, propertyDefinition, dataType);
} else {
throwConstraintValueDoNotMatchPropertyTypeException("Property value is a String while the expected data type is the complex type " + propertyDefinition.getType(), propertyName, propertyDefinition.getType(), value);
}
} else if (value instanceof Map) {
if (ToscaTypes.MAP.equals(typeName)) {
checkMapPropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
} else {
checkDataTypePropertyConstraint(propertyName, (Map<String, Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else if (value instanceof List) {
// Range type is a specific primitive type that is actually wrapped
if (ToscaTypes.RANGE.equals(typeName)) {
checkRangePropertyConstraint(propertyName, (List<Object>) value, propertyDefinition);
} else {
checkListPropertyConstraint(propertyName, (List<Object>) value, propertyDefinition, missingPropertyConsumer);
}
} else {
throw new InvalidArgumentException("Not expecting to receive constraint validation for other types than String, Map or List, but got " + value.getClass().getName());
}
}
Aggregations