e234a4666f3de7c544f85f9bd3d5bdbc5be1e299
[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 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.springframework.util.CollectionUtils;
41 import org.yaml.snakeyaml.Yaml;
42
43 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
46
47     public ToscaFunctionJsonDeserializer() {
48         this(null);
49     }
50
51     public ToscaFunctionJsonDeserializer(Class<?> vc) {
52         super(vc);
53     }
54
55     @Override
56     public ToscaFunction deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
57         final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
58         return deserializeToscaFunction(node, context);
59     }
60
61     private ToscaFunction deserializeToscaFunction(final JsonNode node, final DeserializationContext context) throws IOException {
62         final String functionType;
63         if (node.get("type") != null) {
64             functionType = node.get("type").asText();
65         } else if (node.get("functionType") != null) {
66             //support for legacy tosca function
67             functionType = node.get("functionType").asText();
68         } else {
69             throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
70         }
71         final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(functionType)
72             .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
73                 String.format("Invalid function type '%s' or attribute type not provided", functionType))
74             );
75         if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
76             || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
77             return deserializeToscaGetFunction(toscaFunctionType, node, context);
78         }
79
80         if (toscaFunctionType == ToscaFunctionType.CONCAT) {
81             return this.deserializeConcatFunction(node, context);
82         }
83
84         if (toscaFunctionType == ToscaFunctionType.YAML) {
85             return this.deserializeYamlFunction(node, context);
86         }
87
88         if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
89             return this.deserializeCustomFunction(node, context);
90         }
91
92         return null;
93     }
94
95     private ToscaFunction deserializeYamlFunction(final JsonNode node, final DeserializationContext context) throws JsonMappingException {
96         var yamlFunction = new CustomYamlFunction();
97         final JsonNode valueJsonNode = node.get("value");
98         if (valueJsonNode == null) {
99             return yamlFunction;
100         }
101         final String valueAsText = valueJsonNode.asText();
102         try {
103             yamlFunction.setYamlValue(new Yaml().load(valueAsText));
104         } catch (final Exception e) {
105             final String errorMsg = String.format("Could not parse YAML expression: '%s'", valueAsText);
106             LOGGER.debug(errorMsg, e);
107             throw context.instantiationException(ToscaFunction.class, errorMsg);
108         }
109         return yamlFunction;
110     }
111
112     private ToscaGetFunctionDataDefinition deserializeToscaGetFunction(final ToscaFunctionType toscaFunctionType, final JsonNode node,
113                                                                        final DeserializationContext context) throws JsonMappingException {
114         final ToscaGetFunctionDataDefinition toscaGetFunction = new ToscaGetFunctionDataDefinition();
115         toscaGetFunction.setFunctionType(ToscaGetFunctionType.fromToscaFunctionType(toscaFunctionType).orElse(null));
116         toscaGetFunction.setSourceName(getAsTextOrElseNull(node, "sourceName"));
117         toscaGetFunction.setSourceUniqueId(getAsTextOrElseNull(node, "sourceUniqueId"));
118         toscaGetFunction.setPropertyName(getAsTextOrElseNull(node, "propertyName"));
119         toscaGetFunction.setPropertyUniqueId(getAsTextOrElseNull(node, "propertyUniqueId"));
120         toscaGetFunction.setToscaIndexList(getNumberAsTextOrElseNull(node, "toscaIndexList", context));
121
122         final String propertySource = getAsTextOrElseNull(node, "propertySource");
123         if (StringUtils.isNotEmpty(propertySource)) {
124             final PropertySource propertySource1 = PropertySource.findType(propertySource).orElseThrow(() ->
125                 context.instantiationException(ToscaGetFunctionDataDefinition.class,
126                     String.format("Invalid propertySource '%s'", propertySource))
127             );
128             toscaGetFunction.setPropertySource(propertySource1);
129         }
130         final JsonNode propertyPathFromSourceNode = node.get("propertyPathFromSource");
131         if (propertyPathFromSourceNode != null) {
132             if (!propertyPathFromSourceNode.isArray()) {
133                 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for propertyPathFromSource attribute");
134             }
135             final List<String> pathFromSource = new ArrayList<>();
136             propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
137             toscaGetFunction.setPropertyPathFromSource(pathFromSource);
138         }
139
140         return toscaGetFunction;
141     }
142
143     private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
144         final JsonNode jsonNode = node.get(fieldName);
145         if (jsonNode == null) {
146             return null;
147         }
148         if (!jsonNode.isTextual()) {
149             return null;
150         }
151         return jsonNode.asText();
152     }
153
154     private List<Object> getNumberAsTextOrElseNull(final JsonNode node, final String fieldName, final DeserializationContext context)
155         throws JsonMappingException {
156         List<Object> toscaIndexList = new ArrayList<Object>();
157         final JsonNode jsonNode = node.get(fieldName);
158         if (jsonNode != null) {
159             if (!jsonNode.isArray()) {
160                 throw context.instantiationException(ToscaGetFunctionDataDefinition.class, "Expecting an array for toscaIndexList attribute");
161             }
162             for (int index = 0; index < jsonNode.size(); index++) {
163                 String textValue = jsonNode.get(index).asText();
164                 if (index % 2 == 0) {
165                     if (textValue.equalsIgnoreCase("INDEX")) {
166                         toscaIndexList.add(textValue);
167                     } else {
168                         try {
169                             toscaIndexList.add(Integer.parseInt(textValue));
170                         } catch (Exception e) {
171                             throw context.instantiationException(ToscaGetFunctionDataDefinition.class,
172                                 "Expecting a valid value for toscaIndex attribute");
173                         }
174                     }
175                 } else {
176                     toscaIndexList.add(textValue);
177                 }
178             }
179         }
180         return toscaIndexList;
181     }
182
183     private ToscaConcatFunction deserializeConcatFunction(final JsonNode concatFunctionJsonNode,
184                                                           final DeserializationContext context) throws IOException {
185         final var toscaConcatFunction = new ToscaConcatFunction();
186         List<ToscaFunctionParameter> functionParameterList = getParameters(concatFunctionJsonNode, context);
187         toscaConcatFunction.setParameters(functionParameterList);
188         return toscaConcatFunction;
189     }
190
191     private ToscaCustomFunction deserializeCustomFunction(final JsonNode customFunctionJsonNode,
192                                                           final DeserializationContext context) throws IOException {
193         final var toscaCustomFunction = new ToscaCustomFunction();
194         final String name = getAsTextOrElseNull(customFunctionJsonNode, "name");
195         if (name == null) {
196             throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
197         }
198         toscaCustomFunction.setName(name);
199         toscaCustomFunction.setToscaFunctionType(getCustomFunctionType(name));
200         List<ToscaFunctionParameter> functionParameterList = getParameters(customFunctionJsonNode, context);
201         toscaCustomFunction.setParameters(functionParameterList);
202         if (ToscaFunctionType.GET_INPUT.equals(toscaCustomFunction.getToscaFunctionType())) {
203             validateGetInput(toscaCustomFunction, context);
204         }
205         return toscaCustomFunction;
206     }
207
208     private ToscaFunctionType getCustomFunctionType(String name) {
209         List<Configuration.CustomToscaFunction> customFunctions =
210             ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
211         if (CollectionUtils.isEmpty(customFunctions)) {
212             return ToscaFunctionType.CUSTOM;
213         }
214         Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
215         if (optionalFunc.isEmpty()) {
216             return ToscaFunctionType.CUSTOM;
217         }
218         String type = optionalFunc.get().getType();
219         return ToscaFunctionType.findType(type).get();
220     }
221
222     private void validateGetInput(ToscaCustomFunction toscaCustomFunction, final DeserializationContext context) throws IOException {
223         List<ToscaFunctionParameter> functionParameterList = toscaCustomFunction.getParameters();
224         if (functionParameterList.size() != 1) {
225             throw context.instantiationException(List.class, "Custom GET_INPUT function must contain one GET_INPUT parameter");
226         }
227         ToscaFunctionParameter parameter = functionParameterList.get(0);
228         if (!ToscaFunctionType.GET_INPUT.equals(parameter.getType())) {
229             throw context.instantiationException(List.class, "Custom GET_INPUT function must contain a GET_INPUT parameter");
230         }
231     }
232
233     private List<ToscaFunctionParameter> getParameters(final JsonNode functionJsonNode, final DeserializationContext context) throws IOException {
234         final List<ToscaFunctionParameter> functionParameterList = new ArrayList<>();
235         final JsonNode parametersNode = functionJsonNode.get("parameters");
236         if (parametersNode == null) {
237             return functionParameterList;
238         }
239         if (!parametersNode.isArray()) {
240             throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
241         }
242
243         for (final JsonNode parameterNode : parametersNode) {
244             final JsonNode typeJsonNode = parameterNode.get("type");
245             if (typeJsonNode == null) {
246                 throw context.instantiationException(ToscaFunction.class, "TOSCA function parameter type attribute not provided");
247             }
248             final String parameterType = typeJsonNode.asText();
249             final ToscaFunctionType toscaFunctionType = ToscaFunctionType.findType(parameterType)
250                 .orElseThrow(() -> context.instantiationException(ToscaFunction.class,
251                     String.format("Invalid TOSCA function parameter type '%s'", parameterType))
252                 );
253             if (toscaFunctionType == ToscaFunctionType.STRING) {
254                 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
255                 toscaStringParameter.setValue(parameterNode.get("value").asText());
256                 functionParameterList.add(toscaStringParameter);
257             } else {
258                 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
259                 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
260             }
261         }
262         return functionParameterList;
263     }
264
265 }