use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue 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);
}
}
}
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class SetMatchedNodePropertyModifier method setNodeCapabilityProperty.
private void setNodeCapabilityProperty(FlowExecutionContext context, LocationResourceTemplate locationResourceTemplate, NodeTemplate nodeTemplate, String capabilityName, DeploymentMatchingConfiguration matchingConfiguration) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
Capability locationResourceCapability = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName);
if (locationResourceCapability == null) {
throw new NotFoundException("The capability <" + capabilityName + "> cannot be found on node template <" + templateId + "> of type <" + locationResourceTemplate.getTemplate().getType() + ">");
}
PropertyDefinition propertyDefinition = ToscaContext.getOrFail(CapabilityType.class, locationResourceCapability.getType()).getProperties().get(propertyName);
if (propertyDefinition == null) {
throw new NotFoundException("No property with name <" + propertyName + "> can be found on capability <" + capabilityName + "> of type <" + locationResourceCapability.getType() + ">");
}
AbstractPropertyValue locationResourcePropertyValue = locationResourceTemplate.getTemplate().getCapabilities().get(capabilityName).getProperties().get(propertyName);
ensureNotSet(locationResourcePropertyValue, "by the admin in the Location Resource Template", propertyName, propertyValue);
AbstractPropertyValue originalNodePropertyValue = safe(nodeTemplate.getCapabilities().get(capabilityName).getProperties()).get(propertyName);
ensureNotSet(originalNodePropertyValue, "in the portable topology", propertyName, propertyValue);
// Update the configuration
NodePropsOverride nodePropsOverride = getTemplatePropsOverride(matchingConfiguration);
if (propertyValue == null && nodePropsOverride.getCapabilities().get(capabilityName) != null) {
nodePropsOverride.getCapabilities().get(capabilityName).getProperties().remove(propertyName);
} else {
// Set check constraints
ConstraintPropertyService.checkPropertyConstraint(propertyName, propertyValue, propertyDefinition);
NodeCapabilitiesPropsOverride nodeCapabilitiesPropsOverride = nodePropsOverride.getCapabilities().computeIfAbsent(capabilityName, k -> new NodeCapabilitiesPropsOverride());
nodeCapabilitiesPropsOverride.getProperties().put(propertyName, PropertyService.asPropertyValue(propertyValue));
}
context.saveConfiguration(matchingConfiguration);
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class InputsModifier method process.
@Override
public void process(Topology topology, FlowExecutionContext context) {
EnvironmentContext environmentContext = context.getEnvironmentContext().orElseThrow(() -> new IllegalArgumentException("Input modifier requires an environment context."));
ApplicationEnvironment environment = environmentContext.getEnvironment();
DeploymentInputs deploymentInputs = context.getConfiguration(DeploymentInputs.class, InputsModifier.class.getSimpleName()).orElse(new DeploymentInputs(environment.getTopologyVersion(), environment.getId()));
if (deploymentInputs.getInputs() == null) {
deploymentInputs.setInputs(Maps.newHashMap());
}
Map<String, Location> locations = (Map<String, Location>) context.getExecutionCache().get(FlowExecutionContext.DEPLOYMENT_LOCATIONS_MAP_CACHE_KEY);
Map<String, PropertyValue> applicationInputs = inputService.getAppContextualInputs(context.getEnvironmentContext().get().getApplication(), topology.getInputs());
Map<String, PropertyValue> locationInputs = inputService.getLocationContextualInputs(locations, topology.getInputs());
// If the initial topology or any modifier configuration has changed since last inputs update then we refresh inputs.
if (deploymentInputs.getLastUpdateDate() == null || deploymentInputs.getLastUpdateDate().before(context.getLastFlowParamUpdate())) {
// FIXME exclude the application and location provided inputs from this method as it process them...
boolean updated = deploymentInputService.synchronizeInputs(topology.getInputs(), deploymentInputs.getInputs());
if (updated) {
// save the config if changed. This is for ex, if an input has been deleted from the topology
context.saveConfiguration(deploymentInputs);
}
}
PreconfiguredInputsConfiguration preconfiguredInputsConfiguration = context.getConfiguration(PreconfiguredInputsConfiguration.class, InputsModifier.class.getSimpleName()).orElseThrow(() -> new IllegalStateException("PreconfiguredInputsConfiguration must be in the context"));
Map<String, AbstractPropertyValue> inputValues = Maps.newHashMap(deploymentInputs.getInputs());
inputValues.putAll(applicationInputs);
inputValues.putAll(locationInputs);
inputValues.putAll(preconfiguredInputsConfiguration.getInputs());
// Now that we have inputs ready let's process get_input functions in the topology to actually replace values.
if (topology.getNodeTemplates() != null) {
FunctionEvaluatorContext evaluatorContext = new FunctionEvaluatorContext(topology, inputValues);
for (Entry<String, NodeTemplate> entry : topology.getNodeTemplates().entrySet()) {
NodeTemplate nodeTemplate = entry.getValue();
processGetInput(evaluatorContext, nodeTemplate, nodeTemplate.getProperties());
if (nodeTemplate.getRelationships() != null) {
for (Entry<String, RelationshipTemplate> relEntry : nodeTemplate.getRelationships().entrySet()) {
RelationshipTemplate relationshipTemplate = relEntry.getValue();
processGetInput(evaluatorContext, relationshipTemplate, relationshipTemplate.getProperties());
}
}
if (nodeTemplate.getCapabilities() != null) {
for (Entry<String, Capability> capaEntry : nodeTemplate.getCapabilities().entrySet()) {
Capability capability = capaEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, capability.getProperties());
}
}
if (nodeTemplate.getRequirements() != null) {
for (Entry<String, Requirement> requirementEntry : nodeTemplate.getRequirements().entrySet()) {
Requirement requirement = requirementEntry.getValue();
processGetInput(evaluatorContext, nodeTemplate, requirement.getProperties());
}
}
}
}
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue in project alien4cloud by alien4cloud.
the class TopologyModifierSupport method setNodePropertyPathValue.
private void setNodePropertyPathValue(Csar csar, Topology topology, NodeTemplate nodeTemplate, String propertyPath, AbstractPropertyValue propertyValue, boolean lastPropertyIsAList) {
Map<String, AbstractPropertyValue> propertyValues = nodeTemplate.getProperties();
String nodePropertyName = feedPropertyValue(propertyValues, propertyPath, propertyValue, lastPropertyIsAList);
Object nodePropertyValue = propertyValues.get(nodePropertyName);
UpdateNodePropertyValueOperation updateNodePropertyValueOperation = new UpdateNodePropertyValueOperation();
updateNodePropertyValueOperation.setNodeName(nodeTemplate.getName());
updateNodePropertyValueOperation.setPropertyName(nodePropertyName);
// TODO: can be necessary to serialize value before setting it in case of different types
updateNodePropertyValueOperation.setPropertyValue(nodePropertyValue);
updateNodePropertyValueProcessor.process(csar, topology, updateNodePropertyValueOperation);
}
use of org.alien4cloud.tosca.model.definitions.AbstractPropertyValue 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;
}
Aggregations