2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.apps.controllerblueprints.validation
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.AttributeDefinition
25 import org.onap.ccsdk.apps.controllerblueprints.core.format
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintAttributeDefinitionValidator
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
33 @Service("default-attribute-definition-validator")
34 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
35 open class BluePrintAttributeDefinitionValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintAttributeDefinitionValidator {
38 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
40 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
43 override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
44 attributeDefinition: AttributeDefinition) {
46 log.trace("Validating AttributeDefinition($name)")
47 this.bluePrintRuntimeService = bluePrintRuntimeService
48 val dataType: String = attributeDefinition.type
51 BluePrintTypes.validPrimitiveTypes().contains(dataType) -> {
54 BluePrintTypes.validCollectionTypes().contains(dataType) -> {
55 val entrySchemaType: String = attributeDefinition.entrySchema?.type
56 ?: throw BluePrintException("Entry schema for DataType ($dataType) for the property ($name) not found")
57 checkPrimitiveOrComplex(entrySchemaType, name)
59 else -> checkPropertyDataType(dataType, name)
63 private fun checkPrimitiveOrComplex(dataType: String, propertyName: String): Boolean {
64 if (BluePrintTypes.validPrimitiveTypes().contains(dataType) || checkDataType(dataType)) {
67 throw BluePrintException("DataType($dataType) for the attribute($propertyName) is not valid")
71 private fun checkPropertyDataType(dataTypeName: String, propertyName: String) {
73 val dataType = bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.get(dataTypeName)
74 ?: throw BluePrintException(format("DataType ({}) for the property ({}) not found", dataTypeName, propertyName))
76 checkValidDataTypeDerivedFrom(propertyName, dataType.derivedFrom)
79 private fun checkDataType(key: String): Boolean {
80 return bluePrintRuntimeService.bluePrintContext().serviceTemplate.dataTypes?.containsKey(key) ?: false
83 open fun checkValidDataTypeDerivedFrom(dataTypeName: String, derivedFrom: String) {
84 check(BluePrintTypes.validDataTypeDerivedFroms.contains(derivedFrom)) {
85 throw BluePrintException("Failed to get DataType($dataTypeName)'s derivedFrom($derivedFrom) definition ")