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.List;
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;
42 public class FilterConstraintMapper {
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;
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;
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;
76 return parseValueFromUiConstraint(uiConstraint.getValue());
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;
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;
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());
112 private Object parseValueFromUiConstraint(final Object value) {
113 if (!(value instanceof Map || value instanceof String)) {
116 final Map<?, ?> valueAsMap;
117 if (value instanceof String) {
119 valueAsMap = new Gson().fromJson((String) value, Map.class);
120 } catch (final Exception ignored) {
124 valueAsMap = (Map<?, ?>) value;
127 final Optional<ToscaFunction> toscaFunction = parseValueToToscaFunction(valueAsMap);
128 if (toscaFunction.isPresent()) {
129 return toscaFunction.get();
135 public Optional<ToscaFunction> parseValueToToscaFunction(final Object value) {
136 if (value instanceof ToscaFunction) {
137 return Optional.of((ToscaFunction) value);
139 return readToscaFunctionType(value).map(toscaFunctionType -> new ObjectMapper().convertValue(value, ToscaFunction.class));
142 private Optional<ToscaFunctionType> readToscaFunctionType(final Object toscaFunction) {
143 if (!(toscaFunction instanceof Map)) {
144 return Optional.empty();
146 final Object type = ((Map<?, ?>) toscaFunction).get("type");
147 if (type instanceof String) {
148 return ToscaFunctionType.findType((String) type);
150 return Optional.empty();