Search in sources :

Example 6 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class DefaultNodeMatcherTest method location_resource_should_be_matched_only_if_capabilities_are_satisfied.

@Test
public void location_resource_should_be_matched_only_if_capabilities_are_satisfied() throws Exception {
    // Given
    CapabilityDefinition capabilityArchitecture = new CapabilityDefinition("tosca.capabilities.OperatingSystem");
    computeNodeType.setCapabilities(Arrays.asList(capabilityArchitecture));
    Capability capability = new Capability();
    capability.setType("tosca.capabilities.OperatingSystem");
    capability.setProperties(ImmutableMap.of("architecture", new ScalarPropertyValue("x86")));
    computeNodeTemplate.setCapabilities(ImmutableMap.of("os", capability));
    CapabilityType capabilityType = new CapabilityType();
    PropertyDefinition propertyDefinition = new PropertyDefinition();
    propertyDefinition.setType("string");
    capabilityType.setElementId("tosca.capabilities.OperatingSystem");
    capabilityType.setProperties(ImmutableMap.of("architecture", propertyDefinition));
    locationResources.setCapabilityTypes(ImmutableMap.of("tosca.capabilities.OperatingSystem", capabilityType));
    // Matching configuration
    Map<String, MatchingConfiguration> capabilityFilterConfiguration = new HashMap<>();
    MatchingConfiguration matchingConfiguration = new MatchingConfiguration();
    MatchingFilterDefinition matchingFilterDefinition = new MatchingConfiguration();
    matchingFilterDefinition.setProperties(ImmutableMap.of("architecture", Arrays.asList(new EqualConstraint())));
    matchingConfiguration.setCapabilities(ImmutableMap.of("os", matchingFilterDefinition));
    capabilityFilterConfiguration.put("org.alien4cloud.nodes.mock.aws.Compute", matchingConfiguration);
    // When
    NodeTemplate wantedNodeTemplate = new NodeTemplate();
    wantedNodeTemplate.setType("tosca.nodes.Compute");
    Capability wantedCapability = new Capability();
    wantedCapability.setType("tosca.capabilities.OperatingSystem");
    wantedCapability.setProperties(ImmutableMap.of("architecture", new ScalarPropertyValue("power_pc")));
    wantedNodeTemplate.setCapabilities(ImmutableMap.of("os", wantedCapability));
    NodeType nodeType = new NodeType();
    List<LocationResourceTemplate> proposition = nodeMatcher.matchNode(wantedNodeTemplate, nodeType, locationResources, capabilityFilterConfiguration);
    // Then
    assertThat(proposition).hasSize(0);
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) LocationResourceTemplate(alien4cloud.model.orchestrators.locations.LocationResourceTemplate) HashMap(java.util.HashMap) MatchingConfiguration(alien4cloud.model.deployment.matching.MatchingConfiguration) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) MatchingFilterDefinition(alien4cloud.model.deployment.matching.MatchingFilterDefinition) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) CapabilityDefinition(org.alien4cloud.tosca.model.definitions.CapabilityDefinition) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) Test(org.junit.Test)

Example 7 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class ToscaPropertyDefaultValueConstraintsValidatorTest method createEqualDefinition.

private PropertyDefinition createEqualDefinition(String propertyType, String constraintValue, String defaultValue) {
    EqualConstraint constraint = new EqualConstraint();
    constraint.setEqual(constraintValue);
    return createDefinitions(propertyType, constraint, defaultValue);
}
Also used : LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)

Example 8 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class ToscaSerializerUtilsTest method testRenderConstraint.

