Search in sources :

Example 16 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class AbstractToscaParserSimpleProfileTest method testNodeType.

@SuppressWarnings("unchecked")
@Test
public void testNodeType() throws FileNotFoundException, ParsingException {
    Mockito.reset(csarRepositorySearchService);
    NodeType mockedResult = Mockito.mock(NodeType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.SoftwareComponent"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(mockedResult.getDerivedFrom()).thenReturn(Lists.newArrayList("tosca.nodes.Root"));
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Root"), Mockito.any(Set.class))).thenReturn(mockedResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(NodeType.class), Mockito.eq("tosca.nodes.Compute"), Mockito.any(Set.class))).thenReturn(mockedResult);
    CapabilityType mockedCapabilityResult = Mockito.mock(CapabilityType.class);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("mytypes.mycapabilities.MyCapabilityTypeName"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(CapabilityType.class), Mockito.eq("tosca.capabilities.Endpoint"), Mockito.any(Set.class))).thenReturn(mockedCapabilityResult);
    RelationshipType hostedOn = new RelationshipType();
    Mockito.when(csarRepositorySearchService.getElementInDependencies(Mockito.eq(RelationshipType.class), Mockito.eq("tosca.relationships.HostedOn"), Mockito.any(Set.class))).thenReturn(hostedOn);
    ParsingResult<ArchiveRoot> parsingResult = parser.parseFile(Paths.get(getRootDirectory(), "tosca-node-type.yml"));
    ParserTestUtil.displayErrors(parsingResult);
    assertNoBlocker(parsingResult);
    ArchiveRoot archiveRoot = parsingResult.getResult();
    Assert.assertNotNull(archiveRoot.getArchive());
    Assert.assertEquals(getToscaVersion(), archiveRoot.getArchive().getToscaDefinitionsVersion());
    Assert.assertEquals(1, archiveRoot.getNodeTypes().size());
    // check node type.
    NodeType nodeType = archiveRoot.getNodeTypes().get("my_company.my_types.MyAppNodeType");
    Assert.assertNotNull(nodeType);
    Assert.assertEquals(Lists.newArrayList("tosca.nodes.SoftwareComponent", "tosca.nodes.Root"), nodeType.getDerivedFrom());
    Assert.assertEquals("My company’s custom applicaton", nodeType.getDescription());
    // validate properties parsing
    Assert.assertEquals(4, nodeType.getProperties().size());
    PropertyDefinition def1 = new PropertyDefinition();
    def1.setType("string");
    def1.setDefault(new ScalarPropertyValue("default"));
    def1.setDescription("application password");
    List<PropertyConstraint> constraints = Lists.newArrayList();
    constraints.add(new MinLengthConstraint(6));
    constraints.add(new MaxLengthConstraint(10));
    def1.setConstraints(constraints);
    PropertyDefinition def2 = new PropertyDefinition();
    def2.setType("integer");
    def2.setDescription("application port number");
    PropertyDefinition def3 = new PropertyDefinition();
    def3.setType("scalar-unit.size");
    def3.setDefault(new ScalarPropertyValue("1 GB"));
    LessThanConstraint ltConstraint = new LessThanConstraint();
    ltConstraint.setLessThan("1 TB");
    constraints = Lists.<PropertyConstraint>newArrayList(ltConstraint);
    def3.setConstraints(constraints);
    PropertyDefinition def4 = new PropertyDefinition();
    def4.setType("scalar-unit.time");
    def4.setDefault(new ScalarPropertyValue("1 d"));
    GreaterThanConstraint gtConstraint = new GreaterThanConstraint();
    gtConstraint.setGreaterThan("1 h");
    constraints = Lists.<PropertyConstraint>newArrayList(gtConstraint);
    def4.setConstraints(constraints);
    Assert.assertEquals(MapUtil.newHashMap(new String[] { "my_app_password", "my_app_duration", "my_app_size", "my_app_port" }, new PropertyDefinition[] { def1, def4, def3, def2 }), nodeType.getProperties());
    // check requirements
    Assert.assertEquals(2, nodeType.getRequirements().size());
    RequirementDefinition rd0 = nodeType.getRequirements().get(0);
    Assert.assertEquals("host", rd0.getId());
    Assert.assertEquals(1, rd0.getLowerBound());
    Assert.assertEquals(1, rd0.getUpperBound());
    RequirementDefinition rd1 = nodeType.getRequirements().get(1);
    Assert.assertEquals("other", rd1.getId());
    Assert.assertEquals(0, rd1.getLowerBound());
    Assert.assertEquals(Integer.MAX_VALUE, rd1.getUpperBound());
}
Also used : CapabilityType(org.alien4cloud.tosca.model.types.CapabilityType) Set(java.util.Set) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) RequirementDefinition(org.alien4cloud.tosca.model.definitions.RequirementDefinition) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) LessThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.LessThanConstraint) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) NodeType(org.alien4cloud.tosca.model.types.NodeType) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) GreaterThanConstraint(org.alien4cloud.tosca.model.definitions.constraints.GreaterThanConstraint) Test(org.junit.Test)

