Support TOSCA functions in Node Filters
[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 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;
38
39 public class ToscaFunctionJsonDeserializer extends StdDeserializer<ToscaFunction> {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(ToscaFunctionJsonDeserializer.class);
42
43     public ToscaFunctionJsonDeserializer() {
44         this(null);
45     }
46
47     public ToscaFunctionJsonDeserializer(Class<?> vc) {
48         super(vc);
49     }
50
51     @Override
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);
55     }
56
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();
64         } else {
65             throw context.instantiationException(ToscaFunction.class, "Attribute type not provided");
66         }
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))
70             );
71         if (toscaFunctionType == ToscaFunctionType.GET_INPUT || toscaFunctionType == ToscaFunctionType.GET_ATTRIBUTE
72             || toscaFunctionType == ToscaFunctionType.GET_PROPERTY) {
73             return deserializeToscaGetFunction(toscaFunctionType, node, context);
74         }
75
76         if (toscaFunctionType == ToscaFunctionType.CONCAT) {
77             return this.deserializeConcatFunction(node, context);
78         }
79
80         if (toscaFunctionType == ToscaFunctionType.YAML) {
81             return this.deserializeYamlFunction(node, context);
82         }
83
84         return null;
85     }
86
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) {
91             return yamlFunction;
92         }
93         final String valueAsText = valueJsonNode.asText();
94         try {
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);
100         }
101         return yamlFunction;
102     }
103
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))
117             );
118             toscaGetFunction.setPropertySource(propertySource1);
119         }
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");
124             }
125             final List<String> pathFromSource = new ArrayList<>();
126             propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
127             toscaGetFunction.setPropertyPathFromSource(pathFromSource);
128         }
129
130         return toscaGetFunction;
131     }
132
133     private String getAsTextOrElseNull(final JsonNode node, final String fieldName) {
134         final JsonNode jsonNode = node.get(fieldName);
135         if (jsonNode == null) {
136             return null;
137         }
138         if (!jsonNode.isTextual()) {
139             return null;
140         }
141         return jsonNode.asText();
142     }
143
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;
151         }
152         if (!parametersNode.isArray()) {
153             throw context.instantiationException(List.class, "Expecting an array for the 'parameters' entry");
154         }
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");
159             }
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))
164                 );
165             if (toscaFunctionType == ToscaFunctionType.STRING) {
166                 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
167                 toscaStringParameter.setValue(parameterNode.get("value").asText());
168                 functionParameterList.add(toscaStringParameter);
169             } else {
170                 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
171                 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
172             }
173         }
174         toscaConcatFunction.setParameters(functionParameterList);
175         return toscaConcatFunction;
176     }
177
178 }