Resource Resolution Service
[ccsdk/cds.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / validation / BluePrintNodeTypeValidatorImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.core.validation
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.*
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintNodeTypeValidator
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
29
30
31 open class BluePrintNodeTypeValidatorImpl(private val bluePrintTypeValidatorService: BluePrintTypeValidatorService) : BluePrintNodeTypeValidator {
32
33     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintServiceTemplateValidatorImpl::class.toString())
34
35     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
36     lateinit var bluePrintContext: BluePrintContext
37     var paths: MutableList<String> = arrayListOf()
38
39     override fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTypeName: String, nodeType: NodeType) {
40         log.trace("Validating NodeType($nodeTypeName)")
41         this.bluePrintRuntimeService = bluePrintRuntimeService
42         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
43
44         paths.add(nodeTypeName)
45
46         val derivedFrom: String = nodeType.derivedFrom
47         //Check Derived From
48         checkValidNodeTypesDerivedFrom(nodeTypeName, derivedFrom)
49
50         if (!BluePrintTypes.rootNodeTypes().contains(derivedFrom)) {
51             bluePrintContext.serviceTemplate.nodeTypes?.get(derivedFrom)
52                     ?: throw BluePrintException("Failed to get derivedFrom NodeType($derivedFrom)'s for NodeType($nodeTypeName)")
53         }
54
55         nodeType.properties?.let { bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, nodeType.properties!!) }
56         nodeType.capabilities?.let { validateCapabilityDefinitions(nodeTypeName, nodeType) }
57         nodeType.requirements?.let { validateRequirementDefinitions(nodeTypeName, nodeType) }
58         nodeType.interfaces?.let { validateInterfaceDefinitions(nodeType.interfaces!!) }
59
60         paths.removeAt(paths.lastIndex)
61     }
62
63     fun checkValidNodeTypesDerivedFrom(nodeTypeName: String, derivedFrom: String) {
64         check(BluePrintTypes.validNodeTypeDerivedFroms.contains(derivedFrom)) {
65             throw BluePrintException("Failed to get node type ($nodeTypeName)'s  derivedFrom($derivedFrom) definition ")
66         }
67     }
68
69     open fun validateCapabilityDefinitions(nodeTypeName: String, nodeType: NodeType) {
70         val capabilities = nodeType.capabilities
71         paths.add("capabilities")
72         capabilities?.forEach { capabilityName, capabilityDefinition ->
73             paths.add(capabilityName)
74
75             validateCapabilityDefinition(nodeTypeName, nodeType, capabilityName, capabilityDefinition)
76
77             paths.removeAt(paths.lastIndex)
78         }
79         paths.removeAt(paths.lastIndex)
80     }
81
82     open fun validateCapabilityDefinition(nodeTypeName: String, nodeType: NodeType, capabilityName: String,
83                                           capabilityDefinition: CapabilityDefinition) {
84         val capabilityType = capabilityDefinition.type
85         check(BluePrintTypes.validCapabilityTypes.contains(capabilityType)) {
86             throw BluePrintException("failed to get CapabilityType($capabilityType) for NodeType($nodeTypeName)")
87         }
88     }
89
90     open fun validateRequirementDefinitions(nodeName: String, nodeType: NodeType) {
91         paths.add("requirements")
92         val requirements = nodeType.requirements
93
94         requirements?.forEach { requirementDefinitionName, requirementDefinition ->
95             paths.add(requirementDefinitionName)
96             validateRequirementDefinition(nodeName, nodeType, requirementDefinitionName, requirementDefinition)
97             paths.removeAt(paths.lastIndex)
98         }
99         paths.removeAt(paths.lastIndex)
100     }
101
102     open fun validateRequirementDefinition(nodeTypeName: String, nodeType: NodeType, requirementDefinitionName: String,
103                                            requirementDefinition: RequirementDefinition) {
104
105         log.info("validating NodeType({}) RequirementDefinition ({}) ", nodeTypeName, requirementDefinitionName)
106         val requirementNodeTypeName = requirementDefinition.node!!
107         val capabilityName = requirementDefinition.capability
108         val relationship = requirementDefinition.relationship!!
109
110         check(BluePrintTypes.validRelationShipDerivedFroms.contains(relationship)) {
111             throw BluePrintException("failed to get relationship($relationship) for NodeType($nodeTypeName)'s requirement($requirementDefinitionName)")
112         }
113
114         val relationShipNodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(requirementNodeTypeName)
115                 ?: throw BluePrintException("failed to get requirement NodeType($requirementNodeTypeName)'s for requirement($requirementDefinitionName) ")
116
117         relationShipNodeType.capabilities?.get(capabilityName)
118                 ?: throw BluePrintException("failed to get requirement NodeType($requirementNodeTypeName)'s " +
119                         "capability($nodeTypeName) for NodeType ($capabilityName)'s requirement($requirementDefinitionName) ")
120
121     }
122
123     open fun validateInterfaceDefinitions(interfaces: MutableMap<String, InterfaceDefinition>) {
124         paths.add("interfaces")
125         interfaces.forEach { interfaceName, interfaceDefinition ->
126             paths.add(interfaceName)
127             interfaceDefinition.operations?.let { validateOperationDefinitions(interfaceDefinition.operations!!) }
128             paths.removeAt(paths.lastIndex)
129         }
130         paths.removeAt(paths.lastIndex)
131     }
132
133     open fun validateOperationDefinitions(operations: MutableMap<String, OperationDefinition>) {
134         paths.add("operations")
135         operations.forEach { opertaionName, operationDefinition ->
136             paths.add(opertaionName)
137             operationDefinition.implementation?.let { validateImplementation(operationDefinition.implementation!!) }
138
139             operationDefinition.inputs?.let {
140                 bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, operationDefinition.inputs!!)
141             }
142
143             operationDefinition.outputs?.let {
144                 bluePrintTypeValidatorService.validatePropertyDefinitions(bluePrintRuntimeService, operationDefinition.outputs!!)
145             }
146             paths.removeAt(paths.lastIndex)
147         }
148         paths.removeAt(paths.lastIndex)
149     }
150
151     open fun validateImplementation(implementation: Implementation) {
152         checkNotEmptyOrThrow(implementation.primary)
153     }
154
155 }