diff options
author | JvD_Ericsson <jeff.van.dam@est.tech> | 2023-05-04 13:27:26 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2023-05-18 10:41:07 +0000 |
commit | bc7dd3ad94acace55a2910abc22cc5cb64e0862d (patch) | |
tree | 43948e332fa27ea7cecc70ba19388bb63e1069d0 /catalog-be/src | |
parent | 38eaf2ddd678a837e2bfed25d5b4b45d72fce338 (diff) |
UI support for default custom function names with get_input structure
Issue-ID: SDC-4493
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Change-Id: Iba3eda9bb5d57aabbe86045b6150564e17a0ff3e
Diffstat (limited to 'catalog-be/src')
2 files changed, 84 insertions, 2 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java index 0b9432b3a1..0651e3504e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandler.java @@ -21,6 +21,7 @@ package org.openecomp.sdc.be.components.csar; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -28,6 +29,8 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction; @@ -74,7 +77,7 @@ public class ToscaFunctionYamlParsingHandler { List<String> propertySourceIndex = functionParameters.subList(1, functionParameters.size()); String toscaIndexValue = propertySourceIndex.get((propertySourceIndex.size() - 1)); if (propertySourceIndex.size() > 1 && (toscaIndexValue.equalsIgnoreCase("INDEX") || StringUtils.isNumeric(toscaIndexValue))) { - toscaGetFunction.setPropertyPathFromSource(propertySourceIndex.subList(0,(propertySourceIndex.size() - 1))); + toscaGetFunction.setPropertyPathFromSource(propertySourceIndex.subList(0, (propertySourceIndex.size() - 1))); toscaGetFunction.setToscaIndex(toscaIndexValue); } else { toscaGetFunction.setPropertyPathFromSource(propertySourceIndex); @@ -105,7 +108,7 @@ public class ToscaFunctionYamlParsingHandler { } String toscaIndexValue = functionParameters.get((functionParameters.size() - 1)); if (functionParameters.size() > 1 && (toscaIndexValue.equalsIgnoreCase("INDEX") || StringUtils.isNumeric(toscaIndexValue))) { - toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(0,(functionParameters.size() - 1))); + toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(0, (functionParameters.size() - 1))); toscaGetFunction.setToscaIndex(toscaIndexValue); } else { toscaGetFunction.setPropertyPathFromSource(functionParameters); @@ -155,6 +158,14 @@ public class ToscaFunctionYamlParsingHandler { final ToscaCustomFunction toscaCustomFunction = new ToscaCustomFunction(); toscaCustomFunction.setName(functionType.substring(1)); final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); + toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(toscaCustomFunction.getName())); + if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) { + return handelCustomFunctionGetInputType(toscaCustomFunction, functionValueObj); + } + return handelCustomFunctionCustomType(toscaCustomFunction, functionValueObj); + } + + private Optional<ToscaFunction> handelCustomFunctionCustomType(ToscaCustomFunction toscaCustomFunction, Object functionValueObj) { if (!(functionValueObj instanceof List)) { return Optional.empty(); } @@ -181,6 +192,35 @@ public class ToscaFunctionYamlParsingHandler { return Optional.of(toscaCustomFunction); } + private Optional<ToscaFunction> handelCustomFunctionGetInputType(ToscaCustomFunction toscaCustomFunction, Object functionValueObj) { + if (!(functionValueObj instanceof String)) { + return Optional.empty(); + } + final String parameter = (String) functionValueObj; + Map<String, Object> parameterMap = new HashMap<>(); + parameterMap.put(ToscaFunctionType.GET_INPUT.getName(), parameter); + buildToscaFunctionBasedOnPropertyValue(parameterMap).ifPresent(toscaFunction -> { + if (toscaFunction instanceof ToscaFunctionParameter) { + toscaCustomFunction.addParameter((ToscaFunctionParameter) toscaFunction); + } + }); + return Optional.of(toscaCustomFunction); + } + + private ToscaFunctionType getCustomFunctionType(String name) { + List<Configuration.CustomToscaFunction> customFunctions = + ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions(); + if (customFunctions.isEmpty()) { + return ToscaFunctionType.CUSTOM; + } + Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst(); + if (optionalFunc.isEmpty()) { + return ToscaFunctionType.CUSTOM; + } + String type = optionalFunc.get().getType(); + return ToscaFunctionType.findType(type).get(); + } + /** * Checks if the property value is a supported TOSCA function. * diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandlerTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandlerTest.java index 1dfb7a9113..4791015d76 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandlerTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/ToscaFunctionYamlParsingHandlerTest.java @@ -26,11 +26,14 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import org.junit.jupiter.api.Test; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; @@ -134,6 +137,7 @@ class ToscaFunctionYamlParsingHandlerTest { @Test void buildToscaFunctionBasedOnPropertyValue_CustomTest() { + setDefaultCustomToscaFunctionOnConfiguration(); final List<Object> customValue = List.of("string1", "-", Map.of(ToscaFunctionType.GET_INPUT.getName(), "inputName")); final Map<String, Object> customValueMap = Map.of("$customFuncName", customValue); @@ -158,6 +162,32 @@ class ToscaFunctionYamlParsingHandlerTest { } @Test + void buildToscaFunctionBasedOnPropertyValue_CustomFunctionGetInputTypeTest() { + setDefaultCustomToscaFunctionOnConfiguration(); + final Map<String, Object> customValueMap = Map.of("$custom_function_get_input_type", "controller_actor"); + + final Optional<ToscaFunction> actualToscaFunctionOpt = toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(customValueMap); + assertTrue(actualToscaFunctionOpt.isPresent()); + final ToscaFunction actualToscaFunction = actualToscaFunctionOpt.get(); + assertEquals(ToscaFunctionType.CUSTOM, actualToscaFunction.getType()); + assertTrue(actualToscaFunction instanceof ToscaCustomFunction); + final ToscaCustomFunction toscaCustomFunction = (ToscaCustomFunction) actualToscaFunction; + final String functionName = toscaCustomFunction.getName(); + assertEquals("custom_function_get_input_type", functionName); + assertEquals(1, toscaCustomFunction.getParameters().size()); + assertTrue(toscaCustomFunction.getParameters().get(0) instanceof ToscaGetFunctionDataDefinition); + final ToscaGetFunctionDataDefinition parameter1 = (ToscaGetFunctionDataDefinition) toscaCustomFunction.getParameters().get(0); + assertGetInput(parameter1, List.of("controller_actor")); +// assertEquals("string1", parameter1.getValue()); +// assertTrue(toscaCustomFunction.getParameters().get(1) instanceof ToscaStringParameter); +// final ToscaStringParameter parameter2 = (ToscaStringParameter) toscaCustomFunction.getParameters().get(1); +// assertEquals("-", parameter2.getValue()); +// assertTrue(toscaCustomFunction.getParameters().get(2) instanceof ToscaGetFunctionDataDefinition); +// final ToscaGetFunctionDataDefinition getFunction = (ToscaGetFunctionDataDefinition) toscaCustomFunction.getParameters().get(2); +// assertGetInput(getFunction, List.of("inputName")); + } + + @Test void isPropertyValueToscaFunctionTest() { assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(ToscaFunctionType.GET_INPUT.getName())); assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(new HashMap<>())); @@ -172,4 +202,16 @@ class ToscaFunctionYamlParsingHandlerTest { assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.YAML.getName(), ""))); assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.STRING.getName(), ""))); } + + private void setDefaultCustomToscaFunctionOnConfiguration() { + final var configurationManager = new ConfigurationManager(); + final var configuration = new Configuration(); + List<Configuration.CustomToscaFunction> defaultCustomToscaFunctions = new ArrayList<>(); + Configuration.CustomToscaFunction defaultCustomType = new Configuration.CustomToscaFunction(); + defaultCustomType.setName("custom_function_get_input_type"); + defaultCustomType.setType("get_input"); + defaultCustomToscaFunctions.add(defaultCustomType); + configuration.setDefaultCustomToscaFunctions(defaultCustomToscaFunctions); + configurationManager.setConfiguration(configuration); + } }
\ No newline at end of file |