c2c80d7f771da9bd9039ee283b1ab4b7df8c26aa
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.validation
19
20 import org.slf4j.LoggerFactory
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
24 import org.onap.ccsdk.cds.controllerblueprints.core.format
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintPropertyDefinitionValidator
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.springframework.beans.factory.config.ConfigurableBeanFactory
29 import org.springframework.context.annotation.Scope
30 import org.springframework.stereotype.Service
31
32 @Service("default-property-definition-validator")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class BluePrintPropertyDefinitionValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintPropertyDefinitionValidator {
35
36     private val log= LoggerFactory.getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
37
38     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
39
40
41     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, propertyDefinition: PropertyDefinition) {
42         this.bluePrintRuntimeService = bluePrintRuntimeService
43
44
45         log.trace("Validating PropertyDefinition($name)")
46
47         val dataType: String = propertyDefinition.type
48
49         when {
50             BluePrintTypes.validPrimitiveTypes().contains(dataType) -> {
51                 // Do Nothing
52             }
53             BluePrintTypes.validComplexTypes().contains(dataType) -> {
54                 // Do Nothing
55             }
56             BluePrintTypes.validCollectionTypes().contains(dataType) -> {
57                 val entrySchemaType: String = propertyDefinition.entrySchema?.type
58                         ?: throw BluePrintException(format("Entry schema for DataType ({}) for the property ({}) not found", dataType, name))
59                 checkPrimitiveOrComplex(entrySchemaType, name)
60             }
61             else -> checkPropertyDataType(dataType, name)
62         }
63     }
64
65
66     private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
67         if (BluePrintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
68             return true
69         } else {
70             throw BluePrintException(format("DataType({}) for the property({}) is not valid", dataType, propertyName))
71         }
72     }
73
74     private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
75
76         val dataType = bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.get(dataTypeName)
77                 ?: throw BluePrintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
78
79         checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
80     }
81
82     private fun checkDataType(key: String): Boolean {
83         return bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.containsKey(key) ?: false
84     }
85
86     open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
87         check(BluePrintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
88             throw BluePrintException(format("Failed to get DataType({})'s  derivedFrom({}) definition ", dataTypeName, derivedFrom))
89         }
90     }
91 }