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