Search in sources :

Example 6 with InvalidArgumentException

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");
                }
            }
        }
    }
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) NotFoundException(alien4cloud.exception.NotFoundException) AbstractInheritableToscaType(org.alien4cloud.tosca.model.types.AbstractInheritableToscaType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint)

Example 7 with InvalidArgumentException

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;
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 8 with InvalidArgumentException

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);
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map)

Example 9 with InvalidArgumentException

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");
}
Also used : InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) InvalidPropertyValueException(org.alien4cloud.tosca.exceptions.InvalidPropertyValueException) ScalarType(org.alien4cloud.tosca.normative.types.ScalarType) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) ScalarUnit(org.alien4cloud.tosca.normative.primitives.ScalarUnit)

Example 10 with InvalidArgumentException

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());
    }
}
Also used : PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) InvalidArgumentException(alien4cloud.exception.InvalidArgumentException) PropertyValue(org.alien4cloud.tosca.model.definitions.PropertyValue) DataType(org.alien4cloud.tosca.model.types.DataType) PrimitiveDataType(org.alien4cloud.tosca.model.types.PrimitiveDataType) List(java.util.List) Map(java.util.Map)

Aggregations

InvalidArgumentException (alien4cloud.exception.InvalidArgumentException)10 Map (java.util.Map)4 NotFoundException (alien4cloud.exception.NotFoundException)2 List (java.util.List)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 DataType (org.alien4cloud.tosca.model.types.DataType)2 PrimitiveDataType (org.alien4cloud.tosca.model.types.PrimitiveDataType)2 AuditedMethod (alien4cloud.audit.model.AuditedMethod)1 Method (alien4cloud.audit.model.Method)1 INodeMatcherPlugin (alien4cloud.deployment.matching.plugins.INodeMatcherPlugin)1 MatchingConfiguration (alien4cloud.model.deployment.matching.MatchingConfiguration)1 CsarGitRepository (alien4cloud.model.git.CsarGitRepository)1 LocationResources (alien4cloud.model.orchestrators.locations.LocationResources)1 ServiceResource (alien4cloud.model.service.ServiceResource)1 Group (alien4cloud.security.model.Group)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 HashMap (java.util.HashMap)1 InvalidPropertyValueException (org.alien4cloud.tosca.exceptions.InvalidPropertyValueException)1 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)1