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