2 * Copyright © 2018 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.google.common.base.Preconditions
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
25 import org.onap.ccsdk.apps.controllerblueprints.core.format
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
29 import org.slf4j.LoggerFactory
30 import java.io.Serializable
32 interface ResourceDictionaryValidationService : Serializable {
34 @Throws(BluePrintException::class)
35 fun validate(resourceDefinition: ResourceDefinition)
39 open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: BluePrintRepoService) : ResourceDictionaryValidationService {
41 private val log = LoggerFactory.getLogger(ResourceDictionaryDefaultValidationService::class.java)
43 override fun validate(resourceDefinition: ResourceDefinition) {
44 Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
46 resourceDefinition.sources.forEach { (name, nodeTemplate) ->
47 val sourceType = nodeTemplate.type
49 val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)
50 ?: throw BluePrintException(format("Failed to get node type definition for source({})", sourceType))
52 // Validate Property Name, expression, values and Data Type
53 validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
58 open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
59 nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
63 open fun validatePropertyAssignments(nodeTypeProperties: MutableMap<String, PropertyDefinition>,
64 properties: MutableMap<String, JsonNode>) {
65 properties.forEach { propertyName, propertyAssignment ->
66 val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName]
67 ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName))
68 // Check and Validate if Expression Node
69 val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment)
70 if (!expressionData.isExpression) {
71 checkPropertyValue(propertyDefinition, propertyName, propertyAssignment)
76 open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, jsonNode: JsonNode) {
77 //log.info("validating Property {}, name ({}) value ({})", propertyDefinition, propertyName, jsonNode)