diff options
author | JvD_Ericsson <jeff.van.dam@est.tech> | 2023-04-21 14:41:52 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2023-05-04 08:22:10 +0000 |
commit | c4de5390c2a396e9ea88061454e40a92cea57ce1 (patch) | |
tree | 59e5a8db5b1b754f8a1ff5c614f1e523428ea219 /catalog-be | |
parent | 7c6e28c7023753678f7048aeecf8122eebcf92a9 (diff) |
Support service import with custom tosca functions
Issue-ID: SDC-4479
Change-Id: I1a98c8a191fc255cb3b2b328b0ea1e72977252d2
Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech>
Diffstat (limited to 'catalog-be')
2 files changed, 133 insertions, 70 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 6bc74a69df..2a7af62a0c 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 @@ -27,6 +27,7 @@ import java.util.Optional; import java.util.stream.Stream; import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionParameter; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; @@ -38,6 +39,63 @@ import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; @org.springframework.stereotype.Component public class ToscaFunctionYamlParsingHandler { + private static Optional<ToscaFunction> handleGetPropertyFunction(Map<String, Object> toscaFunctionPropertyValueMap, String functionType, + ToscaFunctionType toscaFunctionType) { + final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); + toscaGetFunction.setFunctionType( + toscaFunctionType == ToscaFunctionType.GET_PROPERTY ? ToscaGetFunctionType.GET_PROPERTY : ToscaGetFunctionType.GET_ATTRIBUTE + ); + final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); + if (!(functionValueObj instanceof List)) { + return Optional.empty(); + } + final List<String> functionParameters; + try { + functionParameters = (List<String>) functionValueObj; + } catch (final ClassCastException ignored) { + return Optional.empty(); + } + if (functionParameters.size() < 2) { + return Optional.empty(); + } + final String propertySourceType = functionParameters.get(0); + final PropertySource propertySource = PropertySource.findType(propertySourceType).orElse(null); + if (propertySource == PropertySource.SELF) { + toscaGetFunction.setPropertySource(propertySource); + } else { + toscaGetFunction.setPropertySource(PropertySource.INSTANCE); + toscaGetFunction.setSourceName(propertySourceType); + } + toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(1, functionParameters.size())); + final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); + toscaGetFunction.setPropertyName(propertyName); + return Optional.of(toscaGetFunction); + } + + private static Optional<ToscaFunction> handleGetInputFunction(Map<String, Object> toscaFunctionPropertyValueMap, String functionType) { + final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); + toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_INPUT); + toscaGetFunction.setPropertySource(PropertySource.SELF); + final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); + if (!(functionValueObj instanceof List) && !(functionValueObj instanceof String)) { + return Optional.empty(); + } + if (functionValueObj instanceof String) { + toscaGetFunction.setPropertyPathFromSource(List.of((String) functionValueObj)); + } else { + final List<String> functionParameters; + try { + functionParameters = (List<String>) functionValueObj; + } catch (final ClassCastException ignored) { + return Optional.empty(); + } + toscaGetFunction.setPropertyPathFromSource(functionParameters); + } + final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); + toscaGetFunction.setPropertyName(propertyName); + return Optional.of(toscaGetFunction); + } + /** * Builds a {@link ToscaFunction} based on the property value. It will build the object with the maximum information available in the property * value, as not all the necessary information can be extracted from it. It will only parse values from supported functions in @@ -51,7 +109,8 @@ public class ToscaFunctionYamlParsingHandler { return Optional.empty(); } final String functionType = toscaFunctionPropertyValueMap.keySet().iterator().next(); - final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType).orElse(null); + final ToscaFunctionType toscaFunctionType = + ToscaFunctionType.findType(functionType).orElse(functionType.startsWith("$") ? ToscaFunctionType.CUSTOM : null); if (toscaFunctionType == null) { return Optional.empty(); } @@ -65,11 +124,43 @@ public class ToscaFunctionYamlParsingHandler { } case CONCAT: return handleConcatFunction(toscaFunctionPropertyValueMap, functionType); + case CUSTOM: + return handleCustomFunction(toscaFunctionPropertyValueMap, functionType); default: return Optional.empty(); } } + private Optional<ToscaFunction> handleCustomFunction(Map<String, Object> toscaFunctionPropertyValueMap, String functionType) { + final ToscaCustomFunction toscaCustomFunction = new ToscaCustomFunction(); + toscaCustomFunction.setName(functionType.substring(1)); + final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); + if (!(functionValueObj instanceof List)) { + return Optional.empty(); + } + final List<Object> functionParameters = (List<Object>) functionValueObj; + functionParameters.forEach(parameter -> { + if (parameter instanceof String) { + final var stringParameter = new ToscaStringParameter(); + stringParameter.setValue((String) parameter); + toscaCustomFunction.addParameter(stringParameter); + return; + } + if (isPropertyValueToscaFunction(parameter)) { + buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) parameter).ifPresent(toscaFunction -> { + if (toscaFunction instanceof ToscaFunctionParameter) { + toscaCustomFunction.addParameter((ToscaFunctionParameter) toscaFunction); + } + }); + return; + } + final var customYamlFunction = new CustomYamlFunction(); + customYamlFunction.setYamlValue(parameter); + toscaCustomFunction.addParameter(customYamlFunction); + }); + return Optional.of(toscaCustomFunction); + } + /** * Checks if the property value is a supported TOSCA function. * @@ -82,6 +173,10 @@ public class ToscaFunctionYamlParsingHandler { if (propValueMap.keySet().size() > 1) { return false; } + if (propValueMap.keySet().stream().anyMatch(keyValue -> keyValue.startsWith("$"))) { + return true; + } + return Stream.of(ToscaFunctionType.GET_INPUT, ToscaFunctionType.GET_PROPERTY, ToscaFunctionType.GET_ATTRIBUTE, ToscaFunctionType.CONCAT) .anyMatch(type -> propValueMap.containsKey(type.getName())); } @@ -120,61 +215,4 @@ public class ToscaFunctionYamlParsingHandler { return Optional.of(toscaConcatFunction); } - private static Optional<ToscaFunction> handleGetPropertyFunction(Map<String, Object> toscaFunctionPropertyValueMap, String functionType, - ToscaFunctionType toscaFunctionType) { - final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); - toscaGetFunction.setFunctionType( - toscaFunctionType == ToscaFunctionType.GET_PROPERTY ? ToscaGetFunctionType.GET_PROPERTY : ToscaGetFunctionType.GET_ATTRIBUTE - ); - final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); - if (!(functionValueObj instanceof List)) { - return Optional.empty(); - } - final List<String> functionParameters; - try { - functionParameters = (List<String>) functionValueObj; - } catch (final ClassCastException ignored) { - return Optional.empty(); - } - if (functionParameters.size() < 2) { - return Optional.empty(); - } - final String propertySourceType = functionParameters.get(0); - final PropertySource propertySource = PropertySource.findType(propertySourceType).orElse(null); - if (propertySource == PropertySource.SELF) { - toscaGetFunction.setPropertySource(propertySource); - } else { - toscaGetFunction.setPropertySource(PropertySource.INSTANCE); - toscaGetFunction.setSourceName(propertySourceType); - } - toscaGetFunction.setPropertyPathFromSource(functionParameters.subList(1, functionParameters.size())); - final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); - toscaGetFunction.setPropertyName(propertyName); - return Optional.of(toscaGetFunction); - } - - private static Optional<ToscaFunction> handleGetInputFunction(Map<String, Object> toscaFunctionPropertyValueMap, String functionType) { - final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition(); - toscaGetFunction.setFunctionType(ToscaGetFunctionType.GET_INPUT); - toscaGetFunction.setPropertySource(PropertySource.SELF); - final Object functionValueObj = toscaFunctionPropertyValueMap.get(functionType); - if (!(functionValueObj instanceof List) && !(functionValueObj instanceof String)) { - return Optional.empty(); - } - if (functionValueObj instanceof String) { - toscaGetFunction.setPropertyPathFromSource(List.of((String) functionValueObj)); - } else { - final List<String> functionParameters; - try { - functionParameters = (List<String>) functionValueObj; - } catch (final ClassCastException ignored) { - return Optional.empty(); - } - toscaGetFunction.setPropertyPathFromSource(functionParameters); - } - final String propertyName = toscaGetFunction.getPropertyPathFromSource().get(toscaGetFunction.getPropertyPathFromSource().size() - 1); - toscaGetFunction.setPropertyName(propertyName); - return Optional.of(toscaGetFunction); - } - } 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 5b0096bb3f..1dfb7a9113 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 @@ -32,6 +32,7 @@ import java.util.Map; import java.util.Optional; import org.junit.jupiter.api.Test; import org.openecomp.sdc.be.datatypes.elements.ToscaConcatFunction; +import org.openecomp.sdc.be.datatypes.elements.ToscaCustomFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunction; import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType; import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition; @@ -43,6 +44,18 @@ class ToscaFunctionYamlParsingHandlerTest { final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler = new ToscaFunctionYamlParsingHandler(); + private static void assertGetInput(final ToscaFunction actualGetInputFunction, final List<String> expectedGetInputParameters) { + assertEquals(ToscaFunctionType.GET_INPUT, actualGetInputFunction.getType()); + assertTrue(actualGetInputFunction instanceof ToscaGetFunctionDataDefinition); + final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualGetInputFunction; + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType()); + assertEquals(expectedGetInputParameters.get(expectedGetInputParameters.size() - 1), toscaGetFunction.getPropertyName()); + assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource()); + assertEquals(expectedGetInputParameters, toscaGetFunction.getPropertyPathFromSource()); + assertNull(toscaGetFunction.getPropertyUniqueId()); + assertNull(toscaGetFunction.getSourceName()); + } + @Test void buildToscaFunctionBasedOnPropertyValue_NotAToscaFunctionTest() { assertEquals(Optional.empty(), toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(null)); @@ -119,6 +132,30 @@ class ToscaFunctionYamlParsingHandlerTest { assertGetInput(getFunction, List.of("inputName")); } + @Test + void buildToscaFunctionBasedOnPropertyValue_CustomTest() { + final List<Object> customValue = List.of("string1", "-", Map.of(ToscaFunctionType.GET_INPUT.getName(), "inputName")); + final Map<String, Object> customValueMap = Map.of("$customFuncName", customValue); + + 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("customFuncName", functionName); + assertEquals(3, toscaCustomFunction.getParameters().size()); + assertTrue(toscaCustomFunction.getParameters().get(0) instanceof ToscaStringParameter); + final ToscaStringParameter parameter1 = (ToscaStringParameter) toscaCustomFunction.getParameters().get(0); + 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() { @@ -135,16 +172,4 @@ class ToscaFunctionYamlParsingHandlerTest { assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.YAML.getName(), ""))); assertFalse(toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(Map.of(ToscaFunctionType.STRING.getName(), ""))); } - - private static void assertGetInput(final ToscaFunction actualGetInputFunction, final List<String> expectedGetInputParameters) { - assertEquals(ToscaFunctionType.GET_INPUT, actualGetInputFunction.getType()); - assertTrue(actualGetInputFunction instanceof ToscaGetFunctionDataDefinition); - final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) actualGetInputFunction; - assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType()); - assertEquals(expectedGetInputParameters.get(expectedGetInputParameters.size() - 1), toscaGetFunction.getPropertyName()); - assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource()); - assertEquals(expectedGetInputParameters, toscaGetFunction.getPropertyPathFromSource()); - assertNull(toscaGetFunction.getPropertyUniqueId()); - assertNull(toscaGetFunction.getSourceName()); - } }
\ No newline at end of file |