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.yaml.snakeyaml.Yaml;
42 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
44 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
46 public ToscaFunctionJsonDeserializer() {
50 public ToscaFunctionJsonDeserializer(Class<?> vc) {
55 public ToscaFunction deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
56 final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
57 return deserializeToscaFunction(node, context);
60 private ToscaFunction deserializeToscaFunction(final JsonNode node, final DeserializationContext context) throws IOException {
61 final String functionType;
62 if (node.get("type") != null) {
63 functionType = node.get("type").asText();
64 } else if (node.get("functionType") != null) {
65 //support for legacy tosca function
66 functionType = node.get("functionType").asText();
68 throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
70 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType)
71 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
72 String.format("Invalid function type '%s' or attribute type not provided", functionType))
74 if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
75 || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
76 return deserializeToscaGetFunction(toscaFunctionType, node, context);
79 if (toscaFunctionType == ToscaFunctionType.CONCAT) {
80 return this.deserializeConcatFunction(node, context);
83 if (toscaFunctionType == ToscaFunctionType.YAML) {
84 return this.deserializeYamlFunction(node, context);
87 if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
88 return this.deserializeCustomFunction(node, context);
94 private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException {
95 var yamlFunction = new CustomYamlFunction();
96 final JsonNode valueJsonNode = node.get("value");
97 if (valueJsonNode == null) {
100 final String valueAsText = valueJsonNode.asText();
102 yamlFunction.setYamlValue(new Yaml().load(valueAsText));
103 } catch (final Exception e) {
104 final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText);
105 LOGGER.debug(errorMsg, e);
106 throw context.instantiationException(ToscaFunction.class, errorMsg);
111 private ToscaGetFunctionDataDefinition deserializeToscaGetFunction(final ToscaFunctionType toscaFunctionType, final JsonNode node,
112 final DeserializationContext context) throws JsonMappingException {
113 final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition();
114 toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType).orElse(null));
115 toscaGetFunction.setSourceName(getAsTextOrElseNull(node, "sourceName"));
116 toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
117 toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
118 toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
119 toscaGetFunction.setToscaIndex(getNumberAsTextOrElseNull(node, "toscaIndex"));
120 final String propertySource = getAsTextOrElseNull(node, "propertySource");
121 if (StringUtils.isNotEmpty(propertySource)) {
122 final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
123 context.instantiationException(ToscaGetFunctionDataDefinition.class,
124 String.format("Invalid propertySource '%s'", propertySource))
126 toscaGetFunction.setPropertySource(propertySource1);
128 final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
129 if (propertyPathFromSourceNode != null) {
130 if (!propertyPathFromSourceNode.isArray()) {
131 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
133 final List<String> pathFromSource = new ArrayList<>();
134 propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
135 toscaGetFunction.setPropertyPathFromSource(pathFromSource);
138 return toscaGetFunction;
141 private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
142 final JsonNode jsonNode = node.get(fieldName);
143 if (jsonNode == null) {
146 if (!jsonNode.isTextual()) {
149 return jsonNode.asText();
152 private Object getNumberAsTextOrElseNull(final JsonNode node, final String fieldName) {
153 final JsonNode jsonNode = node.get(fieldName);
154 if (jsonNode == null) {
157 if (jsonNode.asText().equalsIgnoreCase("INDEX")) {
158 return jsonNode.asText();
161 Integer.parseInt(jsonNode.asText());
162 } catch(Exception e) {
165 return Integer.parseInt(jsonNode.asText());
168 private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
169 final DeserializationContext context) throws IOException {
170 final var toscaConcatFunction = new ToscaConcatFunction();
171 List<ToscaFunctionParameter> functionParameterList = getParameters(concatFunctionJsonNode, context);
172 toscaConcatFunction.setParameters(functionParameterList);
173 return toscaConcatFunction;
176 private ToscaCustomFunction deserializeCustomFunction(final JsonNode customFunctionJsonNode,
177 final DeserializationContext context) throws IOException {
178 final var toscaCustomFunction = new ToscaCustomFunction();
179 final String name = getAsTextOrElseNull(customFunctionJsonNode, "name");
181 throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
183 toscaCustomFunction.setName(name);
184 toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(name));
185 List<ToscaFunctionParameter> functionParameterList = getParameters(customFunctionJsonNode, context);
186 toscaCustomFunction.setParameters(functionParameterList);
187 if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) {
188 validateGetInput(toscaCustomFunction, context);
190 return toscaCustomFunction;
193 private ToscaFunctionType getCustomFunctionType(String name) {
194 List<Configuration.CustomToscaFunction> customFunctions =
195 ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
196 if (customFunctions.isEmpty()) {
197 return ToscaFunctionType.CUSTOM;
199 Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
200 if (optionalFunc.isEmpty()) {
201 return ToscaFunctionType.CUSTOM;
203 String type = optionalFunc.get().getType();
204 return ToscaFunctionType.findType(type).get();
207 private void validateGetInput(ToscaCustomFunction toscaCustomFunction, final DeserializationContext context) throws IOException {
208 List<ToscaFunctionParameter> functionParameterList = toscaCustomFunction.getParameters();
209 if (functionParameterList.size() != 1) {
210 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain one GET_INPUT parameter");
212 ToscaFunctionParameter parameter = functionParameterList.get(0);
213 if (!ToscaFunctionType.GET_INPUT.equals(parameter.getType())) {
214 throw context.instantiationException(List.class, "Custom GET_INPUT function must contain a GET_INPUT parameter");
218 private List<ToscaFunctionParameter> getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException {
219 final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
220 final JsonNode parametersNode = functionJsonNode.get("parameters");
221 if (parametersNode == null) {
222 return functionParameterList;
224 if (!parametersNode.isArray()) {
225 throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
228 for (final JsonNode parameterNode : parametersNode) {
229 final JsonNode typeJsonNode = parameterNode.get("type");
230 if (typeJsonNode == null) {
231 throw context.instantiationException(ToscaFunction.class, "TOSCA function parameter type attribute not provided");
233 final String parameterType = typeJsonNode.asText();
234 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
235 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
236 String.format("Invalid TOSCA function parameter type '%s'", parameterType))
238 if (toscaFunctionType == ToscaFunctionType.STRING) {
239 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
240 toscaStringParameter.setValue(parameterNode.get("value").asText());
241 functionParameterList.add(toscaStringParameter);
243 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
244 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
247 return functionParameterList;