2 * Copyright © 2018 IBM.
3 * Modifications Copyright © 2017-2018 AT&T Intellectual Property.
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.resource.dict.service
20 import com.att.eelf.configuration.EELFLogger
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.google.common.base.Preconditions
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.apps.controllerblueprints.core.format
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService
30 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService
31 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
32 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
33 import com.att.eelf.configuration.EELFManager
34 import java.io.Serializable
36 * ResourceDefinitionValidationService.
38 * @author Brinda Santh
40 interface ResourceDefinitionValidationService : Serializable {
42 @Throws(BluePrintException::class)
43 fun validate(resourceDefinition: ResourceDefinition)
47 * ResourceDefinitionValidationService.
49 * @author Brinda Santh
51 open class ResourceDefinitionDefaultValidationService(private val bluePrintRepoService: BluePrintRepoService) : ResourceDefinitionValidationService {
53 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionValidationService::class.java)
55 override fun validate(resourceDefinition: ResourceDefinition) {
56 Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
57 log.trace("Validating Resource Dictionary Definition {}", resourceDefinition.name)
59 resourceDefinition.sources.forEach { (name, nodeTemplate) ->
60 val sourceType = nodeTemplate.type
62 val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)?.block()
63 ?: throw BluePrintException(format("Failed to get source({}) node type definition({})", name, sourceType))
65 // Validate Property Name, expression, values and Data Type
66 validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
71 open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
72 nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
76 open fun validatePropertyAssignments(nodeTypeProperties: MutableMap<String, PropertyDefinition>,
77 properties: MutableMap<String, JsonNode>) {
78 properties.forEach { propertyName, propertyAssignment ->
79 val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName]
80 ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName))
81 // Check and Validate if Expression Node
82 val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment)
83 if (!expressionData.isExpression) {
84 checkPropertyValue(propertyDefinition, propertyName, propertyAssignment)
86 throw BluePrintException(format("property({}) of expression ({}) is not supported",
87 propertyName, propertyAssignment))
92 open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, propertyAssignment: JsonNode) {
93 val propertyType = propertyDefinition.type
96 if (BluePrintTypes.validPrimitiveTypes().contains(propertyType)) {
97 isValid = JacksonUtils.checkJsonNodeValueOfPrimitiveType(propertyType, propertyAssignment)
99 } else if (BluePrintTypes.validCollectionTypes().contains(propertyType)) {
101 isValid = JacksonUtils.checkJsonNodeValueOfCollectionType(propertyType, propertyAssignment)
103 bluePrintRepoService.getDataType(propertyType)
104 ?: throw BluePrintException(format("property({}) defined of data type({}) is not in repository",
105 propertyName, propertyType))
110 throw BluePrintException(format("property({}) defined of type({}) is not compatable with the value ({})",
111 propertyName, propertyType, propertyAssignment))