diff options
Diffstat (limited to 'common-be')
4 files changed, 118 insertions, 5 deletions
diff --git a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java index 3f0c4fa8a5..981d4ecf8e 100644 --- a/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java @@ -22,19 +22,20 @@ package org.openecomp.sdc.be.datatypes.elements; import com.google.gson.Gson; -import lombok.Getter; -import lombok.Setter; - import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import lombok.Getter; +import lombok.Setter; @Getter @Setter public class ToscaCustomFunction implements ToscaFunction, ToscaFunctionParameter { private String name; + private ToscaFunctionType toscaFunctionType; private List<ToscaFunctionParameter> parameters = new ArrayList<>(); @Override @@ -49,9 +50,18 @@ public class ToscaCustomFunction implements ToscaFunction, ToscaFunctionParamete @Override public Object getJsonObjectValue() { + if (ToscaFunctionType.GET_INPUT.equals(this.toscaFunctionType)) { + Map<String, Object> getInput = parameters.stream().collect(Collectors.toMap( + input -> "$" + name, + input -> { + Map<String, Object> inputMap = (Map<String, Object>) input.getJsonObjectValue(); + return inputMap.get(ToscaFunctionType.GET_INPUT.getName()); + })); + return getInput; + } return Map.of( - "$" + name, - parameters.stream().map(ToscaFunctionParameter::getJsonObjectValue).collect(Collectors.toList()) + "$" + name, + parameters.stream().map(ToscaFunctionParameter::getJsonObjectValue).collect(Collectors.toList()) ); } 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 2e57a4158a..de8e30b897 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 @@ -29,7 +29,10 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; 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.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; import org.slf4j.Logger; @@ -178,11 +181,40 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction throw context.instantiationException(List.class, "Expecting a string for the 'name' entry"); } toscaCustomFunction.setName(name); + toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(name)); List<ToscaFunctionParameter> functionParameterList = getParameters(customFunctionJsonNode, context); toscaCustomFunction.setParameters(functionParameterList); + if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) { + validateGetInput(toscaCustomFunction, context); + } return 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(); + } + + private void validateGetInput(ToscaCustomFunction toscaCustomFunction, final DeserializationContext context) throws IOException { + List<ToscaFunctionParameter> functionParameterList = toscaCustomFunction.getParameters(); + if (functionParameterList.size() != 1) { + throw context.instantiationException(List.class, "Custom GET_INPUT function must contain one GET_INPUT parameter"); + } + ToscaFunctionParameter parameter = functionParameterList.get(0); + if (!ToscaFunctionType.GET_INPUT.equals(parameter.getType())) { + throw context.instantiationException(List.class, "Custom GET_INPUT function must contain a GET_INPUT parameter"); + } + } + private List<ToscaFunctionParameter> getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException { final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>(); final JsonNode parametersNode = functionJsonNode.get("parameters"); diff --git a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java index d7c2e7c65e..5128c63668 100644 --- a/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java +++ b/common-be/src/test/java/org/openecomp/sdc/be/datatypes/elements/ToscaFunctionJsonDeserializerTest.java @@ -33,9 +33,12 @@ import com.fasterxml.jackson.databind.exc.ValueInstantiationException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; import java.util.Map; 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.enums.PropertySource; import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType; import org.yaml.snakeyaml.Yaml; @@ -142,6 +145,7 @@ class ToscaFunctionJsonDeserializerTest { @Test void testCustomToscaFunction() throws IOException { //given + setDefaultCustomToscaFunctionOnConfiguration(); final String toscaCustomFunction = Files.readString(TEST_RESOURCES_PATH.resolve("customFunction.json")); //when final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunction); @@ -189,6 +193,55 @@ class ToscaFunctionJsonDeserializerTest { assertEquals("string3", customParameters.get(1)); } + @Test + void testCustomToscaFunctionGetInputType() throws IOException { + //given + setDefaultCustomToscaFunctionOnConfiguration(); + final String toscaCustomFunctionFile = Files.readString(TEST_RESOURCES_PATH.resolve("customFunctionGetInputType.json")); + //when + final ToscaFunction toscaFunction = parseToscaFunction(toscaCustomFunctionFile); + //then + assertTrue(toscaFunction instanceof ToscaCustomFunction); + ToscaCustomFunction toscaCustomFunction = (ToscaCustomFunction) toscaFunction; + final Object yamlObject = new Yaml().load(toscaFunction.getValue()); + assertTrue(yamlObject instanceof Map); + final Map<String, Object> yamlMap = (Map<String, Object>) yamlObject; + assertEquals(1, yamlMap.size()); + final Object customFunctionGetInputValue = yamlMap.get("$" + ((ToscaCustomFunction) toscaFunction).getName()); + assertTrue(customFunctionGetInputValue instanceof ArrayList); + ArrayList<Object> customFunctionGetInputValueList = (ArrayList<Object>) customFunctionGetInputValue; + assertEquals(1, customFunctionGetInputValueList.size()); + assertEquals(1, customFunctionGetInputValueList.size()); + assertEquals("controller_actor", customFunctionGetInputValueList.get(0)); + + List<ToscaFunctionParameter> parameters = toscaCustomFunction.getParameters(); + assertEquals(1, parameters.size()); + ToscaFunctionParameter paramFunction = toscaCustomFunction.getParameters().get(0); + assertTrue(paramFunction instanceof ToscaGetFunctionDataDefinition); + + final ToscaGetFunctionDataDefinition toscaGetFunction = (ToscaGetFunctionDataDefinition) paramFunction; + assertEquals(ToscaFunctionType.GET_INPUT, toscaGetFunction.getType()); + assertEquals(ToscaGetFunctionType.GET_INPUT, toscaGetFunction.getFunctionType()); + assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63.controller_actor", toscaGetFunction.getPropertyUniqueId()); + assertEquals(PropertySource.SELF, toscaGetFunction.getPropertySource()); + assertEquals("controller_actor", toscaGetFunction.getPropertyName()); + assertEquals("testService", toscaGetFunction.getSourceName()); + assertEquals("dd0ec4d2-7e74-4d92-af2f-89c7436baa63", toscaGetFunction.getSourceUniqueId()); + assertEquals(List.of("controller_actor"), toscaGetFunction.getPropertyPathFromSource()); + } + + 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); + } + private ToscaFunction parseToscaFunction(final String toscaFunctionJson) throws JsonProcessingException { return new ObjectMapper().readValue(toscaFunctionJson, ToscaFunction.class); } diff --git a/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunctionGetInputType.json b/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunctionGetInputType.json new file mode 100644 index 0000000000..81ae571c01 --- /dev/null +++ b/common-be/src/test/resources/toscaFunctionJsonDeserializer/customFunctionGetInputType.json @@ -0,0 +1,18 @@ +{ + "type": "CUSTOM", + "name": "custom_function_get_input_type", + "parameters": [ + { + "propertyUniqueId": "dd0ec4d2-7e74-4d92-af2f-89c7436baa63.controller_actor", + "propertyName": "controller_actor", + "propertySource": "SELF", + "sourceUniqueId": "dd0ec4d2-7e74-4d92-af2f-89c7436baa63", + "sourceName": "testService", + "functionType": "GET_INPUT", + "propertyPathFromSource": [ + "controller_actor" + ], + "type": "GET_INPUT" + } + ] +}
\ No newline at end of file |