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.fasterxml.jackson.databind.JsonNode
21 import com.google.common.base.Preconditions
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
26 import org.onap.ccsdk.apps.controllerblueprints.core.format
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
30 import org.slf4j.LoggerFactory
31 import java.io.Serializable
33 interface ResourceDictionaryValidationService : Serializable {
35 @Throws(BluePrintException::class)
36 fun validate(resourceDefinition: ResourceDefinition)
40 open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: BluePrintRepoService) : ResourceDictionaryValidationService {
42 private val log = LoggerFactory.getLogger(ResourceDictionaryDefaultValidationService::class.java)
44 override fun validate(resourceDefinition: ResourceDefinition) {
45 Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
47 resourceDefinition.sources.forEach { (name, nodeTemplate) ->
48 val sourceType = nodeTemplate.type
50 val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)?.block()
51 ?: throw BluePrintException(format("Failed to get node type definition for source({})", sourceType))
53 // Validate Property Name, expression, values and Data Type
54 validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
59 open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
60 nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
64 open fun validatePropertyAssignments(nodeTypeProperties: MutableMap<String, PropertyDefinition>,
65 properties: MutableMap<String, JsonNode>) {
66 properties.forEach { propertyName, propertyAssignment ->
67 val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName]
68 ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName))
69 // Check and Validate if Expression Node
70 val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment)
71 if (!expressionData.isExpression) {
72 checkPropertyValue(propertyDefinition, propertyName, propertyAssignment)
77 open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, jsonNode: JsonNode) {
78 //log.info("validating Property {}, name ({}) value ({})", propertyDefinition, propertyName, jsonNode)