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.ui.mapper;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.gson.Gson;
26 import java.util.ArrayList;
27 import java.util.List;
29 import java.util.Optional;
30 import org.apache.commons.lang3.StringUtils;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
33 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
34 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
35 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
36 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
37 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
38 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
39 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
40 import org.openecomp.sdc.be.model.dto.FilterConstraintDto;
41 import org.openecomp.sdc.be.ui.model.UIConstraint;
43 public class FilterConstraintMapper {
45 public FilterConstraintDto mapFrom(final UIConstraint uiConstraint) {
46 final var filterConstraint = new FilterConstraintDto();
47 ConstraintType.findByType(uiConstraint.getConstraintOperator()).ifPresent(filterConstraint::setOperator);
48 filterConstraint.setCapabilityName(uiConstraint.getCapabilityName());
49 filterConstraint.setPropertyName(uiConstraint.getServicePropertyName());
50 filterConstraint.setTargetType(StringUtils.isEmpty(uiConstraint.getCapabilityName()) ? PropertyFilterTargetType.PROPERTY : PropertyFilterTargetType.CAPABILITY);
51 FilterValueType.findByName(uiConstraint.getSourceType()).ifPresent(filterConstraint::setValueType);
52 filterConstraint.setValue(mapValueFrom(uiConstraint));
53 filterConstraint.setOriginalType(uiConstraint.getOriginalType());
54 return filterConstraint;
57 private Object mapValueFrom(final UIConstraint uiConstraint) {
58 if (FilterValueType.GET_INPUT.getLegacyName().equals(uiConstraint.getSourceType())) {
59 final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
60 toscaGetFunctionDataDefinition.setPropertySource(PropertySource.SELF);
61 final String value = (String) uiConstraint.getValue();
62 toscaGetFunctionDataDefinition.setPropertyName(value);
63 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_INPUT);
64 toscaGetFunctionDataDefinition.setPropertyPathFromSource(List.of(value));
65 return toscaGetFunctionDataDefinition;
68 if (FilterValueType.GET_PROPERTY.getLegacyName().equals(uiConstraint.getSourceType())) {
69 final ToscaGetFunctionDataDefinition toscaGetFunctionDataDefinition = new ToscaGetFunctionDataDefinition();
70 toscaGetFunctionDataDefinition.setPropertySource(PropertySource.SELF);
71 final String value = (String) uiConstraint.getValue();
72 toscaGetFunctionDataDefinition.setPropertyName(value);
73 toscaGetFunctionDataDefinition.setFunctionType(ToscaGetFunctionType.GET_PROPERTY);
74 toscaGetFunctionDataDefinition.setPropertyPathFromSource(List.of(value));
75 return toscaGetFunctionDataDefinition;
78 return parseValueFromUiConstraint(uiConstraint.getValue());
81 public FilterConstraintDto mapFrom(final PropertyFilterConstraintDataDefinition propertyFilterConstraint) {
82 var filterConstraintDto = new FilterConstraintDto();
83 filterConstraintDto.setTargetType(propertyFilterConstraint.getTargetType());
84 filterConstraintDto.setPropertyName(propertyFilterConstraint.getPropertyName());
85 filterConstraintDto.setCapabilityName(propertyFilterConstraint.getCapabilityName());
86 filterConstraintDto.setOperator(propertyFilterConstraint.getOperator());
87 filterConstraintDto.setValueType(propertyFilterConstraint.getValueType());
88 filterConstraintDto.setValue(propertyFilterConstraint.getValue());
89 return filterConstraintDto;
92 public PropertyFilterConstraintDataDefinition mapTo(final FilterConstraintDto filterConstraintDto) {
93 var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
94 propertyFilterConstraint.setTargetType(filterConstraintDto.getTargetType());
95 propertyFilterConstraint.setPropertyName(filterConstraintDto.getPropertyName());
96 propertyFilterConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
97 propertyFilterConstraint.setOperator(filterConstraintDto.getOperator());
98 propertyFilterConstraint.setValueType(filterConstraintDto.getValueType());
99 propertyFilterConstraint.setValue(filterConstraintDto.getValue());
100 propertyFilterConstraint.setOriginalType(filterConstraintDto.getOriginalType());
101 return propertyFilterConstraint;
104 public UIConstraint mapToUiConstraint(final FilterConstraintDto filterConstraintDto) {
105 final var uiConstraint = new UIConstraint();
106 uiConstraint.setConstraintOperator(filterConstraintDto.getOperator().getType());
107 uiConstraint.setValue(filterConstraintDto.getValue());
108 uiConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
109 uiConstraint.setServicePropertyName(filterConstraintDto.getPropertyName());
110 uiConstraint.setSourceType(filterConstraintDto.getValueType().getName());
111 uiConstraint.setSourceName(uiConstraint.getSourceType());
112 uiConstraint.setOriginalType(uiConstraint.getOriginalType());
116 private Object parseValueFromUiConstraint(final Object value) {
117 if (!(value instanceof Map || value instanceof List || value instanceof String)) {
120 if (value instanceof List) {
121 List<ToscaFunction> listValue = new ArrayList<>();
122 ToscaFunction valueObject;
123 for (Object obj: (List)value) {
125 valueObject = (ToscaFunction) getValueObject(obj);
127 catch (Exception e) {
130 listValue.add(valueObject);
134 return getValueObject(value);
137 private Object getValueObject(Object value) {
138 final Map<?, ?> valueAsMap;
139 if (value instanceof String) {
141 valueAsMap = new Gson().fromJson((String) value, Map.class);
142 } catch (final Exception ignored) {
146 valueAsMap = (Map<?, ?>) value;
149 final Optional<ToscaFunction> toscaFunction = parseValueToToscaFunction(valueAsMap);
150 if (toscaFunction.isPresent()) {
151 return toscaFunction.get();
157 public Optional<ToscaFunction> parseValueToToscaFunction(final Object value) {
158 if (value instanceof ToscaFunction) {
159 return Optional.of((ToscaFunction) value);
161 return readToscaFunctionType(value).map(toscaFunctionType -> new ObjectMapper().convertValue(value, ToscaFunction.class));
164 private Optional<ToscaFunctionType> readToscaFunctionType(final Object toscaFunction) {
165 if (!(toscaFunction instanceof Map)) {
166 return Optional.empty();
168 final Object type = ((Map<?, ?>) toscaFunction).get("type");
169 if (type instanceof String) {
170 return ToscaFunctionType.findType((String) type);
172 return Optional.empty();