Search in sources :

Example 66 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class PojoFormDescriptorGenerator method doParseClass.

private void doParseClass(Class<?> clazz, Map<String, Object> descriptors) {
    Map<String, Object> propertyTypes = Maps.newHashMap();
    descriptors.put(PROPERTY_TYPE_KEY, propertyTypes);
    PropertyDescriptor[] properties = ReflectionUtil.getPropertyDescriptors(clazz);
    String[] orderedPropertiesNames;
    Set<String> propertiesNames;
    FormProperties formPropAnnotation = clazz.getAnnotation(FormProperties.class);
    if (formPropAnnotation != null && formPropAnnotation.value() != null) {
        orderedPropertiesNames = formPropAnnotation.value();
        propertiesNames = Sets.newHashSet(orderedPropertiesNames);
    } else {
        propertiesNames = Sets.newLinkedHashSet();
        for (PropertyDescriptor property : properties) {
            if (property.getReadMethod() != null && property.getWriteMethod() != null) {
                propertiesNames.add(property.getName());
            }
        }
        orderedPropertiesNames = propertiesNames.toArray(new String[propertiesNames.size()]);
        if (log.isDebugEnabled()) {
            log.debug("Class " + clazz.getName() + " do not have FormProperties annotation, all properties will be read and order will not be assured");
        }
    }
    for (PropertyDescriptor property : properties) {
        if ((!propertiesNames.contains(property.getName())) || property.getReadMethod() == null || property.getWriteMethod() == null) {
            continue;
        }
        Class<?> propClazz = property.getPropertyType();
        Map<String, FormType> implementations = getImplementations(clazz, property);
        Map<String, FormType> contentImplementations = getContentImplementations(clazz, property);
        PropertyDefinition propertyDefinition = getPropertyDefinition(clazz, property);
        String customFormType = getCustomFormType(clazz, property);
        String[] validValues = getValidValues(clazz, property);
        String label = getLabel(clazz, property);
        Map<String, Object> type = Maps.newHashMap();
        if (customFormType != null) {
            // Custom type that cannot be processed in a generic way on ui side
            type.put(TYPE_KEY, customFormType);
        } else if (propertyDefinition != null) {
            type.put(TYPE_KEY, TOSCA_TYPE);
            type.put(TOSCA_DEFINITION_KEY, propertyDefinition);
            if (propertyDefinition.isRequired()) {
                type.put(NOT_NULL_KEY, true);
            }
        } else if (isPrimitive(propClazz)) {
            // Primitive type
            type = buildSimpleTypeDescriptor(propClazz);
            if (validValues != null && validValues.length > 0) {
                type.put(VALID_VALUES_KEY, validValues);
            }
        } else if (Map.class.isAssignableFrom(propClazz)) {
            // Map type
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(MAP_TYPE, contentImplementations);
            } else {
                ParameterizedType parameterizedType = (ParameterizedType) property.getReadMethod().getGenericReturnType();
                Type[] types = parameterizedType.getActualTypeArguments();
                Class<?> keyClass = (Class<?>) types[0];
                Class<?> valueClass = (Class<?>) types[1];
                if (keyClass != String.class) {
                    throw new FormDescriptorGenerationException("Cannot generate meta data for map with key not of type String");
                }
                type = buildSequenceTypeDescriptor(MAP_TYPE, valueClass);
            }
        } else if (List.class.isAssignableFrom(propClazz) || Set.class.isAssignableFrom(propClazz)) {
            // List or set types
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(ARRAY_TYPE, contentImplementations);
            } else {
                ParameterizedType pt = (ParameterizedType) property.getReadMethod().getGenericReturnType();
                Type[] types = pt.getActualTypeArguments();
                Class<?> valueClass = (Class<?>) types[0];
                type = buildSequenceTypeDescriptor(ARRAY_TYPE, valueClass);
            }
        } else if (propClazz.isArray()) {
            // Array type
            if (contentImplementations != null && !contentImplementations.isEmpty()) {
                type = buildSequenceAbstractTypeDescriptor(ARRAY_TYPE, contentImplementations);
            } else {
                Class<?> valueClass = property.getReadMethod().getReturnType().getComponentType();
                type = buildSequenceTypeDescriptor(ARRAY_TYPE, valueClass);
            }
        } else if (implementations != null && !implementations.isEmpty()) {
            type = buildAbstractTypeDescriptor(implementations);
        } else {
            // Complex type
            Class<?> valueClass = property.getReadMethod().getReturnType();
            type = buildComplexTypeDescriptor(valueClass);
        }
        Map<String, Object> suggestion = getSuggestion(clazz, property);
        if (suggestion != null) {
            type.put(SUGGESTION_KEY, suggestion);
        }
        if (isNotNull(clazz, property)) {
            type.put(NOT_NULL_KEY, true);
        }
        if (isPassword(clazz, property)) {
            type.put(IS_PASSWORD_KEY, true);
        }
        if (label != null) {
            type.put(LABEL_KEY, label);
        }
        propertyTypes.put(property.getName(), type);
    }
    descriptors.put(TYPE_KEY, COMPLEX_TYPE);
    descriptors.put(ORDER_KEY, orderedPropertiesNames);
}
Also used : Set(java.util.Set) PropertyDescriptor(java.beans.PropertyDescriptor) FormDescriptorGenerationException(alien4cloud.ui.form.exception.FormDescriptorGenerationException) FormProperties(alien4cloud.ui.form.annotation.FormProperties) FormType(alien4cloud.ui.form.annotation.FormType) FormPropertyDefinition(alien4cloud.ui.form.annotation.FormPropertyDefinition) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) ParameterizedType(java.lang.reflect.ParameterizedType) FormType(alien4cloud.ui.form.annotation.FormType) FormCustomType(alien4cloud.ui.form.annotation.FormCustomType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) List(java.util.List) Map(java.util.Map)

