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