use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.
the class ToscaPropertyConstraintDuplicateValidator method isValid.
@Override
public boolean isValid(List<PropertyConstraint> value, ConstraintValidatorContext context) {
if (value == null) {
return true;
}
Set<String> definedConstraints = Sets.newHashSet();
boolean isValid = true;
for (int i = 0; i < value.size(); i++) {
PropertyConstraint constraint = value.get(i);
if (!definedConstraints.add(constraint.getClass().getName())) {
context.buildConstraintViolationWithTemplate("CONSTRAINTS.VALIDATION.DUPLICATED_CONSTRAINT").addBeanNode().inIterable().atIndex(i).addConstraintViolation();
isValid = false;
}
}
return isValid;
}
use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.
the class ConstraintParser method parseConstraint.
private PropertyConstraint parseConstraint(String operator, Node keyNode, Node expressionNode, ParsingContextExecution context) {
ConstraintParsingInfo info = constraintBuildersMap.get(operator);
if (info == null) {
context.getParsingErrors().add(new ParsingError(ParsingErrorLevel.WARNING, ErrorCode.UNKNOWN_CONSTRAINT, "Constraint parsing issue", keyNode.getStartMark(), "Unknown constraint operator, will be ignored.", keyNode.getEndMark(), operator));
return null;
}
PropertyConstraint constraint;
try {
constraint = info.constraintClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new ParsingTechnicalException("Unable to create constraint.", e);
}
BeanWrapper target = new BeanWrapperImpl(constraint);
parseAndSetValue(target, null, expressionNode, context, new MappingTarget(info.expressionPropertyName, info.expressionParser));
return constraint;
}
use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.
the class ConstraintPropertyServiceTest method testValidMapProperty.
@Test
public void testValidMapProperty() throws Exception {
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(ToscaTypes.MAP);
PropertyDefinition entrySchema = new PropertyDefinition();
entrySchema.setType(ToscaTypes.STRING);
propertyDefinition.setEntrySchema(entrySchema);
Object propertyValue = ImmutableMap.builder().put("aa", "bb").build();
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
// test length constraint
LengthConstraint lengthConstraint = new LengthConstraint();
lengthConstraint.setLength(1);
List<PropertyConstraint> constraints = Lists.newArrayList();
constraints.add(lengthConstraint);
propertyDefinition.setConstraints(constraints);
ConstraintPropertyService.checkPropertyConstraint("test", propertyValue, propertyDefinition);
}
use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.
the class MockOrchestratorFactory method getDeploymentPropertyDefinitions.
@Override
public Map<String, PropertyDefinition> getDeploymentPropertyDefinitions() {
// Field 1 : managerUrl as string
PropertyDefinition managerUrl = new PropertyDefinition();
managerUrl.setType(ToscaTypes.STRING.toString());
managerUrl.setRequired(true);
managerUrl.setDescription("PaaS manager URL");
managerUrl.setConstraints(null);
PatternConstraint manageUrlConstraint = new PatternConstraint();
manageUrlConstraint.setPattern("http://.+");
managerUrl.setConstraints(Arrays.asList((PropertyConstraint) manageUrlConstraint));
// Field 2 : number backup with constraint
PropertyDefinition numberBackup = new PropertyDefinition();
numberBackup.setType(ToscaTypes.INTEGER.toString());
numberBackup.setRequired(true);
numberBackup.setDescription("Number of backup");
numberBackup.setConstraints(null);
GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
greaterOrEqualConstraint.setGreaterOrEqual(String.valueOf("1"));
numberBackup.setConstraints(Lists.newArrayList((PropertyConstraint) greaterOrEqualConstraint));
// Field 3 : email manager
PropertyDefinition managerEmail = new PropertyDefinition();
managerEmail.setType(ToscaTypes.STRING.toString());
managerEmail.setRequired(true);
managerEmail.setDescription("PaaS manager email");
managerEmail.setConstraints(null);
PatternConstraint managerEmailConstraint = new PatternConstraint();
managerEmailConstraint.setPattern(".+@.+");
managerEmail.setConstraints(Arrays.asList((PropertyConstraint) managerEmailConstraint));
deploymentProperties.put("managementUrl", managerUrl);
deploymentProperties.put("numberBackup", numberBackup);
deploymentProperties.put("managerEmail", managerEmail);
return deploymentProperties;
}
use of org.alien4cloud.tosca.model.definitions.PropertyConstraint in project alien4cloud by alien4cloud.
the class PropertyConstraintDeserializer method deserialize.
@Override
public PropertyConstraint deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
ObjectNode node = mapper.readTree(jp);
String constraintName = null;
Iterator<String> fieldIterator = node.fieldNames();
// First field is also the constraint name ?
if (fieldIterator.hasNext()) {
constraintName = fieldIterator.next();
} else {
throw JsonMappingException.from(jp, "Constraint definition must contain one field");
}
PropertyNamingStrategy namingStrategy = mapper.getDeserializationConfig().getPropertyNamingStrategy();
Map<String, Class<? extends PropertyConstraint>> constraintsMapping = getTranslatedConstraintsMap(namingStrategy);
if (!constraintsMapping.containsKey(constraintName)) {
if ("rangeMinValue".equals(constraintName) || "rangeMaxValue".equals(constraintName)) {
return mapper.treeToValue(node, InRangeConstraint.class);
} else {
throw JsonMappingException.from(jp, "Constraint not found [" + constraintName + "], expect one of [" + this.constraints.keySet() + "]");
}
}
Class<? extends PropertyConstraint> constraintClass = constraintsMapping.get(constraintName);
return mapper.treeToValue(node, constraintClass);
}
Aggregations