Example 17 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class DeploymentTopologyStepDefinitions method The_deployment_topology_sould_have_the_following_input_properties.

@Then("^the deployment topology should have the following inputs properties$")
public void The_deployment_topology_sould_have_the_following_input_properties(Map<String, String> expectedStringInputProperties) throws Throwable {
    DeploymentTopologyDTO dto = getDTOAndassertNotNull();
    Map<String, AbstractPropertyValue> expectedInputProperties = Maps.newHashMap();
    for (Entry<String, String> inputEntry : expectedStringInputProperties.entrySet()) {
        expectedInputProperties.put(inputEntry.getKey(), new ScalarPropertyValue(inputEntry.getValue()));
    }
    assertPropMapContains(dto.getTopology().getAllInputProperties(), expectedInputProperties);
}
Also used : ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) DeploymentTopologyDTO(alien4cloud.deployment.DeploymentTopologyDTO) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) Then(cucumber.api.java.en.Then)

Example 18 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class NodeFilterValidationService method validatePropertyFilters.

private List<Violations> validatePropertyFilters(Map<String, List<PropertyConstraint>> propertyFilters, Map<String, AbstractPropertyValue> propertyValues, Map<String, PropertyDefinition> propertyDefinitionMap, boolean skipInputs) {
    List<Violations> violations = Lists.newArrayList();
    for (Map.Entry<String, List<PropertyConstraint>> propertyEntry : propertyFilters.entrySet()) {
        Violations violation = new Violations(propertyEntry.getKey());
        List<NodeFilterConstraintViolation> violatedConstraints = Lists.newArrayList();
        violation.violatedConstraints = violatedConstraints;
        AbstractPropertyValue value = propertyValues.get(propertyEntry.getKey());
        String propertyValue = null;
        if (value == null) {
            propertyValue = null;
        } else if (value instanceof ScalarPropertyValue) {
            propertyValue = ((ScalarPropertyValue) value).getValue();
        } else {
            if (skipInputs) {
                continue;
            }
            if (FunctionEvaluator.isGetInput((FunctionPropertyValue) value)) {
                violation.relatedInput = ((FunctionPropertyValue) value).getElementNameToFetch();
            }
        }
        for (PropertyConstraint constraint : propertyEntry.getValue()) {
            if (!propertyDefinitionMap.containsKey(propertyEntry.getKey())) {
                continue;
            }
            // the constraint need to be initiazed with the type of the property (to check that actual value type matches the definition type).
            IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(propertyDefinitionMap.get(propertyEntry.getKey()).getType());
            try {
                constraint.initialize(toscaType);
                constraint.validate(toscaType, propertyValue);
            } catch (ConstraintViolationException e) {
                violatedConstraints.add(new NodeFilterConstraintViolation(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR, e.getMessage(), e.getConstraintInformation()));
            } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
                violatedConstraints.add(new NodeFilterConstraintViolation(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR, e.getMessage(), null));
            }
        }
        if (!violatedConstraints.isEmpty()) {
            violations.add(violation);
        }
    }
    return violations;
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) NodeFilterConstraintViolation(alien4cloud.topology.task.NodeFilterConstraintViolation) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) Violations(alien4cloud.topology.task.NodeFilterToSatisfy.Violations) FunctionPropertyValue(org.alien4cloud.tosca.model.definitions.FunctionPropertyValue) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 19 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue in project alien4cloud by alien4cloud.

