6f67cd73e27f8532aa99f49cc06bdb916f15c49a
[ccsdk/cds.git] /
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 org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintValidationError
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
23 import org.onap.ccsdk.apps.controllerblueprints.core.format
24 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintPropertyDefinitionValidator
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
27
28 open class BluePrintPropertyDefinitionValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintPropertyDefinitionValidator {
29
30     var bluePrintContext: BluePrintContext? = null
31     var error: BluePrintValidationError? = null
32
33     override fun validate(bluePrintContext: BluePrintContext, error: BluePrintValidationError, name: String, propertyDefinition: PropertyDefinition) {
34         this.bluePrintContext = bluePrintContext
35         this.error = error
36
37         val dataType: String = propertyDefinition.type
38
39         when {
40             BluePrintTypes.validPrimitiveTypes().contains(dataType) -> {
41                 // Do Nothing
42             }
43             BluePrintTypes.validCollectionTypes().contains(dataType) -> {
44                 val entrySchemaType: String = propertyDefinition.entrySchema?.type
45                         ?: throw BluePrintException(format("Entry schema for DataType ({}) for the property ({}) not found", dataType, name))
46                 checkPrimitiveOrComplex(entrySchemaType, name)
47             }
48             else -> checkPropertyDataType(dataType, name)
49         }
50     }
51
52
53     private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
54         if (BluePrintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
55             return true
56         } else {
57             throw BluePrintException(format("DataType({}) for the property({}) is not valid", dataType, propertyName))
58         }
59     }
60
61     private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
62
63         val dataType = bluePrintContext!!.serviceTemplate.dataTypes?.get(dataTypeName)
64                 ?: throw BluePrintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
65
66         checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
67     }
68
69     private fun checkDataType(key: String): Boolean {
70         return bluePrintContext!!.serviceTemplate.dataTypes?.containsKey(key) ?: false
71     }
72
73     open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
74         check(BluePrintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
75             throw BluePrintException(format("Failed to get DataType({})'s  derivedFrom({}) definition ", dataTypeName, derivedFrom))
76         }
77     }
78 }