Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / AnnotationValidator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.validation;
22
23 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
24 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
25 import org.openecomp.sdc.be.datatypes.elements.Annotation;
26 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
29 import org.openecomp.sdc.be.model.DataTypeDefinition;
30 import org.openecomp.sdc.be.model.PropertyDefinition;
31 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
32 import org.openecomp.sdc.common.log.wrappers.Logger;
33 import org.springframework.stereotype.Component;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Map;
38
39 @Component
40 public class AnnotationValidator {
41
42
43     private final PropertyValidator propertyValidator;
44     private final ExceptionUtils exceptionUtils;
45     private final ApplicationDataTypeCache dataTypeCache;
46     private final ComponentsUtils componentsUtils;
47
48     private static final Logger log = Logger.getLogger(ResourceImportManager.class);
49
50
51     public AnnotationValidator(PropertyValidator propertyValidator
52                                , ExceptionUtils exceptionUtils, ApplicationDataTypeCache dataTypeCache
53                                ,ComponentsUtils componentsUtils) {
54         this.propertyValidator = propertyValidator;
55         this.exceptionUtils = exceptionUtils;
56         this.dataTypeCache = dataTypeCache;
57         this.componentsUtils = componentsUtils;
58     }
59
60     public List<Annotation> validateAnnotationsProperties(Annotation annotation, AnnotationTypeDefinition dbAnnotationTypeDefinition) {
61         List<Annotation> validAnnotations = new ArrayList<>();
62         if (annotation != null && propertiesValidator(
63                 annotation.getProperties(), dbAnnotationTypeDefinition.getProperties())) {
64                     validAnnotations.add(annotation);
65         }
66         return validAnnotations;
67     }
68
69     private boolean propertiesValidator(List<PropertyDataDefinition> properties, List<PropertyDefinition> dbAnnotationTypeDefinitionProperties) {
70         List<PropertyDefinition> propertyDefinitionsList = new ArrayList<>();
71         if (properties == null || dbAnnotationTypeDefinitionProperties == null) {
72             return false;
73         }
74         properties.stream()
75                 .forEach(property -> propertyDefinitionsList.add(new PropertyDefinition(property)));
76         Map<String, DataTypeDefinition> allDataTypes = dataTypeCache.getAll()
77                 .left()
78                 .on( error -> exceptionUtils
79                 .rollBackAndThrow(error));
80         propertyValidator.thinPropertiesValidator(propertyDefinitionsList, dbAnnotationTypeDefinitionProperties, allDataTypes);
81         return true;
82     }
83
84 }