use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.
the class ToscaSerializerTest method getConstraintList.
private List<PropertyConstraint> getConstraintList() {
List<PropertyConstraint> result = new ArrayList<PropertyConstraint>();
ValidValuesConstraint c1 = new ValidValuesConstraint();
c1.setValidValues(Lists.newArrayList("one", "two", "tree"));
result.add(c1);
GreaterOrEqualConstraint c2 = new GreaterOrEqualConstraint();
c2.setGreaterOrEqual("2");
result.add(c2);
GreaterThanConstraint c3 = new GreaterThanConstraint();
c3.setGreaterThan("3");
result.add(c3);
LessOrEqualConstraint c4 = new LessOrEqualConstraint();
c4.setLessOrEqual("4");
result.add(c4);
LessThanConstraint c5 = new LessThanConstraint();
c5.setLessThan("5");
result.add(c5);
LengthConstraint c6 = new LengthConstraint();
c6.setLength(6);
result.add(c6);
MaxLengthConstraint c7 = new MaxLengthConstraint();
c7.setMaxLength(7);
result.add(c7);
MinLengthConstraint c8 = new MinLengthConstraint();
c8.setMinLength(8);
result.add(c8);
PatternConstraint c9 = new PatternConstraint();
c9.setPattern("9+");
result.add(c9);
EqualConstraint c10 = new EqualConstraint();
c10.setEqual("10");
result.add(c10);
InRangeConstraint c11 = new InRangeConstraint();
c11.setInRange(Lists.newArrayList("11", "12"));
result.add(c11);
return result;
}
use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.
the class AbstractSetMatchedPropertyModifier method ensureNotSet.
/**
* Check that the property is not already defined in a source
*
* @param sourcePropertyValue null or an already defined Property Value.
* @param messageSource The named source to add in the exception message in case of failure.
*/
// This cannot be thrown on getConstraintInformation from an equals constraint.
@SneakyThrows(IntrospectionException.class)
protected void ensureNotSet(AbstractPropertyValue sourcePropertyValue, String messageSource, String propertyName, Object propertyValue) throws ConstraintViolationException {
if (sourcePropertyValue != null) {
EqualConstraint constraint = new EqualConstraint();
if (sourcePropertyValue instanceof ScalarPropertyValue) {
constraint.setEqual(((ScalarPropertyValue) sourcePropertyValue).getValue());
}
ConstraintUtil.ConstraintInformation information = ConstraintUtil.getConstraintInformation(constraint);
// If admin has defined a value users should not be able to override it.
throw new ConstraintViolationException("Overriding value specified " + messageSource + " is not authorized.", null, information);
}
}
use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.
the class SuggestionService method checkPropertyConstraints.
private void checkPropertyConstraints(String prefix, Class<? extends AbstractInheritableToscaType> type, String elementId, String propertyName, List<PropertyConstraint> constraints, ParsingContext context) {
if (constraints != null && !constraints.isEmpty()) {
for (PropertyConstraint propertyConstraint : constraints) {
if (propertyConstraint instanceof EqualConstraint) {
EqualConstraint equalConstraint = (EqualConstraint) propertyConstraint;
String valueToCheck = equalConstraint.getEqual();
if (checkProperty(prefix, propertyName, valueToCheck, type, elementId, context) == null) {
createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(valueToCheck), elementId, propertyName);
}
} else if (propertyConstraint instanceof ValidValuesConstraint) {
ValidValuesConstraint validValuesConstraint = (ValidValuesConstraint) propertyConstraint;
if (validValuesConstraint.getValidValues() != null && !validValuesConstraint.getValidValues().isEmpty()) {
AbstractSuggestionEntry foundSuggestion = null;
for (String valueToCheck : validValuesConstraint.getValidValues()) {
foundSuggestion = checkProperty(prefix, propertyName, valueToCheck, type, elementId, context);
if (foundSuggestion == null) {
// No suggestion exists don't need to check any more for other values
break;
}
}
if (foundSuggestion == null) {
createSuggestionEntry(ElasticSearchDAO.TOSCA_ELEMENT_INDEX, CapabilityType.class, Sets.newHashSet(validValuesConstraint.getValidValues()), elementId, propertyName);
}
}
}
}
}
}
use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint 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.constraints.EqualConstraint 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;
}
Aggregations