Implement Enhancer Framework Interfaces
[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.BluePrintError
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.BluePrintContext
29
30 open class BluePrintPropertyDefinitionValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintPropertyDefinitionValidator {
31
32     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
33
34     var bluePrintContext: BluePrintContext? = null
35     var error: BluePrintError? = null
36
37     override fun validate(bluePrintContext: BluePrintContext, error: BluePrintError, name: String, propertyDefinition: PropertyDefinition) {
38         this.bluePrintContext = bluePrintContext
39         this.error = error
40
41         log.trace("Validating PropertyDefinition($name)")
42
43         val dataType: String = propertyDefinition.type
44
45         when {
46             BluePrintTypes.validPrimitiveTypes().contains(dataType) -> {
47                 // Do Nothing
48             }
49             BluePrintTypes.validCollectionTypes().contains(dataType) -> {
50                 val entrySchemaType: String = propertyDefinition.entrySchema?.type
51                         ?: throw BluePrintException(format("Entry schema for DataType ({}) for the property ({}) not found", dataType, name))
52                 checkPrimitiveOrComplex(entrySchemaType, name)
53             }
54             else -> checkPropertyDataType(dataType, name)
55         }
56     }
57
58
59     private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
60         if (BluePrintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
61             return true
62         } else {
63             throw BluePrintException(format("DataType({}) for the property({}) is not valid", dataType, propertyName))
64         }
65     }
66
67     private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
68
69         val dataType = bluePrintContext!!.serviceTemplate.dataTypes?.get(dataTypeName)
70                 ?: throw BluePrintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
71
72         checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
73     }
74
75     private fun checkDataType(key: String): Boolean {
76         return bluePrintContext!!.serviceTemplate.dataTypes?.containsKey(key) ?: false
77     }
78
79     open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
80         check(BluePrintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
81             throw BluePrintException(format("Failed to get DataType({})'s  derivedFrom({}) definition ", dataTypeName, derivedFrom))
82         }
83     }
84 }