Support adding data types to model
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / PropertyValidator.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 package org.openecomp.sdc.be.components.validation;
21
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Optional;
25 import org.openecomp.sdc.be.components.impl.utils.ExceptionUtils;
26 import org.openecomp.sdc.be.dao.api.ActionStatus;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.DataTypeDefinition;
29 import org.openecomp.sdc.be.model.PropertyDefinition;
30 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
31 import org.openecomp.sdc.exception.ResponseFormat;
32 import org.springframework.stereotype.Component;
33
34 @Component
35 public class PropertyValidator {
36
37     private final PropertyOperation propertyOperation;
38     private final ComponentsUtils componentsUtils;
39     private final ExceptionUtils exceptionUtils;
40
41     public PropertyValidator(PropertyOperation propertyOperation, ComponentsUtils componentsUtils,
42                              ExceptionUtils exceptionUtils) {
43         this.exceptionUtils = exceptionUtils;
44         this.propertyOperation = propertyOperation;
45         this.componentsUtils = componentsUtils;
46     }
47
48     public void thinPropertiesValidator(List<PropertyDefinition> properties, List<PropertyDefinition> dbAnnotationTypeDefinitionProperties,
49                                         Map<String, DataTypeDefinition> allDataTypes) {
50         for (PropertyDefinition property : properties) {
51             PropertyDefinition annotationTypeSpecificProperty = isPropertyInsideAnnotationTypeProperties(dbAnnotationTypeDefinitionProperties,
52                 property);
53             if (annotationTypeSpecificProperty != null) {
54                 verifyPropertyIsOfDefinedType(property, annotationTypeSpecificProperty, allDataTypes);
55             }
56         }
57     }
58
59     private void verifyPropertyIsOfDefinedType(PropertyDefinition property, PropertyDefinition typeSpecificProperty,
60                                                Map<String, DataTypeDefinition> allDataTypes) {
61         propertyOperation
62             .validateAndUpdatePropertyValue(typeSpecificProperty.getType(), property.getValue(), typeSpecificProperty.getSchemaType(), allDataTypes)
63             .left().on(error -> exceptionUtils.rollBackAndThrow(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName()));
64     }
65
66     private PropertyDefinition isPropertyInsideAnnotationTypeProperties(List<PropertyDefinition> dbAnnotationTypeDefinitionProperties,
67                                                                         PropertyDefinition property) {
68         Optional<PropertyDefinition> optionalResult = dbAnnotationTypeDefinitionProperties.stream()
69             .filter(prop -> prop.getName().equals(property.getName())).findFirst();
70         if (optionalResult.isPresent()) {
71             return optionalResult.get();
72         }
73         ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, property.getType(), property.getName());
74         exceptionUtils.rollBackAndThrow(responseFormat);
75         return null;
76     }
77 }