Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-validation / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / validation / BluePrintPropertyDefinitionValidatorImpl.kt
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.apps.controllerblueprints.validation
19
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
25 import org.onap.ccsdk.apps.controllerblueprints.core.format
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintPropertyDefinitionValidator
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.annotation.Scope
31 import org.springframework.stereotype.Service
32
33 @Service("default-property-definition-validator")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class BluePrintPropertyDefinitionValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintPropertyDefinitionValidator {
36
37     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
38
39     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
40
41
42     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, propertyDefinition: PropertyDefinition) {
43         this.bluePrintRuntimeService = bluePrintRuntimeService
44
45
46         log.trace("Validating PropertyDefinition($name)")
47
48         val dataType: String = propertyDefinition.type
49
50         when {
51             BluePrintTypes.validPrimitiveTypes().contains(dataType) -> {
52                 // Do Nothing
53             }
54             BluePrintTypes.validComplexTypes().contains(dataType) -> {
55                 // Do Nothing
56             }
57             BluePrintTypes.validCollectionTypes().contains(dataType) -> {
58                 val entrySchemaType: String = propertyDefinition.entrySchema?.type
59                         ?: throw BluePrintException(format("Entry schema for DataType ({}) for the property ({}) not found", dataType, name))
60                 checkPrimitiveOrComplex(entrySchemaType, name)
61             }
62             else -> checkPropertyDataType(dataType, name)
63         }
64     }
65
66
67     private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
68         if (BluePrintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
69             return true
70         } else {
71             throw BluePrintException(format("DataType({}) for the property({}) is not valid", dataType, propertyName))
72         }
73     }
74
75     private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
76
77         val dataType = bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.get(dataTypeName)
78                 ?: throw BluePrintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
79
80         checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
81     }
82
83     private fun checkDataType(key: String): Boolean {
84         return bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.containsKey(key) ?: false
85     }
86
87     open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
88         check(BluePrintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
89             throw BluePrintException(format("Failed to get DataType({})'s  derivedFrom({}) definition ", dataTypeName, derivedFrom))
90         }
91     }
92 }