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.model.mapper;
24 import com.fasterxml.jackson.annotation.JsonInclude;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.databind.module.SimpleModule;
28 import com.google.gson.Gson;
29 import java.util.ArrayList;
30 import java.util.List;
32 import lombok.AccessLevel;
33 import lombok.NoArgsConstructor;
34 import org.apache.commons.collections4.CollectionUtils;
35 import org.apache.commons.lang3.StringUtils;
36 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
38 import org.openecomp.sdc.be.model.PropertyConstraint;
39 import org.openecomp.sdc.be.model.PropertyDefinition;
40 import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
41 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 @NoArgsConstructor(access = AccessLevel.PRIVATE)
46 public class PropertyDefinitionDtoMapper {
48 private static final Logger LOGGER = LoggerFactory.getLogger(PropertyDefinitionDtoMapper.class);
50 public static PropertyDefinition mapTo(final PropertyDefinitionDto propertyDefinitionDto) {
51 final var propertyDefinition = new PropertyDefinition();
52 propertyDefinition.setUniqueId(propertyDefinitionDto.getUniqueId());
53 propertyDefinition.setType(propertyDefinitionDto.getType());
54 propertyDefinition.setRequired(propertyDefinitionDto.getRequired());
55 propertyDefinition.setName(propertyDefinitionDto.getName());
56 if (StringUtils.isNotBlank(propertyDefinitionDto.getSchemaType())) {
57 final PropertyDefinition schemaProperty = new PropertyDefinition();
58 schemaProperty.setType(propertyDefinitionDto.getSchemaType());
59 final SchemaDefinition schema = new SchemaDefinition();
60 schema.setProperty(schemaProperty);
61 propertyDefinition.setSchema(schema);
63 if (CollectionUtils.isNotEmpty(propertyDefinitionDto.getConstraints())) {
64 final List<PropertyConstraint> propertyConstraints = new ArrayList<>();
66 propertyDefinitionDto.getConstraints().forEach(rawConstraint -> {
67 ObjectMapper mapper = new ObjectMapper();
69 SimpleModule module = new SimpleModule("customDeserializationModule");
70 module.addDeserializer(PropertyConstraint.class, new PropertyOperation.PropertyConstraintJacksonDeserializer());
71 mapper.registerModule(module);
72 mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
74 PropertyConstraint constraint =
75 mapper.readValue(new Gson().toJson(rawConstraint, Map.class), PropertyConstraint.class);
76 propertyConstraints.add(constraint);
77 } catch (JsonProcessingException e) {
78 LOGGER.error("Could not parse constraint '{}' for property '{}'", rawConstraint, propertyDefinitionDto.getName());
81 propertyDefinition.setConstraints(propertyConstraints);
83 propertyDefinition.setDescription(propertyDefinitionDto.getDescription());
84 propertyDefinition.setValue(new Gson().toJson(propertyDefinitionDto.getValue()));
85 propertyDefinition.setDefaultValue(new Gson().toJson(propertyDefinitionDto.getDefaultValue()));
86 return propertyDefinition;
89 public static PropertyDefinitionDto mapFrom(final PropertyDataDefinition propertyDataDefinition) {
90 final var propertyDefinition = new PropertyDefinition(propertyDataDefinition);
91 final var propertyDefinitionDto = new PropertyDefinitionDto();
92 propertyDefinitionDto.setUniqueId(propertyDefinition.getUniqueId());
93 propertyDefinitionDto.setName(propertyDefinition.getName());
94 propertyDefinitionDto.setType(propertyDefinition.getType());
95 propertyDefinitionDto.setDescription(propertyDefinition.getDescription());
96 propertyDefinitionDto.setRequired(propertyDefinition.getRequired());
97 propertyDefinitionDto.setSchemaType(propertyDefinition.getSchemaType());
98 if (CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
99 propertyDefinitionDto.setConstraints(new ArrayList<>(propertyDefinition.getConstraints()));
101 propertyDefinitionDto.setValue(new Gson().fromJson(propertyDataDefinition.getValue(), Object.class));
102 propertyDefinitionDto.setDefaultValue(new Gson().fromJson(propertyDataDefinition.getDefaultValue(), Object.class));
103 return propertyDefinitionDto;