Constraints in data type view
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / mapper / PropertyDefinitionDtoMapper.java
index 67600a5..6e8134c 100644 (file)
 
 package org.openecomp.sdc.be.model.mapper;
 
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.google.gson.Gson;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import org.apache.commons.collections4.CollectionUtils;
@@ -33,10 +38,15 @@ import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
 import org.openecomp.sdc.be.model.PropertyConstraint;
 import org.openecomp.sdc.be.model.PropertyDefinition;
 import org.openecomp.sdc.be.model.dto.PropertyDefinitionDto;
+import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 @NoArgsConstructor(access = AccessLevel.PRIVATE)
 public class PropertyDefinitionDtoMapper {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(PropertyDefinitionDtoMapper.class);
+
     public static PropertyDefinition mapTo(final PropertyDefinitionDto propertyDefinitionDto) {
         final var propertyDefinition = new PropertyDefinition();
         propertyDefinition.setUniqueId(propertyDefinitionDto.getUniqueId());
@@ -52,8 +62,23 @@ public class PropertyDefinitionDtoMapper {
         }
         if (CollectionUtils.isNotEmpty(propertyDefinitionDto.getConstraints())) {
             final List<PropertyConstraint> propertyConstraints = new ArrayList<>();
+
+            propertyDefinitionDto.getConstraints().forEach(rawConstraint -> {
+                ObjectMapper mapper = new ObjectMapper();
+
+                SimpleModule module = new SimpleModule("customDeserializationModule");
+                module.addDeserializer(PropertyConstraint.class, new PropertyOperation.PropertyConstraintJacksonDeserializer());
+                mapper.registerModule(module);
+                mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+                try {
+                    PropertyConstraint constraint =
+                        mapper.readValue(new Gson().toJson(rawConstraint, Map.class), PropertyConstraint.class);
+                    propertyConstraints.add(constraint);
+                } catch (JsonProcessingException e) {
+                    LOGGER.error("Could not parse constraint '{}' for property '{}'", rawConstraint, propertyDefinitionDto.getName());
+                }
+            });
             propertyDefinition.setConstraints(propertyConstraints);
-            propertyConstraints.addAll(propertyDefinitionDto.getConstraints());
         }
         propertyDefinition.setDescription(propertyDefinitionDto.getDescription());
         propertyDefinition.setValue(new Gson().toJson(propertyDefinitionDto.getValue()));
@@ -71,9 +96,7 @@ public class PropertyDefinitionDtoMapper {
         propertyDefinitionDto.setRequired(propertyDefinition.getRequired());
         propertyDefinitionDto.setSchemaType(propertyDefinition.getSchemaType());
         if (CollectionUtils.isNotEmpty(propertyDefinition.getConstraints())) {
-            final List<PropertyConstraint> propertyConstraints = new ArrayList<>();
-            propertyDefinitionDto.setConstraints(propertyConstraints);
-            propertyConstraints.addAll(propertyDefinition.getConstraints());
+            propertyDefinitionDto.setConstraints(new ArrayList<>(propertyDefinition.getConstraints()));
         }
         propertyDefinitionDto.setValue(new Gson().fromJson(propertyDataDefinition.getValue(), Object.class));
         propertyDefinitionDto.setDefaultValue(new Gson().fromJson(propertyDataDefinition.getDefaultValue(), Object.class));