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