re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / AnnotationValidator.java
1 package org.openecomp.sdc.be.components.validation;
2
3 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
4 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
5 import org.openecomp.sdc.be.datatypes.elements.Annotation;
6 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
7 import org.openecomp.sdc.be.impl.ComponentsUtils;
8 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
9 import org.openecomp.sdc.be.model.DataTypeDefinition;
10 import org.openecomp.sdc.be.model.PropertyDefinition;
11 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
12 import org.openecomp.sdc.common.log.wrappers.Logger;
13 import org.springframework.stereotype.Component;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 @Component
20 public class AnnotationValidator {
21
22
23     private final PropertyValidator propertyValidator;
24     private final ExceptionUtils exceptionUtils;
25     private final ApplicationDataTypeCache dataTypeCache;
26     private final ComponentsUtils componentsUtils;
27
28     private static final Logger log = Logger.getLogger(ResourceImportManager.class);
29
30
31     public AnnotationValidator(PropertyValidator propertyValidator
32                                , ExceptionUtils exceptionUtils, ApplicationDataTypeCache dataTypeCache
33                                ,ComponentsUtils componentsUtils) {
34         this.propertyValidator = propertyValidator;
35         this.exceptionUtils = exceptionUtils;
36         this.dataTypeCache = dataTypeCache;
37         this.componentsUtils = componentsUtils;
38     }
39
40     public List<Annotation> validateAnnotationsProperties(Annotation annotation, AnnotationTypeDefinition dbAnnotationTypeDefinition) {
41         List<Annotation> validAnnotations = new ArrayList<>();
42         if (annotation != null && propertiesValidator(
43                 annotation.getProperties(), dbAnnotationTypeDefinition.getProperties())) {
44                     validAnnotations.add(annotation);
45         }
46         return validAnnotations;
47     }
48
49     private boolean propertiesValidator(List<PropertyDataDefinition> properties, List<PropertyDefinition> dbAnnotationTypeDefinitionProperties) {
50         List<PropertyDefinition> propertyDefinitionsList = new ArrayList<>();
51         if (properties == null || dbAnnotationTypeDefinitionProperties == null) {
52             return false;
53         }
54         properties.stream()
55                 .forEach(property -> propertyDefinitionsList.add(new PropertyDefinition(property)));
56         Map<String, DataTypeDefinition> allDataTypes = dataTypeCache.getAll()
57                 .left()
58                 .on( error -> exceptionUtils
59                 .rollBackAndThrow(error));
60         propertyValidator.thinPropertiesValidator(propertyDefinitionsList, dbAnnotationTypeDefinitionProperties, allDataTypes);
61         return true;
62     }
63
64 }