From 35ee855d664246d55694f06b61bde82277cb2c5c Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Wed, 29 Mar 2023 12:34:59 +0100 Subject: Backend support for custom functions Issue-ID: SDC-4455 Signed-off-by: JvD_Ericsson Change-Id: Idb0fd38681066ba9d541f8564c85e316cf03e927 --- .../be/datatypes/elements/ToscaCustomFunction.java | 62 ++++++++++++++++++++++ .../elements/ToscaFunctionJsonDeserializer.java | 37 ++++++++++--- .../be/datatypes/elements/ToscaFunctionType.java | 1 + 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java (limited to 'common-be/src/main') 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 new file mode 100644 index 0000000000..3f0c4fa8a5 --- /dev/null +++ b/common-be/src/main/java/org/openecomp/sdc/be/datatypes/elements/ToscaCustomFunction.java @@ -0,0 +1,62 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +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.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Getter +@Setter +public class ToscaCustomFunction implements ToscaFunction, ToscaFunctionParameter { + + private String name; + private List parameters = new ArrayList<>(); + + @Override + public ToscaFunctionType getType() { + return ToscaFunctionType.CUSTOM; + } + + @Override + public String getValue() { + return new Gson().toJson(getJsonObjectValue()); + } + + @Override + public Object getJsonObjectValue() { + return Map.of( + "$" + name, + parameters.stream().map(ToscaFunctionParameter::getJsonObjectValue).collect(Collectors.toList()) + ); + } + + public void addParameter(final ToscaFunctionParameter functionParameter) { + this.parameters.add(functionParameter); + } + +} 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 c262699cc2..ec3459989e 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 @@ -81,6 +81,10 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer functionParameterList = getParameters(concatFunctionJsonNode, context); + toscaConcatFunction.setParameters(functionParameterList); + return toscaConcatFunction; + } + + private ToscaCustomFunction deserializeCustomFunction(final JsonNode customFunctionJsonNode, + final DeserializationContext context) throws IOException { + final var toscaCustomFunction = new ToscaCustomFunction(); + final String name = getAsTextOrElseNull(customFunctionJsonNode, "name"); + if (name == null) { + throw context.instantiationException(List.class, "Expecting a string for the 'name' entry"); + } + toscaCustomFunction.setName(name); + List functionParameterList = getParameters(customFunctionJsonNode, context); + toscaCustomFunction.setParameters(functionParameterList); + return toscaCustomFunction; + } + + private List getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException { final List functionParameterList = new ArrayList<>(); - final JsonNode parametersNode = concatFunctionJsonNode.get("parameters"); + final JsonNode parametersNode = functionJsonNode.get("parameters"); if (parametersNode == null) { - return toscaConcatFunction; + return functionParameterList; } if (!parametersNode.isArray()) { throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry"); } + for (final JsonNode parameterNode : parametersNode) { final JsonNode typeJsonNode = parameterNode.get("type"); if (typeJsonNode == null) { - throw context.instantiationException(ToscaConcatFunction.class, "TOSCA concat function parameter type attribute not provided"); + throw context.instantiationException(ToscaFunction.class, "TOSCA function parameter type attribute not provided"); } final String parameterType = typeJsonNode.asText(); final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType) - .orElseThrow(() -> context.instantiationException(ToscaConcatFunction.class, - String.format("Invalid TOSCA concat function parameter type '%s'", parameterType)) + .orElseThrow(() -> context.instantiationException(ToscaFunction.class, + String.format("Invalid TOSCA function parameter type '%s'", parameterType)) ); if (toscaFunctionType == ToscaFunctionType.STRING) { final ToscaStringParameter toscaStringParameter = new ToscaStringParameter(); @@ -171,8 +195,7 @@ public class ToscaFunctionJsonDeserializer extends StdDeserializer