diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-07-18 17:25:41 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-07-29 20:08:14 +0000 |
commit | 3f9669fdae5f7c6cb1bfe34742df35dfe3a14aa7 (patch) | |
tree | 307686e72403ee837fd342efb9865b0b6a83da7c /common-be/src/main | |
parent | 22d13840722a4020b1aaf75eeff3622e83c4b6d4 (diff) |
Support a custom yaml value in tosca function
Allows to add a custom YAML value to properties in the TOSCA function
feature.
Change-Id: I15e65088a18537d9832428717be826ac0ef6049a
Issue-ID: SDC-4099
Signed-off-by: André Schmid <andre.schmid@est.tech>
Diffstat (limited to 'common-be/src/main')
2 files changed, 22 insertions, 5 deletions
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CustomYamlFunction.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CustomYamlFunction.java index 84543e29f7..e9d584486d 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CustomYamlFunction.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/CustomYamlFunction.java @@ -36,7 +36,10 @@ public class CustomYamlFunction implements ToscaFunction, ToscaFunctionParameter @Override public String getValue() { - return new Yaml().dump(yamlValue); + if (yamlValue == null) { + return null; + } + return yamlValue instanceof String ? (String) yamlValue : new Yaml().dump(yamlValue); } @Override diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java index 74aed12471..fda832b98e 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializer.java @@ -32,10 +32,14 @@ import java.util.List; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.datatypes.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.Yaml; public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> { + private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class); + public ToscaFunctionJsonDeserializer() { this(null); } @@ -74,16 +78,26 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction } if (toscaFunctionType == ToscaFunctionType.YAML) { - return this.deserializeYamlFunction(node); + return this.deserializeYamlFunction(node, context); } return null; } - private ToscaFunction deserializeYamlFunction(JsonNode node) { + private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException { var yamlFunction = new CustomYamlFunction(); - final Object value = new Yaml().load(node.get("value").asText()); - yamlFunction.setYamlValue(value); + final JsonNode valueJsonNode = node.get("value"); + if (valueJsonNode == null) { + return yamlFunction; + } + final String valueAsText = valueJsonNode.asText(); + try { + yamlFunction.setYamlValue(new Yaml().load(valueAsText)); + } catch (final Exception e) { + final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText); + LOGGER.debug(errorMsg, e); + throw context.instantiationException(ToscaFunction.class, errorMsg); + } return yamlFunction; } |