3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.datatypes.elements;
24 import com.fasterxml.jackson.core.JsonParser;
25 import com.fasterxml.jackson.databind.DeserializationContext;
26 import com.fasterxml.jackson.databind.JsonMappingException;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Optional;
33 import org.apache.commons.lang3.StringUtils;
34 import org.openecomp.sdc.be.config.Configuration;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
37 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.util.CollectionUtils;
41 import org.yaml.snakeyaml.Yaml;
43 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
45 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
47 public ToscaFunctionJsonDeserializer() {
51 public ToscaFunctionJsonDeserializer(Class<?> vc) {
56 public ToscaFunction deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
57 final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
58 return deserializeToscaFunction(node, context);
61 private ToscaFunction deserializeToscaFunction(final JsonNode node, final DeserializationContext context) throws IOException {
62 final String functionType;
63 if (node.get("type") != null) {
64 functionType = node.get("type").asText();
65 } else if (node.get("functionType") != null) {
66 //support for legacy tosca function
67 functionType = node.get("functionType").asText();
69 throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
71 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType)
72 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
73 String.format("Invalid function type '%s' or attribute type not provided", functionType))
75 if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
76 || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
77 return deserializeToscaGetFunction(toscaFunctionType, node, context);
80 if (toscaFunctionType == ToscaFunctionType.CONCAT) {
81 return this.deserializeConcatFunction(node, context);
84 if (toscaFunctionType == ToscaFunctionType.YAML) {
85 return this.deserializeYamlFunction(node, context);
88 if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
89 return this.deserializeCustomFunction(node, context);
95 private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException {
96 var yamlFunction = new CustomYamlFunction();
97 final JsonNode valueJsonNode = node.get("value");
98 if (valueJsonNode == null) {
101 final String valueAsText = valueJsonNode.asText();
103 yamlFunction.setYamlValue(new Yaml().load(valueAsText));
104 } catch (final Exception e) {
105 final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText);
106 LOGGER.debug(errorMsg, e);
107 throw context.instantiationException(ToscaFunction.class, errorMsg);
112 private ToscaGetFunctionDataDefinition deserializeToscaGetFunction(final ToscaFunctionType toscaFunctionType, final JsonNode node,
113 final DeserializationContext context) throws JsonMappingException {
114 final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition();
115 toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType).orElse(null));
116 toscaGetFunction.setSourceName(getAsTextOrElseNull(node, "sourceName"));
117 toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
118 toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
119 toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
120 toscaGetFunction.setToscaIndexList(getNumberAsTextOrElseNull(node, "toscaIndexList", context));
122 final String propertySource = getAsTextOrElseNull(node, "propertySource");
123 if (StringUtils.isNotEmpty(propertySource)) {
124 final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
125 context.instantiationException(ToscaGetFunctionDataDefinition.class,
126 String.format("Invalid propertySource '%s'", propertySource))
128 toscaGetFunction.setPropertySource(propertySource1);
130 final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
131 if (propertyPathFromSourceNode != null) {
132 if (!propertyPathFromSourceNode.isArray()) {
133 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
135 final List<String> pathFromSource = new ArrayList<>();
136 propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
137 toscaGetFunction.setPropertyPathFromSource(pathFromSource);
140 return toscaGetFunction;
143 private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
144 final JsonNode jsonNode = node.get(fieldName);
145 if (jsonNode == null) {
148 if (!jsonNode.isTextual()) {
151 return jsonNode.asText();
154 private List<Object> getNumberAsTextOrElseNull(final JsonNode node, final String fieldName, final DeserializationContext context)
155 throws JsonMappingException {
156 List<Object> toscaIndexList = new ArrayList<Object>();
157 final JsonNode jsonNode = node.get(fieldName);
158 if (jsonNode != null) {
159 if (!jsonNode.isArray()) {
160 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for toscaIndexList attribute");
162 for (int index = 0; index < jsonNode.size(); index++) {
163 String textValue = jsonNode.get(index).asText();
164 if (index % 2 == 0) {
165 if (textValue.equalsIgnoreCase("INDEX")) {
166 toscaIndexList.add(textValue);
169 toscaIndexList.add(Integer.parseInt(textValue));
170 } catch (Exception e) {
171 throw context.instantiationException(ToscaGetFunctionDataDefinition.class,
172 "Expecting a valid value for toscaIndex attribute");
176 toscaIndexList.add(textValue);
180 return toscaIndexList;
183 private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
184 final DeserializationContext context) throws IOException {
185 final var toscaConcatFunction = new ToscaConcatFunction();
186 List<ToscaFunctionParameter> functionParameterList = getParameters(concatFunctionJsonNode, context);
187 toscaConcatFunction.setParameters(functionParameterList);
188 return toscaConcatFunction;
191 private ToscaCustomFunction deserializeCustomFunction(final JsonNode customFunctionJsonNode,
192 final DeserializationContext context) throws IOException {
193 final var toscaCustomFunction = new ToscaCustomFunction();
194 final String name = getAsTextOrElseNull(customFunctionJsonNode, "name");
196 throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
198 toscaCustomFunction.setName(name);
199 toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(name));
200 List<ToscaFunctionParameter> functionParameterList = getParameters(customFunctionJsonNode, context);
201 toscaCustomFunction.setParameters(functionParameterList);
202 if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) {
203 validateGetInput(toscaCustomFunction, context);
205 return toscaCustomFunction;
208 private ToscaFunctionType getCustomFunctionType(String name) {
209 List<Configuration.CustomToscaFunction> customFunctions =
210 ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
211 if (CollectionUtils.isEmpty(customFunctions)) {
212 return ToscaFunctionType.CUSTOM;
214 Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
215 if (optionalFunc.isEmpty()) {
216 return ToscaFunctionType.CUSTOM;
218 String type = optionalFunc.get().getType();
219 return ToscaFunctionType.findType(type).get();
222 private void validateGetInput(ToscaCustomFunction toscaCustomFunction, final DeserializationContext context) throws IOException {
223 List<ToscaFunctionParameter> functionParameterList = toscaCustomFunction.getParameters();
224 if (functionParameterList.size() != 1) {
225 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain one GET_INPUT parameter");
227 ToscaFunctionParameter parameter = functionParameterList.get(0);
228 if (!ToscaFunctionType.GET_INPUT.equals(parameter.getType())) {
229 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain a GET_INPUT parameter");
233 private List<ToscaFunctionParameter> getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException {
234 final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
235 final JsonNode parametersNode = functionJsonNode.get("parameters");
236 if (parametersNode == null) {
237 return functionParameterList;
239 if (!parametersNode.isArray()) {
240 throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
243 for (final JsonNode parameterNode : parametersNode) {
244 final JsonNode typeJsonNode = parameterNode.get("type");
245 if (typeJsonNode == null) {
246 throw context.instantiationException(ToscaFunction.class, "TOSCA function parameter type attribute not provided");
248 final String parameterType = typeJsonNode.asText();
249 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
250 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
251 String.format("Invalid TOSCA function parameter type '%s'", parameterType))
253 if (toscaFunctionType == ToscaFunctionType.STRING) {
254 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
255 toscaStringParameter.setValue(parameterNode.get("value").asText());
256 functionParameterList.add(toscaStringParameter);
258 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
259 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
262 return functionParameterList;