Example 67 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition 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 68 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class NodeInstanceService method updateProperties.

private void updateProperties(NodeType nodeType, NodeTemplate nodeTemplate, Map<String, AbstractPropertyValue> nodeProperties) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    for (Map.Entry<String, AbstractPropertyValue> propertyValueEntry : nodeProperties.entrySet()) {
        if (propertyValueEntry.getValue() != null) {
            AbstractPropertyValue value = PatchUtil.realValue(propertyValueEntry.getValue());
            PropertyDefinition propertyDefinition = safe(nodeType.getProperties()).get(propertyValueEntry.getKey());
            if (propertyDefinition == null) {
                throw new NotFoundException("No property <" + propertyValueEntry.getKey() + "> can be found for node type <" + nodeType.getElementId() + "> in version <" + nodeType.getArchiveVersion() + ">");
            }
            propertyService.setPropertyValue(nodeTemplate, propertyDefinition, propertyValueEntry.getKey(), value);
        }
    }
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 69 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class NodeInstanceService method updateCapabilitiesProperties.

private void updateCapabilitiesProperties(CapabilityType capabilityType, Capability targetCapability, Capability patchCapability) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    for (Map.Entry<String, AbstractPropertyValue> propertyValueEntry : patchCapability.getProperties().entrySet()) {
        if (propertyValueEntry.getValue() != null) {
            AbstractPropertyValue value = PatchUtil.realValue(propertyValueEntry.getValue());
            PropertyDefinition propertyDefinition = safe(capabilityType.getProperties()).get(propertyValueEntry.getKey());
            if (propertyDefinition == null) {
                throw new NotFoundException("No property <" + propertyValueEntry.getKey() + "> can be found for capability <" + capabilityType.getElementId() + ">");
            }
            propertyService.setCapabilityPropertyValue(targetCapability, propertyDefinition, propertyValueEntry.getKey(), value);
        }
    }
}
Also used : NotFoundException(alien4cloud.exception.NotFoundException) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 70 with PropertyDefinition

use of org.alien4cloud.tosca.model.definitions.PropertyDefinition in project alien4cloud by alien4cloud.

the class AbstractSetMatchedPropertyModifier method setProperty.

protected void setProperty(FlowExecutionContext context, V resourceTemplate, U template, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    PropertyDefinition propertyDefinition = ((T) ToscaContext.getOrFail(AbstractToscaType.class, resourceTemplate.getTemplate().getType())).getProperties().get(propertyName);
    if (propertyDefinition == null) {
        throw new NotFoundException("No property of name [" + propertyName + "] can be found on the " + getSubject() + " template [" + templateId + "] of type [" + resourceTemplate.getTemplate().getType() + "]");
    }
    AbstractPropertyValue locationResourcePropertyValue = resourceTemplate.getTemplate().getProperties().get(propertyName);
    ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
    ensureNotSet(template.getProperties().get(propertyName), "in the portable topology", propertyName, propertyValue);
    // Update the configuration
    NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
    // Perform the update of the property
    if (propertyValue == null) {
        nodePropsOverride.getProperties().remove(propertyName);
    } else {
        AbstractPropertyValue abstractPropertyValue = PropertyService.asPropertyValue(propertyValue);
        if (!(abstractPropertyValue instanceof FunctionPropertyValue)) {
            ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
        }
        nodePropsOverride.getProperties().put(propertyName, abstractPropertyValue);
    }
    context.saveConfiguration(matchingConfiguration);
}
Also used : NodePropsOverride(org.alien4cloud.alm.deployment.configuration.model.DeploymentMatchingConfiguration.NodePropsOverride) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) NotFoundException(alien4cloud.exception.NotFoundException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Aggregations

PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)101 Test (org.junit.Test)42 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)23 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)20 Map (java.util.Map)19 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeType (org.alien4cloud.tosca.model.types.NodeType)15 NotFoundException (alien4cloud.exception.NotFoundException)13 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)13 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)11 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)10 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 Capability (org.alien4cloud.tosca.model.templates.Capability)10 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)10 DataType (org.alien4cloud.tosca.model.types.DataType)9 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)8 Set (java.util.Set)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8