Merge "Controller Blueprints Microservice"
[ccsdk/apps.git] / ms / controllerblueprints / modules / resource-dict / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / resource / dict / service / ResourceDictionaryValidationService.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.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.BluePrintTypes
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.apps.controllerblueprints.core.format
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService
29 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService
30 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
32 import org.slf4j.LoggerFactory
33 import java.io.Serializable
34 /**
35  * ResourceDictionaryValidationService.
36  *
37  * @author Brinda Santh
38  */
39 interface ResourceDictionaryValidationService : Serializable {
40
41     @Throws(BluePrintException::class)
42     fun validate(resourceDefinition: ResourceDefinition)
43
44 }
45 /**
46  * ResourceDictionaryDefaultValidationService.
47  *
48  * @author Brinda Santh
49  */
50 open class ResourceDictionaryDefaultValidationService(private val bluePrintRepoService: BluePrintRepoService) : ResourceDictionaryValidationService {
51
52     private val log = LoggerFactory.getLogger(ResourceDictionaryDefaultValidationService::class.java)
53
54     override fun validate(resourceDefinition: ResourceDefinition) {
55         Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition")
56         log.trace("Validating Resource Dictionary Definition {}", resourceDefinition.name)
57
58         resourceDefinition.sources.forEach { (name, nodeTemplate) ->
59             val sourceType = nodeTemplate.type
60
61             val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)?.block()
62                     ?: throw BluePrintException(format("Failed to get source({}) node type definition({})", name, sourceType))
63
64             // Validate Property Name, expression, values and Data Type
65             validateNodeTemplateProperties(nodeTemplate, sourceNodeType)
66         }
67     }
68
69
70     open fun validateNodeTemplateProperties(nodeTemplate: NodeTemplate, nodeType: NodeType) {
71         nodeTemplate.properties?.let { validatePropertyAssignments(nodeType.properties!!, nodeTemplate.properties!!) }
72     }
73
74
75     open fun validatePropertyAssignments(nodeTypeProperties: MutableMap<String, PropertyDefinition>,
76                                          properties: MutableMap<String, JsonNode>) {
77         properties.forEach { propertyName, propertyAssignment ->
78             val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName]
79                     ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName))
80             // Check and Validate if Expression Node
81             val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment)
82             if (!expressionData.isExpression) {
83                 checkPropertyValue(propertyDefinition, propertyName, propertyAssignment)
84             } else {
85                 throw BluePrintException(format("property({}) of expression ({}) is not supported",
86                         propertyName, propertyAssignment))
87             }
88         }
89     }
90
91     open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, propertyAssignment: JsonNode) {
92         val propertyType = propertyDefinition.type
93         var isValid = false
94
95         if (BluePrintTypes.validPrimitiveTypes().contains(propertyType)) {
96             isValid = JacksonUtils.checkJsonNodeValueOfPrimitiveType(propertyType, propertyAssignment)
97
98         } else if (BluePrintTypes.validCollectionTypes().contains(propertyType)) {
99
100             isValid = JacksonUtils.checkJsonNodeValueOfCollectionType(propertyType, propertyAssignment)
101         } else {
102             bluePrintRepoService.getDataType(propertyType)
103                     ?: throw BluePrintException(format("property({}) defined of data type({}) is not in repository",
104                             propertyName, propertyType))
105
106             isValid = true;
107         }
108
109         check(isValid) {
110             throw BluePrintException(format("property({}) defined of type({}) is not compatable with the value ({})",
111                     propertyName, propertyType, propertyAssignment))
112         }
113     }
114 }