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;
27 import java.util.Optional;
28 import org.apache.commons.lang3.StringUtils;
29 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
31 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
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.model.dto.FilterConstraintDto;
36 import org.openecomp.sdc.be.ui.model.UIConstraint;
38 public class FilterConstraintMapper {
40 public FilterConstraintDto mapFrom(final UIConstraint uiConstraint) {
41 final var filterConstraint = new FilterConstraintDto();
42 ConstraintType.findByType(uiConstraint.getConstraintOperator()).ifPresent(filterConstraint::setOperator);
43 filterConstraint.setCapabilityName(uiConstraint.getCapabilityName());
44 filterConstraint.setPropertyName(uiConstraint.getServicePropertyName());
45 filterConstraint.setTargetType(StringUtils.isEmpty(uiConstraint.getCapabilityName()) ? PropertyFilterTargetType.PROPERTY : PropertyFilterTargetType.CAPABILITY);
46 FilterValueType.findByName(uiConstraint.getSourceType()).ifPresent(filterConstraint::setValueType);
47 filterConstraint.setValue(parseValueFromUiConstraint(uiConstraint.getValue()));
48 return filterConstraint;
51 public FilterConstraintDto mapFrom(final PropertyFilterConstraintDataDefinition propertyFilterConstraint) {
52 var filterConstraintDto = new FilterConstraintDto();
53 filterConstraintDto.setTargetType(propertyFilterConstraint.getTargetType());
54 filterConstraintDto.setPropertyName(propertyFilterConstraint.getPropertyName());
55 filterConstraintDto.setCapabilityName(propertyFilterConstraint.getCapabilityName());
56 filterConstraintDto.setOperator(propertyFilterConstraint.getOperator());
57 filterConstraintDto.setValueType(propertyFilterConstraint.getValueType());
58 filterConstraintDto.setValue(propertyFilterConstraint.getValue());
59 return filterConstraintDto;
62 public PropertyFilterConstraintDataDefinition mapTo(final FilterConstraintDto filterConstraintDto) {
63 var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
64 propertyFilterConstraint.setTargetType(filterConstraintDto.getTargetType());
65 propertyFilterConstraint.setPropertyName(filterConstraintDto.getPropertyName());
66 propertyFilterConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
67 propertyFilterConstraint.setOperator(filterConstraintDto.getOperator());
68 propertyFilterConstraint.setValueType(filterConstraintDto.getValueType());
69 propertyFilterConstraint.setValue(filterConstraintDto.getValue());
70 return propertyFilterConstraint;
73 public UIConstraint mapToUiConstraint(final FilterConstraintDto filterConstraintDto) {
74 final var uiConstraint = new UIConstraint();
75 uiConstraint.setConstraintOperator(filterConstraintDto.getOperator().getType());
76 uiConstraint.setValue(filterConstraintDto.getValue());
77 uiConstraint.setCapabilityName(filterConstraintDto.getCapabilityName());
78 uiConstraint.setServicePropertyName(filterConstraintDto.getPropertyName());
79 uiConstraint.setSourceType(filterConstraintDto.getValueType().getName());
83 private Object parseValueFromUiConstraint(final Object value) {
84 if (!(value instanceof Map || value instanceof String)) {
87 final Map<?, ?> valueAsMap;
88 if (value instanceof String) {
90 valueAsMap = new Gson().fromJson((String) value, Map.class);
91 } catch (final Exception ignored) {
95 valueAsMap = (Map<?, ?>) value;
98 final Optional<ToscaFunction> toscaFunction = parseValueToToscaFunction(valueAsMap);
99 if (toscaFunction.isPresent()) {
100 return toscaFunction.get();
106 public Optional<ToscaFunction> parseValueToToscaFunction(final Object value) {
107 if (value instanceof ToscaFunction) {
108 return Optional.of((ToscaFunction) value);
110 return readToscaFunctionType(value).map(toscaFunctionType -> new ObjectMapper().convertValue(value, ToscaFunction.class));
113 private Optional<ToscaFunctionType> readToscaFunctionType(final Object toscaFunction) {
114 if (!(toscaFunction instanceof Map)) {
115 return Optional.empty();
117 final Object type = ((Map<?, ?>) toscaFunction).get("type");
118 if (type instanceof String) {
119 return ToscaFunctionType.findType((String) type);
121 return Optional.empty();