Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-validation / src / main / kotlin / org / onap / ccsdk / cds / 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.cds.controllerblueprints.validation
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.format
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintPropertyDefinitionValidator
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTypeValidatorService
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
27 import org.slf4j.LoggerFactory
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) :
35     BlueprintPropertyDefinitionValidator {
36
37     private val log = LoggerFactory.getLogger(BlueprintServiceTemplateValidatorImpl::class.toString())
38
39     lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
40
41     override fun validate(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, propertyDefinition: PropertyDefinition) {
42         this.bluePrintRuntimeService = bluePrintRuntimeService
43
44         log.trace("Validating PropertyDefinition($name)")
45
46         val dataType: String = propertyDefinition.type
47
48         when {
49             BlueprintTypes.validPrimitiveTypes().contains(dataType) -> {
50                 // Do Nothing
51             }
52             BlueprintTypes.validComplexTypes().contains(dataType) -> {
53                 // Do Nothing
54             }
55             BlueprintTypes.validCollectionTypes().contains(dataType) -> {
56                 val entrySchemaType: String = propertyDefinition.entrySchema?.type
57                     ?: throw BlueprintException(format("Entry schema for DataType ({}) for the property ({}) not found", dataType, name))
58                 checkPrimitiveOrComplex(entrySchemaType, name)
59             }
60             else -> checkPropertyDataType(dataType, name)
61         }
62     }
63
64     private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
65         if (BlueprintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
66             return true
67         } else {
68             throw BlueprintException(format("DataType({}) for the property({}) is not valid", dataType, propertyName))
69         }
70     }
71
72     private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
73
74         val dataType = bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.get(dataTypeName)
75             ?: throw BlueprintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
76
77         checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
78     }
79
80     private fun checkDataType(key: String): Boolean {
81         return bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.containsKey(key) ?: false
82     }
83
84     open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
85         check(BlueprintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
86             throw BlueprintException(format("Failed to get DataType({})'s  derivedFrom({}) definition ", dataTypeName, derivedFrom))
87         }
88     }
89 }