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 org.apache.commons.lang3.StringUtils;
33 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.yaml.snakeyaml.Yaml;
39 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
41 private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
43 public ToscaFunctionJsonDeserializer() {
47 public ToscaFunctionJsonDeserializer(Class<?> vc) {
52 public ToscaFunction deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
53 final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
54 return deserializeToscaFunction(node, context);
57 private ToscaFunction deserializeToscaFunction(final JsonNode node, final DeserializationContext context) throws IOException {
58 final String functionType;
59 if (node.get("type") != null) {
60 functionType = node.get("type").asText();
61 } else if (node.get("functionType") != null) {
62 //support for legacy tosca function
63 functionType = node.get("functionType").asText();
65 throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
67 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType)
68 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
69 String.format("Invalid function type '%s' or attribute type not provided", functionType))
71 if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
72 || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
73 return deserializeToscaGetFunction(toscaFunctionType, node, context);
76 if (toscaFunctionType == ToscaFunctionType.CONCAT) {
77 return this.deserializeConcatFunction(node, context);
80 if (toscaFunctionType == ToscaFunctionType.YAML) {
81 return this.deserializeYamlFunction(node, context);
87 private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException {
88 var yamlFunction = new CustomYamlFunction();
89 final JsonNode valueJsonNode = node.get("value");
90 if (valueJsonNode == null) {
93 final String valueAsText = valueJsonNode.asText();
95 yamlFunction.setYamlValue(new Yaml().load(valueAsText));
96 } catch (final Exception e) {
97 final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText);
98 LOGGER.debug(errorMsg, e);
99 throw context.instantiationException(ToscaFunction.class, errorMsg);
104 private ToscaGetFunctionDataDefinition deserializeToscaGetFunction(final ToscaFunctionType toscaFunctionType, final JsonNode node,
105 final DeserializationContext context) throws JsonMappingException {
106 final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition();
107 toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType).orElse(null));
108 toscaGetFunction.setSourceName(getAsTextOrElseNull(node, "sourceName"));
109 toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
110 toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
111 toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
112 final String propertySource = getAsTextOrElseNull(node, "propertySource");
113 if (StringUtils.isNotEmpty(propertySource)) {
114 final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
115 context.instantiationException(ToscaGetFunctionDataDefinition.class,
116 String.format("Invalid propertySource '%s'", propertySource))
118 toscaGetFunction.setPropertySource(propertySource1);
120 final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
121 if (propertyPathFromSourceNode != null) {
122 if (!propertyPathFromSourceNode.isArray()) {
123 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
125 final List<String> pathFromSource = new ArrayList<>();
126 propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
127 toscaGetFunction.setPropertyPathFromSource(pathFromSource);
130 return toscaGetFunction;
133 private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
134 final JsonNode jsonNode = node.get(fieldName);
135 if (jsonNode == null) {
138 if (!jsonNode.isTextual()) {
141 return jsonNode.asText();
144 private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
145 final DeserializationContext context) throws IOException {
146 final var toscaConcatFunction = new ToscaConcatFunction();
147 final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
148 final JsonNode parametersNode = concatFunctionJsonNode.get("parameters");
149 if (parametersNode == null) {
150 return toscaConcatFunction;
152 if (!parametersNode.isArray()) {
153 throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
155 for (final JsonNode parameterNode : parametersNode) {
156 final JsonNode typeJsonNode = parameterNode.get("type");
157 if (typeJsonNode == null) {
158 throw context.instantiationException(ToscaConcatFunction.class, "TOSCA concat function parameter type attribute not provided");
160 final String parameterType = typeJsonNode.asText();
161 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
162 .orElseThrow(() -> context.instantiationException(ToscaConcatFunction.class,
163 String.format("Invalid TOSCA concat function parameter type '%s'", parameterType))
165 if (toscaFunctionType == ToscaFunctionType.STRING) {
166 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
167 toscaStringParameter.setValue(parameterNode.get("value").asText());
168 functionParameterList.add(toscaStringParameter);
170 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
171 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
174 toscaConcatFunction.setParameters(functionParameterList);
175 return toscaConcatFunction;