the class TopologyPropertiesValidationService method addRequiredPropertyIdToTaskProperties.

private void addRequiredPropertyIdToTaskProperties(String prefix, Map<String, AbstractPropertyValue> properties, Map<String, PropertyDefinition> relatedProperties, PropertiesTask task, boolean skipInputProperties) {
    for (Map.Entry<String, AbstractPropertyValue> propertyEntry : properties.entrySet()) {
        PropertyDefinition propertyDef = relatedProperties.get(propertyEntry.getKey());
        String propertyErrorKey = prefix == null ? propertyEntry.getKey() : prefix + "." + propertyEntry.getKey();
        AbstractPropertyValue value = propertyEntry.getValue();
        if (propertyDef != null && propertyDef.isRequired()) {
            if (value == null) {
                addRequiredPropertyError(task, propertyErrorKey);
            } else if (value instanceof ScalarPropertyValue) {
                String propertyValue = ((ScalarPropertyValue) value).getValue();
                if (StringUtils.isBlank(propertyValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ComplexPropertyValue) {
                Map<String, Object> mapValue = ((ComplexPropertyValue) value).getValue();
                if (MapUtils.isEmpty(mapValue)) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (value instanceof ListPropertyValue) {
                List<Object> listValue = ((ListPropertyValue) value).getValue();
                if (listValue.isEmpty()) {
                    addRequiredPropertyError(task, propertyErrorKey);
                }
            } else if (FunctionEvaluator.containGetSecretFunction(value)) {
                // this is a get_secret function, we should not validate the get_secret here
                continue;
            } else if (skipInputProperties) {
                // get_input Will be validated later on
                continue;
            } else {
                addRequiredPropertyError(task, propertyErrorKey);
            }
        }
    }
}
Also used : ListPropertyValue(org.alien4cloud.tosca.model.definitions.ListPropertyValue) List(java.util.List) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) ComplexPropertyValue(org.alien4cloud.tosca.model.definitions.ComplexPropertyValue) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Example 20 with ScalarPropertyValue

use of org.alien4cloud.tosca.model.definitions.ScalarPropertyValue 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);
    }
}
Also used : ConstraintUtil(alien4cloud.tosca.properties.constraints.ConstraintUtil) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ScalarPropertyValue(org.alien4cloud.tosca.model.definitions.ScalarPropertyValue) EqualConstraint(org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint) SneakyThrows(lombok.SneakyThrows)

Aggregations

ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)37 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)15 PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)14 Test (org.junit.Test)13 Map (java.util.Map)10 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 ListPropertyValue (org.alien4cloud.tosca.model.definitions.ListPropertyValue)8 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)7 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)7 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 HashMap (java.util.HashMap)5 List (java.util.List)5 Set (java.util.Set)5 EqualConstraint (org.alien4cloud.tosca.model.definitions.constraints.EqualConstraint)5 NodeType (org.alien4cloud.tosca.model.types.NodeType)5 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)4 ConstraintViolationException (org.alien4cloud.tosca.exceptions.ConstraintViolationException)4 ConcatPropertyValue (org.alien4cloud.tosca.model.definitions.ConcatPropertyValue)4 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)4 Capability (org.alien4cloud.tosca.model.templates.Capability)4