|
57 | 57 |
|
58 | 58 | import javax.inject.Inject; |
59 | 59 | import javax.inject.Named; |
| 60 | +import java.util.ArrayList; |
60 | 61 | import java.util.Collections; |
61 | 62 | import java.util.HashMap; |
62 | 63 | import java.util.HashSet; |
@@ -367,32 +368,56 @@ protected void populateNodeTemplateAttributes(ToscaNodeTemplate nodeTemplate, Ma |
367 | 368 | * or when the return value does not pass the validation function. |
368 | 369 | */ |
369 | 370 | protected void executeGetAttributeAndGetPropertyFunctionCalls(ToscaNodeTemplate nodeTemplate, ToscaServiceTemplate serviceTemplate) { |
370 | | - Set<ToscaGetterFunctionCallContext> functionCalls = new HashSet<>(nodeTemplate.getGetPropertyFunctionCalls()); |
371 | | - functionCalls.addAll(nodeTemplate.getGetAttributeFunctionCalls()); |
372 | | - if (CollectionUtils.isEmpty(functionCalls)) { |
373 | | - logger.debug("Node template [{}] has no function calls to the [{}] and [{}] TOSCA function.", nodeTemplate.getName(), ToscaConstants.GET_ATTRIBUTE_FUNCTION, ToscaConstants.GET_PROPERTY_FUNCTION); |
| 371 | + Set<ToscaProperty> unresolvedProperties = new HashSet<>(nodeTemplate.getUnresolvedPropertiesByGetProperty()); |
| 372 | + unresolvedProperties.addAll(nodeTemplate.getUnresolvedPropertiesByGetAttribute()); |
| 373 | + if (CollectionUtils.isEmpty(unresolvedProperties)) { |
| 374 | + logger.debug("Node template [{}] has no unresolved properties by the [{}] and [{}] TOSCA functions.", nodeTemplate.getName(), ToscaConstants.GET_ATTRIBUTE_FUNCTION, ToscaConstants.GET_PROPERTY_FUNCTION); |
374 | 375 | return; |
375 | 376 | } |
376 | 377 |
|
377 | 378 | logger.info("Handling the [{}] and [{}] TOSCA function calls performed by the [{}] node template.", ToscaConstants.GET_ATTRIBUTE_FUNCTION, ToscaConstants.GET_PROPERTY_FUNCTION, nodeTemplate.getName()); |
378 | | - for (ToscaGetterFunctionCallContext functionCall : functionCalls) { |
379 | | - Object valueToBeResolved = resolveGetAttributeAndGetPropertyFunctionCall(functionCall.getFunctionCall(), serviceTemplate, nodeTemplate); |
380 | | - ToscaProperty callerProperty = functionCall.getProperty(); |
| 379 | + for (ToscaProperty property : unresolvedProperties) { |
| 380 | + Object evaluatedValue = getPropertyEvaluatedValue(property.getRawValue(), serviceTemplate, nodeTemplate); |
381 | 381 |
|
382 | | - if (callerProperty.getDefinition().getValidation() != null && callerProperty.getDefinition().getType().getKind() == ToscaTypeDefinition.Kind.PRIMITIVE) { |
383 | | - logger.debug("The property [{}] has a validation clause. Executing it.", callerProperty.getDefinition().getName()); |
384 | | - boolean validationResult = callerProperty.getDefinition().getValidation().evaluate(valueToBeResolved); |
| 382 | + if (property.getDefinition().getValidation() != null && property.getDefinition().getType().getKind() == ToscaTypeDefinition.Kind.PRIMITIVE) { |
| 383 | + logger.debug("The property [{}] has a validation clause. Executing it.", property.getDefinition().getName()); |
| 384 | + boolean validationResult = property.getDefinition().getValidation().evaluate(evaluatedValue); |
385 | 385 | if (!validationResult) { |
386 | | - logger.error("The value of the property [{}] of node template [{}] is not valid. Aborting IaC template deployment.", callerProperty.getDefinition().getName(), nodeTemplate.getName()); |
387 | | - throw new InvalidParameterValueException(String.format("The value of the property [%s] of node template [%s] is not valid. Please, check the value and try again.", callerProperty.getDefinition().getName(), nodeTemplate.getName())); |
| 386 | + logger.error("The value of the property [{}] of node template [{}] is not valid. Aborting IaC template deployment.", property.getDefinition().getName(), nodeTemplate.getName()); |
| 387 | + throw new InvalidParameterValueException(String.format("The value of the property [%s] of node template [%s] is not valid. Please, check the value and try again.", property.getDefinition().getName(), nodeTemplate.getName())); |
388 | 388 | } |
389 | 389 | } |
390 | 390 |
|
391 | | - logger.debug("The unresolved property [{}] of node template [{}] will be resolved to value [{}].", callerProperty.getDefinition().getName(), nodeTemplate.getName(), valueToBeResolved); |
392 | | - callerProperty.setEvaluatedValue(valueToBeResolved); |
| 391 | + logger.debug("Property [{}] of node template [{}] resolved to [{}].", property.getDefinition().getName(), nodeTemplate.getName(), evaluatedValue); |
| 392 | + property.setEvaluatedValue(evaluatedValue); |
393 | 393 | } |
394 | 394 | } |
395 | 395 |
|
| 396 | + private Object getPropertyEvaluatedValue(Object rawValue, ToscaServiceTemplate serviceTemplate, ToscaNodeTemplate nodeTemplate) { |
| 397 | + if (rawValue instanceof List) { |
| 398 | + List<Object> evaluatedValue = new ArrayList<>(); |
| 399 | + for (Object item : (List<?>) rawValue) { |
| 400 | + evaluatedValue.add(getPropertyEvaluatedValue(item, serviceTemplate, nodeTemplate)); |
| 401 | + } |
| 402 | + return evaluatedValue; |
| 403 | + } |
| 404 | + |
| 405 | + if (rawValue instanceof Map) { |
| 406 | + Map<String, Object> rawValueAsMap = ToscaYamlHelper.asMap(rawValue); |
| 407 | + boolean isToscaFunction = rawValueAsMap.size() == 1 && ToscaConstants.GETTER_FUNCTION_KEYS.contains(rawValueAsMap.keySet().iterator().next()); |
| 408 | + if (isToscaFunction) { |
| 409 | + return resolveGetAttributeAndGetPropertyFunctionCall(rawValueAsMap, serviceTemplate, nodeTemplate); |
| 410 | + } |
| 411 | + Map<String, Object> evaluatedValue = new LinkedHashMap<>(); |
| 412 | + for (Map.Entry<String, Object> entry : rawValueAsMap.entrySet()) { |
| 413 | + evaluatedValue.put(entry.getKey(), getPropertyEvaluatedValue(entry.getValue(), serviceTemplate, nodeTemplate)); |
| 414 | + } |
| 415 | + return evaluatedValue; |
| 416 | + } |
| 417 | + |
| 418 | + return rawValue; |
| 419 | + } |
| 420 | + |
396 | 421 | private Object resolveGetAttributeAndGetPropertyFunctionCall(Map<String, Object> functionCall, ToscaServiceTemplate serviceTemplate, ToscaNodeTemplate nodeTemplate) { |
397 | 422 | String toscaFunction = functionCall.keySet().iterator().next(); |
398 | 423 | List<?> args = ToscaYamlHelper.asList(functionCall.get(toscaFunction)); |
|
0 commit comments