Search in sources :

Example 96 with PropertyDefinition

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

the class ConstraintPropertyService method checkListPropertyConstraint.

private static void checkListPropertyConstraint(String propertyName, List<Object> listPropertyValue, PropertyDefinition propertyDefinition, Consumer<String> missingPropertyConsumer) throws ConstraintValueDoNotMatchPropertyTypeException, ConstraintViolationException {
    if (!ToscaTypes.LIST.equals(propertyDefinition.getType())) {
        throwConstraintValueDoNotMatchPropertyTypeException("The property definition should be a list but we found " + propertyDefinition.getType(), propertyName, ToscaTypes.LIST, null);
    }
    PropertyDefinition entrySchema = propertyDefinition.getEntrySchema();
    if (entrySchema == null) {
        throw new ConstraintValueDoNotMatchPropertyTypeException("value is a list but type actually is <" + propertyDefinition.getType() + ">");
    }
    checkLengthConstraints(propertyDefinition.getConstraints(), listPropertyValue);
    for (int i = 0; i < listPropertyValue.size(); i++) {
        checkPropertyConstraint(propertyName + "[" + String.valueOf(i) + "]", listPropertyValue.get(i), entrySchema, missingPropertyConsumer);
    }
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) PropertyConstraint(org.alien4cloud.tosca.model.definitions.PropertyConstraint) LengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint) MaxLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint) MinLengthConstraint(org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)

Example 97 with PropertyDefinition

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

the class RuntimeController method validateParameters.

private void validateParameters(Interface interfass, OperationExecRequest operationRequest, Set<CSARDependency> dependencies) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException, ConstraintRequiredParameterException {
    try {
        if (dependencies != null) {
            ToscaContext.init(dependencies);
        }
        ArrayList<String> missingParams = Lists.newArrayList();
        Operation operation = interfass.getOperations().get(operationRequest.getOperationName());
        if (operation.getInputParameters() != null) {
            for (Entry<String, IValue> inputParameter : operation.getInputParameters().entrySet()) {
                if (inputParameter.getValue().isDefinition()) {
                    Object requestInputParameter = operationRequest.getParameters() == null ? null : operationRequest.getParameters().get(inputParameter.getKey());
                    PropertyDefinition currentOperationParameter = (PropertyDefinition) inputParameter.getValue();
                    if (requestInputParameter != null) {
                        if (!(requestInputParameter instanceof Map) || !FunctionEvaluator.containGetSecretFunction(PropertyService.asFunctionPropertyValue(requestInputParameter))) {
                            // recover the good property definition for the current parameter
                            ConstraintPropertyService.checkPropertyConstraint(inputParameter.getKey(), requestInputParameter, currentOperationParameter);
                        }
                    } else if (currentOperationParameter.isRequired()) {
                        // input param not in the request, id required this is a missing parameter...
                        missingParams.add(inputParameter.getKey());
                    } else {
                        // set the value to null
                        operation.getInputParameters().put(inputParameter.getKey(), null);
                    }
                }
            }
        }
        // check required input issue
        if (!missingParams.isEmpty()) {
            log.error("Missing required parameter", missingParams);
            ConstraintInformation constraintInformation = new ConstraintInformation(null, null, missingParams.toString(), "required");
            throw new ConstraintRequiredParameterException("Missing required parameters", null, constraintInformation);
        }
    } finally {
        if (ToscaContext.get() != null) {
            ToscaContext.destroy();
        }
    }
}
Also used : IValue(org.alien4cloud.tosca.model.definitions.IValue) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) ApiOperation(io.swagger.annotations.ApiOperation) Operation(org.alien4cloud.tosca.model.definitions.Operation) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Map(java.util.Map) ConstraintRequiredParameterException(org.alien4cloud.tosca.exceptions.ConstraintRequiredParameterException)

Example 98 with PropertyDefinition

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

the class InputPropertiesStepDefinitions method The_topology_should_not_have_the_property_defined_as_input_property.

@Then("^The topology should not have the property \"([^\"]*)\" defined as input property$")
public void The_topology_should_not_have_the_property_defined_as_input_property(String inputId) throws Throwable {
    TopologyDTO topologyDTO = JsonUtil.read(Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId()), TopologyDTO.class, Context.getJsonMapper()).getData();
    Map<String, PropertyDefinition> inputProperties = topologyDTO.getTopology().getInputs();
    Assert.assertFalse(inputProperties.containsKey(inputId));
}
Also used : TopologyDTO(alien4cloud.topology.TopologyDTO) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Then(cucumber.api.java.en.Then)

Example 99 with PropertyDefinition

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

the class InputPropertiesStepDefinitions method The_topology_should_have_the_property_of_the_node_defined_as_input_property.

