fda832b98e443e4dca8c1bb3445cc1cf3bb791b9
[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(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))
115             );
116             toscaGetFunction.setPropertySource(propertySource1);
117         }
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");
125             }
126             final List<String> pathFromSource = new ArrayList<>();
127             propertyPathFromSourceNode.forEach(jsonNode -> pathFromSource.add(jsonNode.asText()));
128             toscaGetFunction.setPropertyPathFromSource(pathFromSource);
129         }
130
131         return toscaGetFunction;
132     }
133
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, "");
141         }
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");
146             }
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))
151                 );
152             if (toscaFunctionType == ToscaFunctionType.STRING) {
153                 final ToscaStringParameter toscaStringParameter = new ToscaStringParameter();
154                 toscaStringParameter.setValue(parameterNode.get("value").asText());
155                 functionParameterList.add(toscaStringParameter);
156             } else {
157                 final ToscaFunction toscaFunction = this.deserializeToscaFunction(parameterNode, context);
158                 functionParameterList.add((ToscaFunctionParameter) toscaFunction);
159             }
160         }
161         toscaConcatFunction.setParameters(functionParameterList);
162         return toscaConcatFunction;
163     }
164
165 }