Migrate "ms/controllerblueprints" from ccsdk/apps
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / interfaces / BlueprintValidator.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
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.cds.controllerblueprints.core.interfaces
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
23
24
25 interface BluePrintValidator<T> {
26
27     fun validate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, type: T)
28
29 }
30
31
32 interface BluePrintServiceTemplateValidator : BluePrintValidator<ServiceTemplate>
33
34 interface BluePrintTopologyTemplateValidator : BluePrintValidator<TopologyTemplate>
35
36 interface BluePrintArtifactTypeValidator : BluePrintValidator<ArtifactType>
37
38 interface BluePrintArtifactDefinitionValidator : BluePrintValidator<ArtifactDefinition>
39
40 interface BluePrintDataTypeValidator : BluePrintValidator<DataType>
41
42 interface BluePrintNodeTypeValidator : BluePrintValidator<NodeType>
43
44 interface BluePrintNodeTemplateValidator : BluePrintValidator<NodeTemplate>
45
46 interface BluePrintWorkflowValidator : BluePrintValidator<Workflow>
47
48 interface BluePrintPropertyDefinitionValidator : BluePrintValidator<PropertyDefinition>
49
50 interface BluePrintAttributeDefinitionValidator : BluePrintValidator<AttributeDefinition>
51
52 /**
53  * Blueprint Validation Interface.
54  */
55 interface BluePrintValidatorService {
56
57     @Throws(BluePrintException::class)
58     fun validateBluePrints(basePath: String): Boolean
59
60     @Throws(BluePrintException::class)
61     fun validateBluePrints(bluePrintRuntimeService: BluePrintRuntimeService<*>): Boolean
62 }
63
64
65 interface BluePrintTypeValidatorService {
66
67     fun <T : BluePrintValidator<*>> bluePrintValidator(referenceName: String, classType: Class<T>): T?
68
69     fun <T : BluePrintValidator<*>> bluePrintValidators(referenceNamePrefix: String, classType: Class<T>): List<T>?
70
71     fun <T : BluePrintValidator<*>> bluePrintValidators(classType: Class<T>): List<T>?
72
73     fun getServiceTemplateValidators(): List<BluePrintServiceTemplateValidator>
74
75     fun getDataTypeValidators(): List<BluePrintDataTypeValidator>
76
77     fun getArtifactTypeValidators(): List<BluePrintArtifactTypeValidator>
78
79     fun getArtifactDefinitionsValidators(): List<BluePrintArtifactDefinitionValidator>
80
81     fun getNodeTypeValidators(): List<BluePrintNodeTypeValidator>
82
83     fun getTopologyTemplateValidators(): List<BluePrintTopologyTemplateValidator>
84
85     fun getNodeTemplateValidators(): List<BluePrintNodeTemplateValidator>
86
87     fun getWorkflowValidators(): List<BluePrintWorkflowValidator>
88
89     fun getPropertyDefinitionValidators(): List<BluePrintPropertyDefinitionValidator>
90
91     fun getAttributeDefinitionValidators(): List<BluePrintAttributeDefinitionValidator>
92
93     fun validateServiceTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, serviceTemplate: ServiceTemplate) {
94         val validators = getServiceTemplateValidators()
95         doValidation(bluePrintRuntimeService, name, serviceTemplate, validators)
96     }
97
98     fun validateArtifactType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, artifactType: ArtifactType) {
99         val validators = getArtifactTypeValidators()
100         doValidation(bluePrintRuntimeService, name, artifactType, validators)
101     }
102
103     fun validateArtifactDefinition(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String,
104                                    artifactDefinition: ArtifactDefinition) {
105         val validators = getArtifactDefinitionsValidators()
106         doValidation(bluePrintRuntimeService, name, artifactDefinition, validators)
107     }
108
109     fun validateDataType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, dataType: DataType) {
110         val validators = getDataTypeValidators()
111         doValidation(bluePrintRuntimeService, name, dataType, validators)
112     }
113
114     fun validateNodeType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeType: NodeType) {
115         val validators = getNodeTypeValidators()
116         doValidation(bluePrintRuntimeService, name, nodeType, validators)
117     }
118
119     fun validateTopologyTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, topologyTemplate: TopologyTemplate) {
120         val validators = getTopologyTemplateValidators()
121         doValidation(bluePrintRuntimeService, name, topologyTemplate, validators)
122     }
123
124     fun validateNodeTemplate(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeTemplate: NodeTemplate) {
125         val validators = getNodeTemplateValidators()
126         doValidation(bluePrintRuntimeService, name, nodeTemplate, validators)
127     }
128
129     fun validateWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
130         val validators = getWorkflowValidators()
131         doValidation(bluePrintRuntimeService, name, workflow, validators)
132     }
133
134     fun validatePropertyDefinitions(bluePrintRuntimeService: BluePrintRuntimeService<*>, properties: MutableMap<String, PropertyDefinition>) {
135         properties.forEach { propertyName, propertyDefinition ->
136             validatePropertyDefinition(bluePrintRuntimeService, propertyName, propertyDefinition)
137         }
138     }
139
140     fun validatePropertyDefinition(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, propertyDefinition: PropertyDefinition) {
141         val validators = getPropertyDefinitionValidators()
142         doValidation(bluePrintRuntimeService, name, propertyDefinition, validators)
143     }
144
145     fun validateAttributeDefinitions(bluePrintRuntimeService: BluePrintRuntimeService<*>, attributes: MutableMap<String, AttributeDefinition>) {
146         attributes.forEach { attributeName, attributeDefinition ->
147             validateAttributeDefinition(bluePrintRuntimeService, attributeName, attributeDefinition)
148         }
149     }
150
151     fun validateAttributeDefinition(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, attributeDefinition: AttributeDefinition) {
152         val validators = getAttributeDefinitionValidators()
153         doValidation(bluePrintRuntimeService, name, attributeDefinition, validators)
154     }
155
156     @Suppress("UNCHECKED_CAST")
157     private fun <T> doValidation(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, definition: Any, validators: List<BluePrintValidator<T>>) {
158         validators.forEach {
159             it.validate(bluePrintRuntimeService, name, definition as T)
160         }
161     }
162 }
163
164
165