e210b93e710c0e932f5e06b89cdff4ae8ff65777
[sdc.git] / common-be / src / main / java / org / openecomp / sdc / be / datatypes / elements / ToscaFunctionJsonDeserializer.java
1 /*
2  * -
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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.datatypes.elements;
23
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;
30
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;
44
45 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
48
49     public ToscaFunctionJsonDeserializer() {
50         this(null);
51     }
52
53     public ToscaFunctionJsonDeserializer(Class<?> vc) {
54         super(vc);
55     }
56
57     @Override
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);
61     }
62
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();
70         } else {
71             throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
72         }
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))
76             );
77         if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
78             || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
79             return deserializeToscaGetFunction(toscaFunctionType, node, context);
80         }
81
82         if (toscaFunctionType == ToscaFunctionType.CONCAT) {
83             return this.deserializeConcatFunction(node, context);
84         }
85
86         if (toscaFunctionType == ToscaFunctionType.YAML) {
87             return this.deserializeYamlFunction(node, context);
88         }
89
90         if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
91             return this.deserializeCustomFunction(node, context);
92         }
93
94         return null;
95     }
96
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) {
101             return yamlFunction;
102         }
103         final String valueAsText = valueJsonNode.asText();
104         try {
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);
110         }
111         return yamlFunction;
112     }
113
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));
123
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))
129             );
130             toscaGetFunction.setPropertySource(propertySource1);
131         }
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");
136             }
137             final List<String> pathFromSource = new ArrayList<>();
138             propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
139             toscaGetFunction.setPropertyPathFromSource(pathFromSource);
140         }
141
142         return toscaGetFunction;
143     }
144
145     private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
146         final JsonNode jsonNode = node.get(fieldName);
147         if (jsonNode == null) {
148             return null;
149         }
150         if (!jsonNode.isTextual()) {
151             return null;
152         }
153         return jsonNode.asText();
154     }
155
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");
163             }
164
165             jsonNode.forEach(nodeValue -> {
166                 String indexValue = nodeValue.asText();
167                 if (StringUtils.isNumeric(indexValue)) {
168                     toscaIndexList.add(Integer.parseInt(indexValue));
169                 } else {
170                     toscaIndexList.add(indexValue);
171                 }
172             });
173         }
174         return toscaIndexList;
175     }
176
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;
183     }
184
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");
189         if (name == null) {
190             throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
191         }
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);
198         }
199         return toscaCustomFunction;
200     }
201
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;
207         }
208         Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
209         if (optionalFunc.isEmpty()) {
210             return ToscaFunctionType.CUSTOM;
211         }
212         String type = optionalFunc.get().getType();
213         return ToscaFunctionType.findType(type).get();
214     }
215
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");
220         }
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");
224         }
225     }
226
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;
232         }
233         if (!parametersNode.isArray()) {
234             throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
235         }
236
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");
241             }
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))
246                 );
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);
251             } else {
252                 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
253                 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
254             }
255         }
256         return functionParameterList;
257     }
258
259 }