@Then("^The topology should have the property \"([^\"]*)\" defined as input property$")
public void The_topology_should_have_the_property_of_the_node_defined_as_input_property(String inputId) throws Throwable {
    String response = Context.getRestClientInstance().get("/rest/v1/topologies/" + Context.getInstance().getTopologyId());
    JavaType restResponseType = Context.getJsonMapper().getTypeFactory().constructParametricType(RestResponse.class, TopologyDTO.class);
    TopologyDTO topologyDTO = ((RestResponse<TopologyDTO>) Context.getJsonMapper().readValue(response, restResponseType)).getData();
    Map<String, PropertyDefinition> inputProperties = topologyDTO.getTopology().getInputs();
    Assert.assertNotNull(inputProperties);
    PropertyDefinition inputPropertieDefinition = inputProperties.get(inputId);
    Assert.assertNotNull(inputPropertieDefinition);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) TopologyDTO(alien4cloud.topology.TopologyDTO) RestResponse(alien4cloud.rest.model.RestResponse) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition) Then(cucumber.api.java.en.Then)

Example 100 with PropertyDefinition

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

the class PropertyValueChecker method checkProperties.

/**
 * Check that the value of a property has the right type and match constraints.
 *
 * @param type The type that defines the properties (NodeType, CapabilityType, RequirementType).
 * @param propertyValues The map of values.
 * @param templateName The name of the node template /capability template / requirement template.
 */
public void checkProperties(final AbstractInheritableToscaType type, final Map<String, AbstractPropertyValue> propertyValues, final String templateName) {
    if (type == null) {
        // if the type is null we cannot check properties against their definition. Error is managed elsewhere.
        return;
    }
    ArchiveRoot archiveRoot = (ArchiveRoot) ParsingContextExecution.getRoot().getWrappedInstance();
    Topology topology = archiveRoot.getTopology();
    for (Map.Entry<String, AbstractPropertyValue> propertyEntry : safe(propertyValues).entrySet()) {
        String propertyName = propertyEntry.getKey();
        AbstractPropertyValue propertyValue = propertyEntry.getValue();
        Node propertyValueNode = ParsingContextExecution.getObjectToNodeMap().get(propertyValue);
        if (type.getProperties() == null || !type.getProperties().containsKey(propertyName)) {
            ParsingContextExecution.getParsingErrors().add(new ParsingError(ParsingErrorLevel.ERROR, ErrorCode.UNRECOGNIZED_PROPERTY, templateName, propertyValueNode.getStartMark(), "Property " + propertyName + " does not exist in type " + type.getElementId(), propertyValueNode.getEndMark(), propertyName));
            continue;
        }
        PropertyDefinition propertyDefinition = type.getProperties().get(propertyName);
        checkProperty(propertyName, propertyValueNode, propertyValue, propertyDefinition, topology.getInputs(), templateName);
    }
}
Also used : ArchiveRoot(alien4cloud.tosca.model.ArchiveRoot) ParsingError(alien4cloud.tosca.parser.ParsingError) Node(org.yaml.snakeyaml.nodes.Node) Topology(org.alien4cloud.tosca.model.templates.Topology) Map(java.util.Map) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue) PropertyDefinition(org.alien4cloud.tosca.model.definitions.PropertyDefinition)

Aggregations

PropertyDefinition (org.alien4cloud.tosca.model.definitions.PropertyDefinition)101 Test (org.junit.Test)42 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)23 PropertyConstraint (org.alien4cloud.tosca.model.definitions.PropertyConstraint)20 Map (java.util.Map)19 ScalarPropertyValue (org.alien4cloud.tosca.model.definitions.ScalarPropertyValue)17 NodeType (org.alien4cloud.tosca.model.types.NodeType)15 NotFoundException (alien4cloud.exception.NotFoundException)13 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)13 FunctionPropertyValue (org.alien4cloud.tosca.model.definitions.FunctionPropertyValue)11 MinLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MinLengthConstraint)11 PropertyValue (org.alien4cloud.tosca.model.definitions.PropertyValue)10 MaxLengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.MaxLengthConstraint)10 Capability (org.alien4cloud.tosca.model.templates.Capability)10 CapabilityType (org.alien4cloud.tosca.model.types.CapabilityType)10 DataType (org.alien4cloud.tosca.model.types.DataType)9 ArchiveRoot (alien4cloud.tosca.model.ArchiveRoot)8 Set (java.util.Set)8 ComplexPropertyValue (org.alien4cloud.tosca.model.definitions.ComplexPropertyValue)8 LengthConstraint (org.alien4cloud.tosca.model.definitions.constraints.LengthConstraint)8