use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class InputValidationModifier method process.
/**
* Validate all required input is provided with a non null value.
*
* @param topology The topology to process.
* @param context The object that stores warnings and errors (tasks) associated with the execution flow. Note that the flow will end-up if an error
*/
@Override
public void process(Topology topology, FlowExecutionContext context) {
Optional<DeploymentInputs> inputsOptional = context.getConfiguration(DeploymentInputs.class, InputValidationModifier.class.getSimpleName());
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputValidationModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
// Define a task regarding properties
PropertiesTask task = new PropertiesTask();
task.setCode(TaskCode.INPUT_PROPERTY);
task.setProperties(Maps.newHashMap());
task.getProperties().put(TaskLevel.REQUIRED, Lists.newArrayList());
Map<String, AbstractPropertyValue> inputValues = safe(inputsOptional.orElse(new DeploymentInputs()).getInputs());
Map<String, PropertyValue> predefinedInputValues = safe(preconfiguredInputsConfiguration.getInputs());
// override deployer inputValues with predefinedInputValues
inputValues = Maps.newHashMap(inputValues);
inputValues.putAll(predefinedInputValues);
for (Entry<String, PropertyDefinition> propDef : safe(topology.getInputs()).entrySet()) {
if (propDef.getValue().isRequired() && inputValues.get(propDef.getKey()) == null) {
task.getProperties().get(TaskLevel.REQUIRED).add(propDef.getKey());
}
}
if (CollectionUtils.isNotEmpty(task.getProperties().get(TaskLevel.REQUIRED))) {
context.log().error(task);
}
// Check input artifacts
deploymentInputArtifactValidationService.validate(topology, inputsOptional.orElse(new DeploymentInputs())).forEach(inputArtifactTask -> context.log().error(inputArtifactTask));
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class FunctionEvaluator method getFromPath.
private static AbstractPropertyValue getFromPath(FunctionEvaluatorContext evaluatorContext, AbstractInstantiableTemplate targetTemplate, Map<String, AbstractPropertyValue> properties, String propertyPath) {
if (propertyPath.contains(".")) {
String propertyName = propertyPath.split("\\.")[0];
AbstractPropertyValue propertyValue = properties.get(propertyName);
if (!(propertyValue instanceof PropertyValue)) {
// if the value is not a property value resolve it first
propertyValue = tryResolveValue(evaluatorContext, targetTemplate, properties, propertyValue);
if (propertyValue == null) {
return null;
}
}
// now it is a property value
Object value = MapUtil.get(((PropertyValue) propertyValue).getValue(), propertyPath.substring(propertyName.length() + 1));
if (value == null) {
return null;
} else if (value instanceof String) {
return new ScalarPropertyValue((String) value);
} else if (value instanceof List) {
return new ListPropertyValue((List<Object>) value);
} else if (value instanceof Map) {
return new ComplexPropertyValue((Map<String, Object>) value);
}
throw new IllegalArgumentException("The value of a property must be a scalar, a list or a map.");
}
return safe(properties).get(propertyPath);
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class FunctionEvaluatorTest method nodeGetComplexProp.
@Test
public void nodeGetComplexProp() {
FunctionEvaluatorContext context = getEvaluationContext();
NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
PropertyValue resolved = resolveValue(context, template, template.getProperties(), template.getProperties().get("get_complex_prop"));
Assert.assertNotNull(resolved);
Assert.assertEquals(ScalarPropertyValue.class, resolved.getClass());
Assert.assertEquals("complex scalar value", resolved.getValue());
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class FunctionEvaluatorTest method nodeGetScalarProp.
@Test
public void nodeGetScalarProp() {
FunctionEvaluatorContext context = getEvaluationContext();
NodeTemplate template = context.getTopology().getNodeTemplates().get("my_node");
PropertyValue resolved = resolveValue(context, template, template.getProperties(), template.getProperties().get("get_scalar_prop"));
Assert.assertNotNull(resolved);
Assert.assertEquals(ScalarPropertyValue.class, resolved.getClass());
Assert.assertEquals("scalar value", resolved.getValue());
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class InputsMappingFileVariableResolverTest method check_inputs_mapping_can_be_parsed_when_variable.
@Ignore("Update when ToscaTypeConverter behavior is clearly defined")
@Test
public // Bad test
void check_inputs_mapping_can_be_parsed_when_variable() throws Exception {
inputsMappingFileVariableResolverConfigured.customConverter(new ToscaTypeConverter((concreteType, id) -> {
if (id.equals("datatype.complex_input_entry.sub1")) {
DataType dataType = new DataType();
dataType.setDeriveFromSimpleType(false);
dataType.setProperties(//
ImmutableMap.of(//
"complex", //
buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING)));
return dataType;
}
if (id.equals("datatype.complex_input_entry")) {
DataType dataType = new DataType();
dataType.setDeriveFromSimpleType(false);
dataType.setProperties(//
ImmutableMap.of(//
"sub1", //
buildPropDef("datatype.complex_input_entry.sub1"), //
"sub2", //
buildPropDef(ToscaTypes.MAP, ToscaTypes.STRING), //
"field01", //
buildPropDef(ToscaTypes.STRING)));
return dataType;
}
return null;
}));
Map<String, PropertyValue> inputsMappingFileResolved = resolve("src/test/resources/alien/variables/inputs_mapping_with_variables.yml");
assertThat(inputsMappingFileResolved.get("int_input")).isEqualTo(new ScalarPropertyValue("1"));
assertThat(inputsMappingFileResolved.get("float_input")).isEqualTo(new ScalarPropertyValue("3.14"));
assertThat(inputsMappingFileResolved.get("string_input")).isEqualTo(new ScalarPropertyValue("text_3.14"));
//
assertThat(inputsMappingFileResolved.get("complex_input")).isEqualTo(new ComplexPropertyValue(//
ImmutableMap.of("sub1", new ComplexPropertyValue(//
ImmutableMap.of("complex", //
new ComplexPropertyValue(ImmutableMap.of("subfield", new ScalarPropertyValue("text"))))), "sub2", new ComplexPropertyValue(//
ImmutableMap.of("subfield21", //
new ScalarPropertyValue("1"))), "field01", //
new ScalarPropertyValue("text"))));
}
Aggregations