1 package org.openecomp.sdc.be.components.impl;
3 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
4 import org.openecomp.sdc.be.dao.utils.MapUtil;
5 import org.openecomp.sdc.be.datatypes.elements.Annotation;
6 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
7 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
8 import org.openecomp.sdc.be.model.InputDefinition;
9 import org.openecomp.sdc.be.model.PropertyDefinition;
10 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
11 import org.springframework.stereotype.Component;
15 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
18 public class AnnotationBusinessLogic {
20 private final AnnotationTypeOperations annotationTypeOperations;
22 private final AnnotationValidator annotationValidator;
24 public AnnotationBusinessLogic(AnnotationTypeOperations annotationTypeOperations,
25 AnnotationValidator annotationValidator){
26 this.annotationTypeOperations = annotationTypeOperations;
27 this.annotationValidator = annotationValidator;
30 public void validateAndMergeAnnotationsAndAssignToInput(Map<String, InputDefinition> inputs) {
31 if (!inputs.isEmpty()){
32 for (InputDefinition input : inputs.values()) {
33 List<Annotation> inputAnnotationList = input.getAnnotations();
34 if (isNotEmpty(inputAnnotationList)) {
35 for (Annotation annotation : inputAnnotationList) {
36 AnnotationTypeDefinition dbAnnotationTypeDefinition = annotationTypeOperations.getLatestType(annotation.getType());
37 validateMergeAndSetAnnoProps(annotation, dbAnnotationTypeDefinition);
40 input.setAnnotations(inputAnnotationList);
45 public AnnotationTypeOperations getAnnotationTypeOperations() {
46 return annotationTypeOperations;
49 private void validateMergeAndSetAnnoProps(Annotation annotation, AnnotationTypeDefinition dbAnnotationTypeDefinition) {
50 annotationValidator.validateAnnotationsProperties(annotation, dbAnnotationTypeDefinition);
51 List<PropertyDataDefinition> mergedPropertiesList = mergePropsOfAnnoDataTypeWithParsedAnnoProps(annotation.getProperties(), dbAnnotationTypeDefinition.getProperties());
52 annotation.setProperties(mergedPropertiesList);
55 private List<PropertyDataDefinition> mergePropsOfAnnoDataTypeWithParsedAnnoProps(List<PropertyDataDefinition> annoProperties, List<PropertyDefinition> typePropertiesList) {
56 Set<PropertyDataDefinition> mergedPropertiesSet = new HashSet<>(typePropertiesList);
57 Map<String, PropertyDefinition> typePropsMap = MapUtil.toMap(typePropertiesList, PropertyDefinition::getName);
58 for (PropertyDataDefinition propertyDataDefinitionObject : annoProperties) {
59 PropertyDefinition foundTypePropertyDefinitionObject = typePropsMap.get(propertyDataDefinitionObject.getName());
60 //The case of unexisting property was already handled in the validation process (result: failure)
61 PropertyDataDefinition modifiedPropertyDataObject = new PropertyDataDefinition(foundTypePropertyDefinitionObject);
62 modifiedPropertyDataObject.setValue(propertyDataDefinitionObject.getValue());
63 mergedPropertiesSet.add(modifiedPropertyDataObject);
65 return new ArrayList<>(mergedPropertiesSet);