Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / AnnotationBusinessLogic.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.impl;
22
23 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
24 import org.openecomp.sdc.be.dao.utils.MapUtil;
25 import org.openecomp.sdc.be.datatypes.elements.Annotation;
26 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
27 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
28 import org.openecomp.sdc.be.model.InputDefinition;
29 import org.openecomp.sdc.be.model.PropertyDefinition;
30 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
31 import org.springframework.stereotype.Component;
32
33 import java.util.ArrayList;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
40
41 @Component
42 public class AnnotationBusinessLogic {
43
44     private final AnnotationTypeOperations annotationTypeOperations;
45
46     private final AnnotationValidator annotationValidator;
47
48     public AnnotationBusinessLogic(AnnotationTypeOperations annotationTypeOperations,
49                                    AnnotationValidator annotationValidator){
50         this.annotationTypeOperations = annotationTypeOperations;
51         this.annotationValidator = annotationValidator;
52     }
53
54     public void validateAndMergeAnnotationsAndAssignToInput(Map<String, InputDefinition> inputs) {
55         if (!inputs.isEmpty()){
56             for (InputDefinition input : inputs.values()) {
57                 List<Annotation> inputAnnotationList = input.getAnnotations();
58                 if (isNotEmpty(inputAnnotationList)) {
59                     for (Annotation annotation : inputAnnotationList) {
60                         AnnotationTypeDefinition dbAnnotationTypeDefinition = annotationTypeOperations.getLatestType(annotation.getType());
61                         validateMergeAndSetAnnoProps(annotation, dbAnnotationTypeDefinition);
62                     }
63                 }
64                 input.setAnnotations(inputAnnotationList);
65             }
66         }
67     }
68
69     public AnnotationTypeOperations getAnnotationTypeOperations() {
70         return annotationTypeOperations;
71     }
72
73     private void validateMergeAndSetAnnoProps(Annotation annotation, AnnotationTypeDefinition dbAnnotationTypeDefinition) {
74         annotationValidator.validateAnnotationsProperties(annotation, dbAnnotationTypeDefinition);
75         List<PropertyDataDefinition> mergedPropertiesList = mergePropsOfAnnoDataTypeWithParsedAnnoProps(annotation.getProperties(), dbAnnotationTypeDefinition.getProperties());
76         annotation.setProperties(mergedPropertiesList);
77     }
78
79     private List<PropertyDataDefinition> mergePropsOfAnnoDataTypeWithParsedAnnoProps(List<PropertyDataDefinition> annoProperties, List<PropertyDefinition> typePropertiesList) {
80         Set<PropertyDataDefinition> mergedPropertiesSet = new HashSet<>(typePropertiesList);
81         Map<String, PropertyDefinition> typePropsMap = MapUtil.toMap(typePropertiesList, PropertyDefinition::getName);
82         for (PropertyDataDefinition propertyDataDefinitionObject : annoProperties) {
83             PropertyDefinition foundTypePropertyDefinitionObject = typePropsMap.get(propertyDataDefinitionObject.getName());
84             //The case of unexisting property was already handled in the validation process (result: failure)
85             PropertyDataDefinition modifiedPropertyDataObject = new PropertyDataDefinition(foundTypePropertyDefinitionObject);
86             modifiedPropertyDataObject.setValue(propertyDataDefinitionObject.getValue());
87             mergedPropertiesSet.add(modifiedPropertyDataObject);
88         }
89         return new ArrayList<>(mergedPropertiesSet);
90     }
91
92 }