a767133a4ce6404b6183ed41e1d681477193ccf9
[sdc.git] /
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.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Map;
32 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
33 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
34 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
35 import org.openecomp.sdc.be.utils.PropertyFilterConstraintDataDefinitionHelper;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class PropertyFilterConstraintDataDefinitionJsonDeserializer extends StdDeserializer<PropertyFilterConstraintDataDefinition> {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(PropertyFilterConstraintDataDefinitionJsonDeserializer.class);
42     private static final String COULD_NOT_PARSE_CLASS = "Could not parse {} value as {}";
43
44     public PropertyFilterConstraintDataDefinitionJsonDeserializer() {
45         this(null);
46     }
47
48     public PropertyFilterConstraintDataDefinitionJsonDeserializer(Class<?> vc) {
49         super(vc);
50     }
51
52     @Override
53     public PropertyFilterConstraintDataDefinition deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
54         final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
55
56         if (node.isTextual()) {
57             return PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(node.asText());
58         }
59
60         final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
61         if (node.get("propertyName") != null) {
62             propertyFilterConstraint.setPropertyName(node.get("propertyName").asText());
63         }
64         if (node.get("capabilityName") != null) {
65             propertyFilterConstraint.setCapabilityName(node.get("capabilityName").asText());
66         }
67         if (node.get("targetType") != null) {
68             propertyFilterConstraint.setTargetType(PropertyFilterTargetType.valueOf(node.get("targetType").asText()));
69         }
70         if (node.get("operator") != null) {
71             propertyFilterConstraint.setOperator(ConstraintType.valueOf(node.get("operator").asText()));
72         }
73         if (node.get("valueType") != null) {
74             propertyFilterConstraint.setValueType(FilterValueType.valueOf(node.get("valueType").asText()));
75         }
76         propertyFilterConstraint.setValue(deserializeValue(node.get("value")));
77
78         return propertyFilterConstraint;
79     }
80
81     private Object deserializeValue(final JsonNode value) {
82         final ObjectMapper objectMapper = new ObjectMapper();
83         try {
84             return objectMapper.treeToValue(value, ToscaFunction.class);
85         } catch (final Exception e) {
86             LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), ToscaFunction.class.getName(), e);
87         }
88         try {
89             return objectMapper.treeToValue(value, Map.class);
90         } catch (final Exception e) {
91             LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), Map.class.getName(), e);
92         }
93         try {
94             return objectMapper.treeToValue(value, List.class);
95         } catch (final Exception e) {
96             LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), List.class.getName(), e);
97         }
98         try {
99             return objectMapper.treeToValue(value, String.class);
100         } catch (final Exception e) {
101             LOGGER.debug(COULD_NOT_PARSE_CLASS, PropertyFilterConstraintDataDefinition.class.getName(), String.class.getName(), e);
102         }
103
104         return null;
105     }
106
107 }