@Test
public void testRenderConstraint() {
    GreaterOrEqualConstraint greaterOrEqualConstraint = new GreaterOrEqualConstraint();
    Assert.assertEquals("greater_or_equal: null", utils.renderConstraint(greaterOrEqualConstraint));
    greaterOrEqualConstraint.setGreaterOrEqual("1");
    Assert.assertEquals("greater_or_equal: 1", utils.renderConstraint(greaterOrEqualConstraint));
    GreaterThanConstraint greaterThanConstraint = new GreaterThanConstraint();
    Assert.assertEquals("greater_than: null", utils.renderConstraint(greaterThanConstraint));
    greaterThanConstraint.setGreaterThan("1");
    Assert.assertEquals("greater_than: 1", utils.renderConstraint(greaterThanConstraint));
    LessOrEqualConstraint lessOrEqualConstraint = new LessOrEqualConstraint();
    Assert.assertEquals("less_or_equal: null", utils.renderConstraint(lessOrEqualConstraint));
    lessOrEqualConstraint.setLessOrEqual("1");
    Assert.assertEquals("less_or_equal: 1", utils.renderConstraint(lessOrEqualConstraint));
    LessThanConstraint lessThanConstraint = new LessThanConstraint();
    Assert.assertEquals("less_than: null", utils.renderConstraint(lessThanConstraint));
    lessThanConstraint.setLessThan("1");
    Assert.assertEquals("less_than: 1", utils.renderConstraint(lessThanConstraint));
    LengthConstraint lengthConstraint = new LengthConstraint();
    Assert.assertEquals("length: null", utils.renderConstraint(lengthConstraint));
    lengthConstraint.setLength(1);
    Assert.assertEquals("length: 1", utils.renderConstraint(lengthConstraint));
    MaxLengthConstraint maxLengthConstraint = new MaxLengthConstraint();
    Assert.assertEquals("max_length: null", utils.renderConstraint(maxLengthConstraint));
    maxLengthConstraint.setMaxLength(1);
    Assert.assertEquals("max_length: 1", utils.renderConstraint(maxLengthConstraint));
    MinLengthConstraint minLengthConstraint = new MinLengthConstraint();
    Assert.assertEquals("min_length: null", utils.renderConstraint(minLengthConstraint));
    minLengthConstraint.setMinLength(1);
    Assert.assertEquals("min_length: 1", utils.renderConstraint(minLengthConstraint));
    PatternConstraint patternConstraint = new PatternConstraint();
    Assert.assertEquals("pattern: null", utils.renderConstraint(patternConstraint));
    patternConstraint.setPattern("a");
    Assert.assertEquals("pattern: a", utils.renderConstraint(patternConstraint));
    patternConstraint.setPattern("[.*]");
    Assert.assertEquals("pattern: \"[.*]\"", utils.renderConstraint(patternConstraint));
    EqualConstraint equalConstraint = new EqualConstraint();
    Assert.assertEquals("equal: null", utils.renderConstraint(equalConstraint));
    equalConstraint.setEqual("value");
    Assert.assertEquals("equal: value", utils.renderConstraint(equalConstraint));
    equalConstraint.setEqual(" value");
    Assert.assertEquals("equal: \" value\"", utils.renderConstraint(equalConstraint));
    InRangeConstraint inRangeConstraint = new InRangeConstraint();
    Assert.assertEquals("in_range: []", utils.renderConstraint(inRangeConstraint));
    List<String> inRange = Lists.newArrayList();
    inRangeConstraint.setInRange(inRange);
    Assert.assertEquals("in_range: []", utils.renderConstraint(inRangeConstraint));
    inRange.add("1");
    Assert.assertEquals("in_range: [1]", utils.renderConstraint(inRangeConstraint));
    inRange.add("2");
    Assert.assertEquals("in_range: [1, 2]", utils.renderConstraint(inRangeConstraint));
    ValidValuesConstraint validValuesConstraint = new ValidValuesConstraint();
    Assert.assertEquals("valid_values: []", utils.renderConstraint(validValuesConstraint));
    List<String> validValues = Lists.newArrayList();
    validValuesConstraint.setValidValues(validValues);
    Assert.assertEquals("valid_values: []", utils.renderConstraint(validValuesConstraint));
    validValues.add("value1");
    Assert.assertEquals("valid_values: [value1]", utils.renderConstraint(validValuesConstraint));
    validValues.add("value2 ");
    Assert.assertEquals("valid_values: [value1, \"value2 \"]", utils.renderConstraint(validValuesConstraint));
    validValues.add("value3,value4");
    Assert.assertEquals("valid_values: [value1, \"value2 \", \"value3,value4\"]", utils.renderConstraint(validValuesConstraint));
    // finally test an unknown constraint
    AbstractPropertyConstraint abstractPropertyConstraint = new AbstractPropertyConstraint() {

        @Override
        public void validate(Object propertyValue) throws ConstraintViolationException {
        }

        @Override
        public void initialize(IPropertyType<?> propertyType) throws ConstraintValueDoNotMatchPropertyTypeException {
        }
    };
    Assert.assertEquals("", utils.renderConstraint(abstractPropertyConstraint));
}
Also used : MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) ValidValuesConstraint(org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint) AbstractPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.AbstractPropertyConstraint) IPropertyType(org.alien4cloud.tosca.normative.types.IPropertyType) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) PatternConstraint(org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint) InRangeConstraint(org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) Test(org.junit.Test)

Example 9 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project alien4cloud by alien4cloud.

