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