Fix node filter API payload retro-compatibility
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / ui / mapper / FilterConstraintMapper.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.ui.mapper;
23
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.gson.Gson;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Optional;
29 import org.apache.commons.lang3.StringUtils;
30 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
32 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
33 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
34 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
35 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
36 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
37 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
38 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
39 import org.openecomp.sdc.be.model.dto.FilterConstraintDto;
40 import org.openecomp.sdc.be.ui.model.UIConstraint;
41
42 public class FilterConstraintMapper {
43
44     public FilterConstraintDto mapFrom(final UIConstraint uiConstraint) {
45         final var filterConstraint = new FilterConstraintDto();
46         ConstraintType.findByType(uiConstraint.getConstraintOperator()).ifPresent(filterConstraint::setOperator);
47         filterConstraint.setCapabilityName(uiConstraint.getCapabilityName());
48         filterConstraint.setPropertyName(uiConstraint.getServicePropertyName());
49         filterConstraint.setTargetType(StringUtils.isEmpty(uiConstraint.getCapabilityName()) ? PropertyFilterTargetType.PROPERTY : PropertyFilterTargetType.CAPABILITY);
50         FilterValueType.findByName(uiConstraint.getSourceType()).ifPresent(filterConstraint::setValueType);
51         filterConstraint.setValue(mapValueFrom(uiConstraint));
52         return filterConstraint;
53     }
54
55     private Object mapValueFrom(final UIConstraint uiConstraint) {
56         if (FilterValueType.GET_INPUT.getLegacyName().equals(uiConstraint.getSourceType())) {
57             final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
58             toscaGetFunctionDataDefinition.setPropertySource(PropertySource.SELF);
59             final String value = (String) uiConstraint.getValue();
60             toscaGetFunctionDataDefinition.setPropertyName(value);
61             toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
62             toscaGetFunctionDataDefinition.setPropertyPathFromSource(List.of(value));
63             return toscaGetFunctionDataDefinition;
64         }
65
66         if (FilterValueType.GET_PROPERTY.getLegacyName().equals(uiConstraint.getSourceType())) {
67             final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
68             toscaGetFunctionDataDefinition.setPropertySource(PropertySource.SELF);
69             final String value = (String) uiConstraint.getValue();
70             toscaGetFunctionDataDefinition.setPropertyName(value);
71             toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
72             toscaGetFunctionDataDefinition.setPropertyPathFromSource(List.of(value));
73             return toscaGetFunctionDataDefinition;
74         }
75
76         return parseValueFromUiConstraint(uiConstraint.getValue());
77     }
78
79     public FilterConstraintDto mapFrom(final PropertyFilterConstraintDataDefinition propertyFilterConstraint) {
80         var filterConstraintDto = new FilterConstraintDto();
81         filterConstraintDto.setTargetType(propertyFilterConstraint.getTargetType());
82         filterConstraintDto.setPropertyName(propertyFilterConstraint.getPropertyName());
83         filterConstraintDto.setCapabilityName(propertyFilterConstraint.getCapabilityName());
84         filterConstraintDto.setOperator(propertyFilterConstraint.getOperator());
85         filterConstraintDto.setValueType(propertyFilterConstraint.getValueType());
86         filterConstraintDto.setValue(propertyFilterConstraint.getValue());
87         return filterConstraintDto;
88     }
89
90     public PropertyFilterConstraintDataDefinition mapTo(final FilterConstraintDto filterConstraintDto) {
91         var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
92         propertyFilterConstraint.setTargetType(filterConstraintDto.getTargetType());
93         propertyFilterConstraint.setPropertyName(filterConstraintDto.getPropertyName());
94         propertyFilterConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
95         propertyFilterConstraint.setOperator(filterConstraintDto.getOperator());
96         propertyFilterConstraint.setValueType(filterConstraintDto.getValueType());
97         propertyFilterConstraint.setValue(filterConstraintDto.getValue());
98         return propertyFilterConstraint;
99     }
100
101     public UIConstraint mapToUiConstraint(final FilterConstraintDto filterConstraintDto) {
102         final var uiConstraint = new UIConstraint();
103         uiConstraint.setConstraintOperator(filterConstraintDto.getOperator().getType());
104         uiConstraint.setValue(filterConstraintDto.getValue());
105         uiConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
106         uiConstraint.setServicePropertyName(filterConstraintDto.getPropertyName());
107         uiConstraint.setSourceType(filterConstraintDto.getValueType().getName());
108         uiConstraint.setSourceName(uiConstraint.getSourceType());
109         return uiConstraint;
110     }
111
112     private Object parseValueFromUiConstraint(final Object value) {
113         if (!(value instanceof Map || value instanceof String)) {
114             return value;
115         }
116         final Map<?, ?> valueAsMap;
117         if (value instanceof String) {
118             try {
119                 valueAsMap = new Gson().fromJson((String) value, Map.class);
120             } catch (final Exception ignored) {
121                 return value;
122             }
123         } else {
124             valueAsMap = (Map<?, ?>) value;
125         }
126
127         final Optional<ToscaFunction> toscaFunction = parseValueToToscaFunction(valueAsMap);
128         if (toscaFunction.isPresent()) {
129             return toscaFunction.get();
130         }
131
132         return valueAsMap;
133     }
134
135     public Optional<ToscaFunction> parseValueToToscaFunction(final Object value) {
136         if (value instanceof ToscaFunction) {
137             return Optional.of((ToscaFunction) value);
138         }
139         return readToscaFunctionType(value).map(toscaFunctionType -> new ObjectMapper().convertValue(value, ToscaFunction.class));
140     }
141
142     private Optional<ToscaFunctionType> readToscaFunctionType(final Object toscaFunction) {
143         if (!(toscaFunction instanceof Map)) {
144             return Optional.empty();
145         }
146         final Object type = ((Map<?, ?>) toscaFunction).get("type");
147         if (type instanceof String) {
148             return ToscaFunctionType.findType((String) type);
149         }
150         return Optional.empty();
151     }
152
153 }