cef1f15809896c747e1adcf7c4bfe05f8ab0a915
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2018 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service
18
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
31
32 interface ResourceDictionaryValidationService : Serializable {
33
34     @Throws(BluePrintException::class)
35     fun validate(resourceDefinition: ResourceDefinition)
36
37 }
38
39 open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: BluePrintRepoService) : ResourceDictionaryValidationService {
40
41     private val log = LoggerFactory.getLogger(ResourceDictionaryDefaultValidationService::class.java)
42
43     override fun validate(resourceDefinition: ResourceDefinition) {
44         Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
45
46         resourceDefinition.sources.forEach { (name, nodeTemplate) ->
47             val sourceType = nodeTemplate.type
48
49             val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)
50                     ?: throw BluePrintException(format("Failed to get node type definition for source({})", sourceType))
51
52             // Validate Property Name, expression, values and Data Type
53             validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
54         }
55     }
56
57
58     open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
59         nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
60     }
61
62
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)
72             }
73         }
74     }
75
76     open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, jsonNode: JsonNode) {
77         //log.info("validating Property {}, name ({}) value ({})", propertyDefinition, propertyName, jsonNode)
78         //TODO
79     }
80 }