Updating README.md
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-validation / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / validation / BluePrintTypeValidatorServiceImpl.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.validation
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
21 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactDefinitionValidator
22 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintArtifactTypeValidator
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintAttributeDefinitionValidator
24 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintDataTypeValidator
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintNodeTemplateValidator
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintNodeTypeValidator
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintPropertyDefinitionValidator
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintServiceTemplateValidator
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTopologyTemplateValidator
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeValidatorService
31 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintValidator
32 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowValidator
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.context.ApplicationContext
35 import org.springframework.stereotype.Service
36
37 @Service
38 class BluePrintTypeValidatorServiceImpl : BluePrintTypeValidatorService {
39
40     companion object {
41         const val PREFIX_DEFAULT = "default"
42     }
43
44     @Autowired
45     private lateinit var context: ApplicationContext
46
47     override fun <T : BluePrintValidator<*>> bluePrintValidator(referenceName: String, classType: Class<T>): T? {
48         return if (context.containsBean(referenceName)) {
49             context.getBean(referenceName, classType)
50         } else {
51             null
52         }
53     }
54
55     override fun <T : BluePrintValidator<*>> bluePrintValidators(referenceNamePrefix: String, classType: Class<T>): List<T>? {
56         return context.getBeansOfType(classType)
57             .filter { it.key.startsWith(referenceNamePrefix) }
58             .mapNotNull { it.value }
59     }
60
61     override fun <T : BluePrintValidator<*>> bluePrintValidators(classType: Class<T>): List<T>? {
62         return context.getBeansOfType(classType).mapNotNull { it.value }
63     }
64
65     override fun getServiceTemplateValidators(): List<BluePrintServiceTemplateValidator> {
66         return bluePrintValidators(PREFIX_DEFAULT, BluePrintServiceTemplateValidator::class.java)
67             ?: throw BluePrintProcessorException("failed to get default ServiceTemplate validators")
68     }
69
70     override fun getDataTypeValidators(): List<BluePrintDataTypeValidator> {
71         return bluePrintValidators(PREFIX_DEFAULT, BluePrintDataTypeValidator::class.java)
72             ?: throw BluePrintProcessorException("failed to get default DataType validators")
73     }
74
75     override fun getArtifactTypeValidators(): List<BluePrintArtifactTypeValidator> {
76         return bluePrintValidators(PREFIX_DEFAULT, BluePrintArtifactTypeValidator::class.java)
77             ?: throw BluePrintProcessorException("failed to get default ArtifactType validators")
78     }
79
80     override fun getArtifactDefinitionsValidators(): List<BluePrintArtifactDefinitionValidator> {
81         return bluePrintValidators(PREFIX_DEFAULT, BluePrintArtifactDefinitionValidator::class.java)
82             ?: throw BluePrintProcessorException("failed to get default ArtifactDefinition validators")
83     }
84
85     override fun getNodeTypeValidators(): List<BluePrintNodeTypeValidator> {
86         return bluePrintValidators(PREFIX_DEFAULT, BluePrintNodeTypeValidator::class.java)
87             ?: throw BluePrintProcessorException("failed to get default NodeType validators")
88     }
89
90     override fun getTopologyTemplateValidators(): List<BluePrintTopologyTemplateValidator> {
91         return bluePrintValidators(PREFIX_DEFAULT, BluePrintTopologyTemplateValidator::class.java)
92             ?: throw BluePrintProcessorException("failed to get default TopologyTemplate validators")
93     }
94
95     override fun getNodeTemplateValidators(): List<BluePrintNodeTemplateValidator> {
96         return bluePrintValidators(PREFIX_DEFAULT, BluePrintNodeTemplateValidator::class.java)
97             ?: throw BluePrintProcessorException("failed to get default NodeTemplate validators")
98     }
99
100     override fun getWorkflowValidators(): List<BluePrintWorkflowValidator> {
101         return bluePrintValidators(PREFIX_DEFAULT, BluePrintWorkflowValidator::class.java)
102             ?: throw BluePrintProcessorException("failed to get default Workflow validators")
103     }
104
105     override fun getPropertyDefinitionValidators(): List<BluePrintPropertyDefinitionValidator> {
106         return bluePrintValidators(PREFIX_DEFAULT, BluePrintPropertyDefinitionValidator::class.java)
107             ?: throw BluePrintProcessorException("failed to get default PropertyDefinition validators")
108     }
109
110     override fun getAttributeDefinitionValidators(): List<BluePrintAttributeDefinitionValidator> {
111         return bluePrintValidators(PREFIX_DEFAULT, BluePrintAttributeDefinitionValidator::class.java)
112             ?: throw BluePrintProcessorException("failed to get default AttributeDefinition validators")
113     }
114 }