the class DanglingRequirementService method setPropertiesFromFilter.

// TODO This is a workaround as right now matching does not use filter but properties.
private void setPropertiesFromFilter(NodeTemplate danglingTemplate, NodeType danglingNodeType) {
    if (danglingTemplate.getNodeFilter() == null) {
        return;
    }
    for (Entry<String, List<PropertyConstraint>> constraintEntry : safe(danglingTemplate.getNodeFilter().getProperties()).entrySet()) {
        if (constraintEntry.getValue().size() == 1 && constraintEntry.getValue().get(0) instanceof EqualConstraint && ToscaTypes.isSimple(danglingNodeType.getProperties().get(constraintEntry.getKey()).getType())) {
            danglingTemplate.getProperties().put(constraintEntry.getKey(), new ScalarPropertyValue(((EqualConstraint) constraintEntry.getValue().get(0)).getEqual()));
        }
    }
    for (Entry<String, FilterDefinition> capabilityFilter : safe(danglingTemplate.getNodeFilter().getCapabilities()).entrySet()) {
        Capability capability = getCapability(danglingTemplate, capabilityFilter.getKey());
        CapabilityType capabilityType = ToscaContext.get(CapabilityType.class, capability.getType());
        for (Entry<String, List<PropertyConstraint>> constraintEntry : safe(capabilityFilter.getValue().getProperties()).entrySet()) {
            if (constraintEntry.getValue().size() == 1 && constraintEntry.getValue().get(0) instanceof EqualConstraint && ToscaTypes.isSimple(capabilityType.getProperties().get(constraintEntry.getKey()).getType())) {
                capability.getProperties().put(constraintEntry.getKey(), new ScalarPropertyValue(((EqualConstraint) constraintEntry.getValue().get(0)).getEqual()));
            }
        }
    }
}
Also used : FilterDefinition(org.alien4cloud.tosca.model.definitions.FilterDefinition) CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Capability(org.alien4cloud.tosca.model.templates.Capability) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)

Example 10 with EqualConstraint

use of org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint in project yorc-a4c-plugin by ystia.

the class AbstractLocationConfigurer method printProperties.

private void printProperties(Map<String, List<IMatchPropertyConstraint>> props) {
    for (String kprop : props.keySet()) {
        List<IMatchPropertyConstraint> lc = props.get(kprop);
        for (IMatchPropertyConstraint mpc : lc) {
            if (mpc instanceof LessOrEqualConstraint) {
                LessOrEqualConstraint cons = (LessOrEqualConstraint) mpc;
                log.debug("  " + kprop + " <= " + cons.getLessOrEqual());
            } else if (mpc instanceof EqualConstraint) {
                EqualConstraint cons = (EqualConstraint) mpc;
                log.debug("  " + kprop + " == " + cons.getEqual());
            } else if (mpc instanceof GreaterOrEqualConstraint) {
                GreaterOrEqualConstraint cons = (GreaterOrEqualConstraint) mpc;
                log.debug("  " + kprop + " >= " + cons.getGreaterOrEqual());
            } else if (mpc instanceof GreaterThanConstraint) {
                GreaterThanConstraint cons = (GreaterThanConstraint) mpc;
                log.debug("  " + kprop + " > " + cons.getGreaterThan());
            } else if (mpc instanceof LessThanConstraint) {
                LessThanConstraint cons = (LessThanConstraint) mpc;
                log.debug("  " + kprop + " < " + cons.getLessThan());
            } else {
                log.debug("  " + kprop + " " + mpc.toString());
            }
        }
    }
}
Also used : LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) IMatchPropertyConstraint(org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) LessOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) GreaterOrEqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)

Aggregations

EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)10 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)5 GreaterOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterOrEqualConstraint)5 LessOrEqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessOrEqualConstraint)5 GreaterThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint)4 LessThanConstraint (org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint)4 ValidValuesConstraint (org.alien4cloud.tosca.model.definitions.constraints.ValidValuesConstraint)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)3 InRangeConstraint (org.alien4cloud.tosca.model.definitions.constraints.InRangeConstraint)3 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)3 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)3 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)3 PatternConstraint (org.alien4cloud.tosca.model.definitions.constraints.PatternConstraint)3 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)3 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)2 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)2 IMatchPropertyConstraint (org.alien4cloud.tosca.model.definitions.constraints.IMatchPropertyConstraint)2 Capability (org.alien4cloud.tosca.model.templates.Capability)2 Test (org.junit.Test)2 AbstractSuggestionEntry (alien4cloud.model.common.AbstractSuggestionEntry)1