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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.be.datatypes.elements;
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;
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;
39 public class PropertyFilterConstraintDataDefinitionJsonDeserializer extends StdDeserializer<PropertyFilterConstraintDataDefinition> {
41 private static final Logger LOGGER = LoggerFactory.getLogger(PropertyFilterConstraintDataDefinitionJsonDeserializer.class);
42 private static final String COULD_NOT_PARSE_CLASS = "Could not parse {} value as {}";
44 public PropertyFilterConstraintDataDefinitionJsonDeserializer() {
48 public PropertyFilterConstraintDataDefinitionJsonDeserializer(Class<?> vc) {
53 public PropertyFilterConstraintDataDefinition deserialize(final JsonParser jsonParser, final DeserializationContext context) throws IOException {
54 final JsonNode node = jsonParser.getCodec().readTree(jsonParser);
56 if (node.isTextual()) {
57 return PropertyFilterConstraintDataDefinitionHelper.convertLegacyConstraint(node.asText());
60 final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
61 if (node.get("propertyName") != null) {
62 propertyFilterConstraint.setPropertyName(node.get("propertyName").asText());
64 if (node.get("capabilityName") != null) {
65 propertyFilterConstraint.setCapabilityName(node.get("capabilityName").asText());
67 if (node.get("targetType") != null) {
68 propertyFilterConstraint.setTargetType(PropertyFilterTargetType.valueOf(node.get("targetType").asText()));
70 if (node.get("operator") != null) {
71 propertyFilterConstraint.setOperator(ConstraintType.valueOf(node.get("operator").asText()));
73 if (node.get("valueType") != null) {
74 propertyFilterConstraint.setValueType(FilterValueType.valueOf(node.get("valueType").asText()));
76 propertyFilterConstraint.setValue(deserializeValue(node.get("value")));
78 return propertyFilterConstraint;
81 private Object deserializeValue(final JsonNode value) {
82 final ObjectMapper objectMapper = new ObjectMapper();
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);
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);
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);
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);