use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class ToscaPropertyDefaultValueConstraintsValidator method isValid.
@Override
public boolean isValid(PropertyDefinition value, ConstraintValidatorContext context) {
PropertyValue defaultValue = value.getDefault();
if (defaultValue == null) {
// no default value is specified.
return true;
}
// validate that the default value matches the defined constraints.
IPropertyType<?> toscaType = ToscaTypes.fromYamlTypeName(value.getType());
if (toscaType == null) {
return false;
}
if (!(defaultValue instanceof ScalarPropertyValue)) {
// No constraint can be made on other thing than scalar values
return false;
}
String defaultValueAsString = ((ScalarPropertyValue) defaultValue).getValue();
Object parsedDefaultValue;
try {
parsedDefaultValue = toscaType.parse(defaultValueAsString);
} catch (InvalidPropertyValueException e) {
return false;
}
if (value.getConstraints() != null) {
for (PropertyConstraint constraint : value.getConstraints()) {
try {
constraint.validate(parsedDefaultValue);
} catch (ConstraintViolationException e) {
return false;
}
}
}
return true;
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class DeploymentInputService method synchronizeInputs.
/**
* Ensure that the specified input values matches the eventually updated input definitions.
*
* @param inputDefinitions Inputs definitions as specified in the topology.
* @param inputValues Input properties values as specified by the user.
* @return true if there is an update on inputValues (removal or addition). false if nothing has changed
*/
public boolean synchronizeInputs(Map<String, PropertyDefinition> inputDefinitions, Map<String, AbstractPropertyValue> inputValues) {
boolean updated = false;
if (!MapUtils.isEmpty(inputValues)) {
// Ensure that previous defined values are still compatible with the latest input definition (as the topology may have changed).
Iterator<Map.Entry<String, AbstractPropertyValue>> inputPropertyEntryIterator = inputValues.entrySet().iterator();
while (inputPropertyEntryIterator.hasNext()) {
Map.Entry<String, AbstractPropertyValue> inputPropertyEntry = inputPropertyEntryIterator.next();
// remove if the value is null, or the input is not register as one
if (inputPropertyEntry.getValue() == null || !safe(inputDefinitions).containsKey(inputPropertyEntry.getKey())) {
inputPropertyEntryIterator.remove();
} else if (!(inputPropertyEntry.getValue() instanceof FunctionPropertyValue)) {
try {
ConstraintPropertyService.checkPropertyConstraint(inputPropertyEntry.getKey(), ((PropertyValue) inputPropertyEntry.getValue()).getValue(), inputDefinitions.get(inputPropertyEntry.getKey()));
} catch (ConstraintViolationException | ConstraintValueDoNotMatchPropertyTypeException e) {
// Property is not valid anymore for the input, remove the old value
inputPropertyEntryIterator.remove();
updated = true;
}
}
}
}
// set default values for every unset property.
for (Map.Entry<String, PropertyDefinition> inputDefinitionEntry : safe(inputDefinitions).entrySet()) {
AbstractPropertyValue existingValue = inputValues.get(inputDefinitionEntry.getKey());
if (existingValue == null) {
// If user has not specified a value and there is
PropertyValue defaultValue = inputDefinitionEntry.getValue().getDefault();
if (defaultValue != null) {
inputValues.put(inputDefinitionEntry.getKey(), defaultValue);
updated = true;
}
}
}
return updated;
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class InputService method getLocationContextualInputs.
/**
* Get input values matching the requested input definitions as defined in location meta properties.
*
* @param locations The map of locations from which to fetch meta properties based inputs.
* @param inputDefinitions The input definitions that may define request for location meta based inputs.
* @return A map of <Input name, Property value> computed from the location meta properties.
*/
public Map<String, PropertyValue> getLocationContextualInputs(Map<String, Location> locations, Map<String, PropertyDefinition> inputDefinitions) {
if (inputDefinitions == null) {
return Maps.newHashMap();
}
Map<String, String> metaPropertiesValuesMap = Maps.newHashMap();
for (Location location : safe(locations).values()) {
metaPropertiesValuesMap.putAll(safe(location.getMetaProperties()));
}
Map<String, PropertyValue> inputs = Maps.newHashMap();
prefixAndAddContextInput(inputDefinitions, inputs, LOC_META, metaPropertiesValuesMap, true);
return inputs;
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class ArchiveRootPostProcessor method processRepositoriesDefinitions.
private void processRepositoriesDefinitions(Map<String, RepositoryDefinition> repositories) {
if (MapUtils.isNotEmpty(repositories)) {
DataType credentialType = ToscaContext.get(DataType.class, NormativeCredentialConstant.DATA_TYPE);
repositories.values().forEach(repositoryDefinition -> {
if (repositoryDefinition.getCredential() != null) {
credentialType.getProperties().forEach((propertyName, propertyDefinition) -> {
// Fill with default value
if (!repositoryDefinition.getCredential().getValue().containsKey(propertyName)) {
AbstractPropertyValue defaultValue = PropertyUtil.getDefaultPropertyValueFromPropertyDefinition(propertyDefinition);
if (defaultValue instanceof PropertyValue) {
repositoryDefinition.getCredential().getValue().put(propertyName, ((PropertyValue) defaultValue).getValue());
}
}
});
Node credentialNode = ParsingContextExecution.getObjectToNodeMap().get(repositoryDefinition.getCredential());
PropertyDefinition propertyDefinition = new PropertyDefinition();
propertyDefinition.setType(NormativeCredentialConstant.DATA_TYPE);
propertyValueChecker.checkProperty("credential", credentialNode, repositoryDefinition.getCredential(), propertyDefinition, repositoryDefinition.getId());
}
});
}
}
use of org.alien4cloud.tosca.model.definitions.PropertyValue in project alien4cloud by alien4cloud.
the class PreconfiguredInputsModifier method process.
@Override
public void process(Topology topology, FlowExecutionContext context) {
EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Preconfigured input modifier requires an environment context."));
ApplicationEnvironment environment = environmentContext.getEnvironment();
Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
AlienContextVariables alienContextVariables = new AlienContextVariables();
alienContextVariables.setApplicationEnvironment(environment);
alienContextVariables.setLocation(locations.values().stream().findFirst().get());
alienContextVariables.setApplication(environmentContext.getApplication());
// TODO: avoid reloading every time - find a way to know the last update on files (git hash ?)
Properties appVarProps = quickFileStorageService.loadApplicationVariables(environmentContext.getApplication().getId());
Properties envTypeVarProps = quickFileStorageService.loadEnvironmentTypeVariables(topology.getId(), environment.getEnvironmentType());
Properties envVarProps = quickFileStorageService.loadEnvironmentVariables(topology.getId(), environment.getId());
Map<String, Object> inputsMappingsMap = quickFileStorageService.loadInputsMappingFile(topology.getId());
InputsMappingFileVariableResolver.InputsResolvingResult inputsResolvingResult = InputsMappingFileVariableResolver.configure(appVarProps, envTypeVarProps, envVarProps, alienContextVariables).resolve(inputsMappingsMap, topology.getInputs());
if (CollectionUtils.isNotEmpty(inputsResolvingResult.getMissingVariables())) {
context.log().error(new MissingVariablesTask(inputsResolvingResult.getMissingVariables()));
}
if (CollectionUtils.isNotEmpty(inputsResolvingResult.getUnresolved())) {
context.log().error(new UnresolvablePredefinedInputsTask(inputsResolvingResult.getUnresolved()));
}
// checking constraints
Map<String, ConstraintUtil.ConstraintInformation> violations = Maps.newHashMap();
Map<String, ConstraintUtil.ConstraintInformation> typesViolations = Maps.newHashMap();
for (Map.Entry<String, PropertyValue> entry : safe(inputsResolvingResult.getResolved()).entrySet()) {
try {
ConstraintPropertyService.checkPropertyConstraint(entry.getKey(), entry.getValue(), topology.getInputs().get(entry.getKey()));
} catch (ConstraintViolationException e) {
violations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
} catch (ConstraintValueDoNotMatchPropertyTypeException e) {
typesViolations.put(entry.getKey(), getConstraintInformation(e.getMessage(), e.getConstraintInformation()));
}
}
if (MapUtils.isNotEmpty(violations)) {
context.log().error(new PredefinedInputsConstraintViolationTask(violations, TaskCode.PREDEFINED_INPUTS_CONSTRAINT_VIOLATION));
}
if (MapUtils.isNotEmpty(typesViolations)) {
context.log().error(new PredefinedInputsConstraintViolationTask(typesViolations, TaskCode.PREDEFINED_INPUTS_TYPE_VIOLATION));
}
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = new PreconfiguredInputsConfiguration(environment.getTopologyVersion(), environment.getId());
preconfiguredInputsConfiguration.setInputs(inputsResolvingResult.getResolved());
// add unresolved so that they are not considered as deployer input
inputsResolvingResult.getUnresolved().forEach(unresolved -> preconfiguredInputsConfiguration.getInputs().put(unresolved, null));
// TODO: improve me
preconfiguredInputsConfiguration.setLastUpdateDate(new Date());
preconfiguredInputsConfiguration.setCreationDate(new Date());
context.saveConfiguration(preconfiguredInputsConfiguration);
}
Aggregations