Search in sources :

Example 21 with PropertyConstraint

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;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint)

Example 22 with PropertyConstraint

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;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl)

Example 23 with PropertyConstraint

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);
}
Also used : LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Test(org.junit.Test)

Example 24 with PropertyConstraint

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;
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)

Example 25 with PropertyConstraint

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);
}
Also used : PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)30 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)18 Test (org.junit.Test)15 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)4 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)4 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)3 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)3 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)3 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 ConstraintInformation (alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation)2 IntrospectionException (java.beans.IntrospectionException)2