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(node.get("sourceName").asText());
109 toscaGetFunction.setPropertyUniqueId(node.get("propertyUniqueId").asText());
110 final String propertySource = node.get("propertySource").asText();
111 if (StringUtils.isNotEmpty(propertySource)) {
112 final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
113 context.instantiationException(ToscaGetFunctionDataDefinition.class,
114 String.format("Invalid propertySource '%s'", propertySource))
116 toscaGetFunction.setPropertySource(propertySource1);
118 toscaGetFunction.setPropertyName(node.get("propertyName").asText());
119 toscaGetFunction.setSourceName(node.get("sourceName").asText());
120 toscaGetFunction.setSourceUniqueId(node.get("sourceUniqueId").asText());
121 final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
122 if (propertyPathFromSourceNode != null) {
123 if (!propertyPathFromSourceNode.isArray()) {
124 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
126 final List<String> pathFromSource = new ArrayList<>();
127 propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
128 toscaGetFunction.setPropertyPathFromSource(pathFromSource);
131 return toscaGetFunction;
134 private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
135 final DeserializationContext context) throws IOException {
136 final var toscaConcatFunction = new ToscaConcatFunction();
137 final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
138 final JsonNode parametersNode = concatFunctionJsonNode.get("parameters");
139 if (!parametersNode.isArray()) {
140 throw context.instantiationException(List.class, "");
142 for (final JsonNode parameterNode : parametersNode) {
143 final JsonNode typeJsonNode = parameterNode.get("type");
144 if (typeJsonNode == null) {
145 throw context.instantiationException(ToscaConcatFunction.class, "TOSCA concat function parameter type attribute not provided");
147 final String parameterType = typeJsonNode.asText();
148 final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
149 .orElseThrow(() -> context.instantiationException(ToscaConcatFunction.class,
150 String.format("Invalid TOSCA concat function parameter type '%s'", parameterType))
152 if (toscaFunctionType == ToscaFunctionType.STRING) {
153 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
154 toscaStringParameter.setValue(parameterNode.get("value").asText());
155 functionParameterList.add(toscaStringParameter);
157 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
158 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
161 toscaConcatFunction.setParameters(functionParameterList);
162 return toscaConcatFunction;