UI support for default custom function names with get_input structure
[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.yaml.snakeyaml.Yaml;
41
42 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
45
46     public ToscaFunctionJsonDeserializer() {
47         this(null);
48     }
49
50     public ToscaFunctionJsonDeserializer(Class<?> vc) {
51         super(vc);
52     }
53
54     @Override
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);
58     }
59
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();
67         } else {
68             throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
69         }
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))
73             );
74         if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
75             || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
76             return deserializeToscaGetFunction(toscaFunctionType, node, context);
77         }
78
79         if (toscaFunctionType == ToscaFunctionType.CONCAT) {
80             return this.deserializeConcatFunction(node, context);
81         }
82
83         if (toscaFunctionType == ToscaFunctionType.YAML) {
84             return this.deserializeYamlFunction(node, context);
85         }
86
87         if (toscaFunctionType == ToscaFunctionType.CUSTOM) {
88             return this.deserializeCustomFunction(node, context);
89         }
90
91         return null;
92     }
93
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) {
98             return yamlFunction;
99         }
100         final String valueAsText = valueJsonNode.asText();
101         try {
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);
107         }
108         return yamlFunction;
109     }
110
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))
125             );
126             toscaGetFunction.setPropertySource(propertySource1);
127         }
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");
132             }
133             final List<String> pathFromSource = new ArrayList<>();
134             propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
135             toscaGetFunction.setPropertyPathFromSource(pathFromSource);
136         }
137
138         return toscaGetFunction;
139     }
140
141     private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
142         final JsonNode jsonNode = node.get(fieldName);
143         if (jsonNode == null) {
144             return null;
145         }
146         if (!jsonNode.isTextual()) {
147             return null;
148         }
149         return jsonNode.asText();
150     }
151
152     private Object getNumberAsTextOrElseNull(final JsonNode node, final String fieldName) {
153         final JsonNode jsonNode = node.get(fieldName);
154         if (jsonNode == null) {
155             return null;
156         }
157         if (jsonNode.asText().equalsIgnoreCase("INDEX")) {
158             return jsonNode.asText();
159         }
160         try {
161             Integer.parseInt(jsonNode.asText());
162         } catch(Exception e) {
163             return null;
164         }
165         return Integer.parseInt(jsonNode.asText());
166     }
167
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;
174     }
175
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");
180         if (name == null) {
181             throw context.instantiationException(List.class, "Expecting a string for the 'name' entry");
182         }
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);
189         }
190         return toscaCustomFunction;
191     }
192
193     private ToscaFunctionType getCustomFunctionType(String name) {
194         List<Configuration.CustomToscaFunction> customFunctions =
195             ConfigurationManager.getConfigurationManager().getConfiguration().getDefaultCustomToscaFunctions();
196         if (customFunctions.isEmpty()) {
197             return ToscaFunctionType.CUSTOM;
198         }
199         Optional<Configuration.CustomToscaFunction> optionalFunc = customFunctions.stream().filter(func -> func.getName().equals(name)).findFirst();
200         if (optionalFunc.isEmpty()) {
201             return ToscaFunctionType.CUSTOM;
202         }
203         String type = optionalFunc.get().getType();
204         return ToscaFunctionType.findType(type).get();
205     }
206
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");
211         }
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");
215         }
216     }
217
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;
223         }
224         if (!parametersNode.isArray()) {
225             throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
226         }
227
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");
232             }
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))
237                 );
238             if (toscaFunctionType == ToscaFunctionType.STRING) {
239                 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
240                 toscaStringParameter.setValue(parameterNode.get("value").asText());
241                 functionParameterList.add(toscaStringParameter);
242             } else {
243                 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
244                 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
245             }
246         }
247         return functionParameterList;
248     }
249
250 }