2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.impl;
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;
33 import java.util.ArrayList;
34 import java.util.HashSet;
35 import java.util.List;
39 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
42 public class AnnotationBusinessLogic {
44 private final AnnotationTypeOperations annotationTypeOperations;
46 private final AnnotationValidator annotationValidator;
48 public AnnotationBusinessLogic(AnnotationTypeOperations annotationTypeOperations,
49 AnnotationValidator annotationValidator){
50 this.annotationTypeOperations = annotationTypeOperations;
51 this.annotationValidator = annotationValidator;
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);
64 input.setAnnotations(inputAnnotationList);
69 public AnnotationTypeOperations getAnnotationTypeOperations() {
70 return annotationTypeOperations;
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);
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);
89 return new ArrayList<>(mergedPropertiesSet);