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);
}
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;
}
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);
}
}
}
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);
}
}
}
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);
}
Aggregations