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 com.google.common.base.CharMatcher;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.Optional;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openecomp.sdc.be.config.Configuration;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.util.CollectionUtils;
43 import org.yaml.snakeyaml.Yaml;
45 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
47 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
49 public ToscaFunctionJsonDeserializer() {
53 public ToscaFunctionJsonDeserializer(Class<?> vc) {
58 public ToscaFunction deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
59 final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
60 return deserializeToscaFunction(node, context);
63 private ToscaFunction deserializeToscaFunction(final JsonNode node, final DeserializationContext context) throws IOException {
64 final String functionType;
65 if (node.get("type") != null) {
66 functionType = node.get("type").asText();
67 } else if (node.get("functionType") != null) {
68 //support for legacy tosca function
69 functionType = node.get("functionType").asText();
71 throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
73 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType)
74 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
75 String.format("Invalid function type '%s' or attribute type not provided", functionType))
77 if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
78 || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
79 return deserializeToscaGetFunction(toscaFunctionType, node, context);
82 if (toscaFunctionType == ToscaFunctionType.CONCAT) {
83 return this.deserializeConcatFunction(node, context);
86 if (toscaFunctionType == ToscaFunctionType.YAML) {
87 return this.deserializeYamlFunction(node, context);
90 if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
91 return this.deserializeCustomFunction(node, context);
97 private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException {
98 var yamlFunction = new CustomYamlFunction();
99 final JsonNode valueJsonNode = node.get("value");
100 if (valueJsonNode == null) {
103 final String valueAsText = valueJsonNode.asText();
105 yamlFunction.setYamlValue(new Yaml().load(valueAsText));
106 } catch (final Exception e) {
107 final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText);
108 LOGGER.debug(errorMsg, e);
109 throw context.instantiationException(ToscaFunction.class, errorMsg);
114 private ToscaGetFunctionDataDefinition deserializeToscaGetFunction(final ToscaFunctionType toscaFunctionType, final JsonNode node,
115 final DeserializationContext context) throws JsonMappingException {
116 final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition();
117 toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType).orElse(null));
118 toscaGetFunction.setSourceName(getAsTextOrElseNull(node, "sourceName"));
119 toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
120 toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
121 toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
122 toscaGetFunction.setToscaIndexList(getNumberAsTextOrElseNull(node, "toscaIndexList", context));
124 final String propertySource = getAsTextOrElseNull(node, "propertySource");
125 if (StringUtils.isNotEmpty(propertySource)) {
126 final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
127 context.instantiationException(ToscaGetFunctionDataDefinition.class,
128 String.format("Invalid propertySource '%s'", propertySource))
130 toscaGetFunction.setPropertySource(propertySource1);
132 final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
133 if (propertyPathFromSourceNode != null) {
134 if (!propertyPathFromSourceNode.isArray()) {
135 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
137 final List<String> pathFromSource = new ArrayList<>();
138 propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
139 toscaGetFunction.setPropertyPathFromSource(pathFromSource);
142 return toscaGetFunction;
145 private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
146 final JsonNode jsonNode = node.get(fieldName);
147 if (jsonNode == null) {
150 if (!jsonNode.isTextual()) {
153 return jsonNode.asText();
156 private List<Object> getNumberAsTextOrElseNull(final JsonNode node, final String fieldName, final DeserializationContext context)
157 throws JsonMappingException {
158 List<Object> toscaIndexList = new ArrayList<Object>();
159 final JsonNode jsonNode = node.get(fieldName);
160 if (jsonNode != null) {
161 if (!jsonNode.isArray()) {
162 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for toscaIndexList attribute");
165 jsonNode.forEach(nodeValue -> {
166 String indexValue = nodeValue.asText();
167 if (StringUtils.isNumeric(indexValue)) {
168 toscaIndexList.add(Integer.parseInt(indexValue));
170 toscaIndexList.add(indexValue);
174 return toscaIndexList;
177 private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
178 final DeserializationContext context) throws IOException {
179 final var toscaConcatFunction = new ToscaConcatFunction();
180 List<ToscaFunctionParameter> functionParameterList = getParameters(concatFunctionJsonNode, context);
181 toscaConcatFunction.setParameters(functionParameterList);
182 return toscaConcatFunction;
185 private ToscaCustomFunction deserializeCustomFunction(final JsonNode customFunctionJsonNode,
186 final DeserializationContext context) throws IOException {
187 final var toscaCustomFunction = new ToscaCustomFunction();
188 final String name = getAsTextOrElseNull(customFunctionJsonNode, "name");
190 throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
192 toscaCustomFunction.setName(name);
193 toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(name));
194 List<ToscaFunctionParameter> functionParameterList = getParameters(customFunctionJsonNode, context);
195 toscaCustomFunction.setParameters(functionParameterList);
196 if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) {
197 validateGetInput(toscaCustomFunction, context);
199 return toscaCustomFunction;
202 private ToscaFunctionType getCustomFunctionType(String name) {
203 List<Configuration.CustomToscaFunction> customFunctions =
204 ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
205 if (CollectionUtils.isEmpty(customFunctions)) {
206 return ToscaFunctionType.CUSTOM;
208 Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
209 if (optionalFunc.isEmpty()) {
210 return ToscaFunctionType.CUSTOM;
212 String type = optionalFunc.get().getType();
213 return ToscaFunctionType.findType(type).get();
216 private void validateGetInput(ToscaCustomFunction toscaCustomFunction, final DeserializationContext context) throws IOException {
217 List<ToscaFunctionParameter> functionParameterList = toscaCustomFunction.getParameters();
218 if (functionParameterList.size() != 1) {
219 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain one GET_INPUT parameter");
221 ToscaFunctionParameter parameter = functionParameterList.get(0);
222 if (!ToscaFunctionType.GET_INPUT.equals(parameter.getType())) {
223 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain a GET_INPUT parameter");
227 private List<ToscaFunctionParameter> getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException {
228 final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
229 final JsonNode parametersNode = functionJsonNode.get("parameters");
230 if (parametersNode == null) {
231 return functionParameterList;
233 if (!parametersNode.isArray()) {
234 throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
237 for (final JsonNode parameterNode : parametersNode) {
238 final JsonNode typeJsonNode = parameterNode.get("type");
239 if (typeJsonNode == null) {
240 throw context.instantiationException(ToscaFunction.class, "TOSCA function parameter type attribute not provided");
242 final String parameterType = typeJsonNode.asText();
243 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
244 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
245 String.format("Invalid TOSCA function parameter type '%s'", parameterType))
247 if (toscaFunctionType == ToscaFunctionType.STRING) {
248 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
249 toscaStringParameter.setValue(CharMatcher.anyOf("\"\'").trimFrom(parameterNode.get("value").asText()));
250 functionParameterList.add(toscaStringParameter);
252 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
253 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
256 return functionParameterList;