use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.
the class PropertyDefinitionConverter method convert.
public PropertyDefinition convert(FormPropertyDefinition definitionAnnotation) {
if (definitionAnnotation == null) {
return null;
}
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(definitionAnnotation.type());
// FIXME ? can be other than a scalar here ?
propertyDefinition.setDefault(new ScalarPropertyValue(definitionAnnotation.defaultValue()));
propertyDefinition.setDescription(definitionAnnotation.description());
propertyDefinition.setPassword(definitionAnnotation.isPassword());
propertyDefinition.setRequired(definitionAnnotation.isRequired());
List<PropertyConstraint> constraints = Lists.newArrayList();
if (!definitionAnnotation.constraints().equal().isEmpty()) {
EqualConstraint equalConstraint = new EqualConstraint();
equalConstraint.setEqual(definitionAnnotation.constraints().equal());
constraints.add(equalConstraint);
}
if (!definitionAnnotation.constraints().greaterOrEqual().isEmpty()) {
GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
greaterOrEqualConstraint.setGreaterOrEqual(definitionAnnotation.constraints().greaterOrEqual());
constraints.add(greaterOrEqualConstraint);
}
if (!definitionAnnotation.constraints().greaterThan().isEmpty()) {
GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
greaterThanConstraint.setGreaterThan(definitionAnnotation.constraints().greaterThan());
constraints.add(greaterThanConstraint);
}
if (!definitionAnnotation.constraints().inRange().isEmpty()) {
String inRangeText = definitionAnnotation.constraints().inRange();
Matcher matcher = IN_RANGE_REGEXP.matcher(inRangeText);
if (matcher.matches()) {
InRangeConstraint inRangeConstraint = new InRangeConstraint();
inRangeConstraint.setRangeMinValue(matcher.group(1).trim());
inRangeConstraint.setRangeMaxValue(matcher.group(2).trim());
constraints.add(inRangeConstraint);
} else {
throw new FormDescriptorGenerationException("In range constraint definition must be in this format '[ $min - $max ]'");
}
}
if (definitionAnnotation.constraints().length() >= 0) {
LengthConstraint lengthConstraint = new LengthConstraint();
lengthConstraint.setLength(definitionAnnotation.constraints().length());
constraints.add(lengthConstraint);
}
if (!definitionAnnotation.constraints().lessOrEqual().isEmpty()) {
LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
lessOrEqualConstraint.setLessOrEqual(definitionAnnotation.constraints().lessOrEqual());
constraints.add(lessOrEqualConstraint);
}
if (!definitionAnnotation.constraints().lessThan().isEmpty()) {
LessThanConstraint lessThanConstraint = new LessThanConstraint();
lessThanConstraint.setLessThan(definitionAnnotation.constraints().lessThan());
constraints.add(lessThanConstraint);
}
if (definitionAnnotation.constraints().maxLength() >= 0) {
MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
maxLengthConstraint.setMaxLength(definitionAnnotation.constraints().maxLength());
constraints.add(maxLengthConstraint);
}
if (definitionAnnotation.constraints().minLength() >= 0) {
MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
minLengthConstraint.setMinLength(definitionAnnotation.constraints().minLength());
constraints.add(minLengthConstraint);
}
if (!definitionAnnotation.constraints().pattern().isEmpty()) {
PatternConstraint patternConstraint = new PatternConstraint();
patternConstraint.setPattern(definitionAnnotation.constraints().pattern());
constraints.add(patternConstraint);
}
if (definitionAnnotation.constraints().validValues().length > 0) {
ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
validValuesConstraint.setValidValues(Lists.newArrayList(definitionAnnotation.constraints().validValues()));
constraints.add(validValuesConstraint);
}
if (!constraints.isEmpty()) {
propertyDefinition.setConstraints(constraints);
}
return propertyDefinition;
}
use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.
the class ToscaPropertyDefaultValueConstraintsValidator method isValid.
@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
PropertyValue defaultValue = value.getDefault();
if (defaultValue == null) {
// no default value is specified.
return true;
}
// validate that the default value matches the defined constraints.
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
if (toscaType == null) {
return false;
}
if (!(defaultValue instanceof ScalarPropertyValue)) {
// No constraint can be made on other thing than scalar values
return false;
}
String defaultValueAsString = ((ScalarPropertyValue) defaultValue).getValue();
Object parsedDefaultValue;
try {
parsedDefaultValue = toscaType.parse(defaultValueAsString);
} catch (InvalidPropertyValueException e) {
return false;
}
if (value.getConstraints() != null) {
for (PropertyConstraint constraint : value.getConstraints()) {
try {
constraint.validate(parsedDefaultValue);
} catch (ConstraintViolationException e) {
return false;
}
}
}
return true;
}
use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.
the class ServiceStepDefinitions method iSetThePropertyToForTheService.
@And("^I set the property \"([^\"]*)\" to \"([^\"]*)\" for the service \"([^\"]*)\"$")
public void iSetThePropertyToForTheService(String propertyName, String propertyValue, String serviceName) throws Throwable {
String serviceId = Context.getInstance().getServiceId(serviceName);
PatchServiceResourceRequest request = new PatchServiceResourceRequest();
NodeInstanceDTO nodeInstance = new NodeInstanceDTO();
nodeInstance.setProperties(Maps.newHashMap());
nodeInstance.getProperties().put(propertyName, new ScalarPropertyValue(propertyValue));
request.setNodeInstance(nodeInstance);
Context.getInstance().registerRestResponse(Context.getRestClientInstance().patchJSon("/rest/v1/services/" + serviceId, JsonUtil.toString(request)));
}
use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.
the class AbstractTemplateMatcher method isValidTemplatePropertiesMatch.
/**
* Add filters ent/ICSARRepositorySearchService.java from the matching configuration to the node filter that will be applied for matching only if a value is
* specified on the configuration template.
*
* @param templatePropertyValues The properties values from the template to match.
* @param candidatePropertyValues The values defined on the Location Template.
* @param propertyDefinitions The properties definitions associated with the node.
* @param configuredFilters The filtering map (based on constraints) from matching configuration, other properties fall backs to an equal constraint/filter.
*/
protected boolean isValidTemplatePropertiesMatch(Map<String, AbstractPropertyValue> templatePropertyValues, Map<String, AbstractPropertyValue> candidatePropertyValues, Map<String, PropertyDefinition> propertyDefinitions, Map<String, List<IMatchPropertyConstraint>> configuredFilters) {
// We perform matching on every property that is defined on the candidate (admin node) and that has a value defined in the topology.
for (Map.Entry<String, AbstractPropertyValue> candidateValueEntry : safe(candidatePropertyValues).entrySet()) {
List<IMatchPropertyConstraint> filter = safe(configuredFilters).get(candidateValueEntry.getKey());
AbstractPropertyValue templatePropertyValue = templatePropertyValues.get(candidateValueEntry.getKey());
// For now we support matching only on scalar properties.
if (candidateValueEntry.getValue() != null && candidateValueEntry.getValue() instanceof ScalarPropertyValue && templatePropertyValue != null && templatePropertyValue instanceof ScalarPropertyValue) {
try {
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinitions.get(candidateValueEntry.getKey()).getType());
if (filter == null) {
// If no filter is defined then process matching using an equal constraint.
filter = Lists.newArrayList(new EqualConstraint());
}
// set the constraint value and add it to the node filter
for (IMatchPropertyConstraint constraint : filter) {
constraint.setConstraintValue(toscaType, ((ScalarPropertyValue) candidateValueEntry.getValue()).getValue());
try {
constraint.validate(toscaType, ((ScalarPropertyValue) templatePropertyValue).getValue());
} catch (ConstraintViolationException e) {
return false;
}
}
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
log.debug("The value of property for a constraint is not valid.", e);
}
}
}
return true;
}
use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.
the class SuggestionService method checkProperties.
private void checkProperties(String nodePrefix, Map<String, AbstractPropertyValue> propertyValueMap, Class<? extends AbstractInheritableToscaType> type, String elementId, ParsingContext context) {
if (MapUtils.isNotEmpty(propertyValueMap)) {
for (Map.Entry<String, AbstractPropertyValue> propertyValueEntry : propertyValueMap.entrySet()) {
String propertyName = propertyValueEntry.getKey();
AbstractPropertyValue propertyValue = propertyValueEntry.getValue();
if (propertyValue instanceof ScalarPropertyValue) {
String propertyTextValue = ((ScalarPropertyValue) propertyValue).getValue();
checkProperty(nodePrefix, propertyName, propertyTextValue, type, elementId, context);
}
}
}
}
Aggregations