Blueprint Processor Python Script Components
[ccsdk/cds.git] / components / resource-dict / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / resource / dict / service / ResourceDefinitionValidationService.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2017-2018 AT&T Intellectual Property.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service
19
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import com.fasterxml.jackson.databind.JsonNode
23 import com.google.common.base.Preconditions
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
28 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
29 import org.onap.ccsdk.apps.controllerblueprints.core.format
30 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService
32 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
33 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
34 import java.io.Serializable
35
36 /**
37  * ResourceDefinitionValidationService.
38  *
39  * @author Brinda Santh
40  */
41 interface ResourceDefinitionValidationService : Serializable {
42
43     @Throws(BluePrintException::class)
44     fun validate(resourceDefinition: ResourceDefinition)
45
46 }
47
48 /**
49  * ResourceDefinitionValidationService.
50  *
51  * @author Brinda Santh
52  */
53 open class ResourceDefinitionValidationServiceImpl(private val bluePrintRepoService: BluePrintRepoService) : ResourceDefinitionValidationService {
54
55     private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionValidationService::class.java)
56
57     override fun validate(resourceDefinition: ResourceDefinition) {
58         Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
59         log.trace("Validating Resource Dictionary Definition {}", resourceDefinition.name)
60
61         resourceDefinition.sources.forEach { name, nodeTemplate ->
62             val sourceType = nodeTemplate.type
63
64             val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)
65
66             // Validate Property Name, expression, values and Data Type
67             validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
68         }
69     }
70
71
72     open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
73         nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
74     }
75
76
77     open fun validatePropertyAssignments(nodeTypeProperties: MutableMap<String, PropertyDefinition>,
78                                          properties: MutableMap<String, JsonNode>) {
79         properties.forEach { propertyName, propertyAssignment ->
80             val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName]
81                     ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName))
82             // Check and Validate if Expression Node
83             val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment)
84             if (!expressionData.isExpression) {
85                 checkPropertyValue(propertyDefinition, propertyName, propertyAssignment)
86             } else {
87                 throw BluePrintException(format("property({}) of expression ({}) is not supported",
88                         propertyName, propertyAssignment))
89             }
90         }
91     }
92
93     open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, propertyAssignment: JsonNode) {
94         val propertyType = propertyDefinition.type
95         val isValid: Boolean
96
97         if (BluePrintTypes.validPrimitiveTypes().contains(propertyType)) {
98             isValid = JacksonUtils.checkJsonNodeValueOfPrimitiveType(propertyType, propertyAssignment)
99
100         } else if (BluePrintTypes.validCollectionTypes().contains(propertyType)) {
101
102             isValid = JacksonUtils.checkJsonNodeValueOfCollectionType(propertyType, propertyAssignment)
103         } else {
104             bluePrintRepoService.getDataType(propertyType)
105             isValid = true
106         }
107
108         check(isValid) {
109             throw BluePrintException(format("property({}) defined of type({}) is not compatable with the value ({})",
110                     propertyName, propertyType, propertyAssignment))
111         }
112     }
113 }