Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / interfaces / BlueprintEnhancer.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 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.AttributeDefinition
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.PolicyType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipTemplate
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.TopologyTemplate
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintRuntimeService
34
35 interface BlueprintEnhancer<T> {
36
37     fun enhance(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, type: T)
38 }
39
40 interface BlueprintServiceTemplateEnhancer : BlueprintEnhancer<ServiceTemplate>
41
42 interface BlueprintTopologyTemplateEnhancer : BlueprintEnhancer<TopologyTemplate>
43
44 interface BlueprintWorkflowEnhancer : BlueprintEnhancer<Workflow>
45
46 interface BlueprintNodeTemplateEnhancer : BlueprintEnhancer<NodeTemplate>
47
48 interface BlueprintNodeTypeEnhancer : BlueprintEnhancer<NodeType>
49
50 interface BlueprintRelationshipTemplateEnhancer : BlueprintEnhancer<RelationshipTemplate>
51
52 interface BlueprintRelationshipTypeEnhancer : BlueprintEnhancer<RelationshipType>
53
54 interface BlueprintArtifactDefinitionEnhancer : BlueprintEnhancer<ArtifactDefinition>
55
56 interface BlueprintPolicyTypeEnhancer : BlueprintEnhancer<PolicyType>
57
58 interface BlueprintPropertyDefinitionEnhancer : BlueprintEnhancer<PropertyDefinition>
59
60 interface BlueprintAttributeDefinitionEnhancer : BlueprintEnhancer<AttributeDefinition>
61
62 interface BlueprintEnhancerService {
63
64     @Throws(BlueprintException::class)
65     suspend fun enhance(basePath: String, enrichedBasePath: String): BlueprintContext
66
67     @Throws(BlueprintException::class)
68     suspend fun enhance(basePath: String): BlueprintContext
69 }
70
71 interface BlueprintTypeEnhancerService {
72
73     fun getServiceTemplateEnhancers(): List<BlueprintServiceTemplateEnhancer>
74
75     fun getTopologyTemplateEnhancers(): List<BlueprintTopologyTemplateEnhancer>
76
77     fun getWorkflowEnhancers(): List<BlueprintWorkflowEnhancer>
78
79     fun getNodeTemplateEnhancers(): List<BlueprintNodeTemplateEnhancer>
80
81     fun getNodeTypeEnhancers(): List<BlueprintNodeTypeEnhancer>
82
83     fun getRelationshipTemplateEnhancers(): List<BlueprintRelationshipTemplateEnhancer>
84
85     fun getRelationshipTypeEnhancers(): List<BlueprintRelationshipTypeEnhancer>
86
87     fun getArtifactDefinitionEnhancers(): List<BlueprintArtifactDefinitionEnhancer>
88
89     fun getPolicyTypeEnhancers(): List<BlueprintPolicyTypeEnhancer>
90
91     fun getPropertyDefinitionEnhancers(): List<BlueprintPropertyDefinitionEnhancer>
92
93     fun getAttributeDefinitionEnhancers(): List<BlueprintAttributeDefinitionEnhancer>
94
95     fun enhanceServiceTemplate(
96         bluePrintRuntimeService: BlueprintRuntimeService<*>,
97         name: String,
98         serviceTemplate: ServiceTemplate
99     ) {
100         val enhancers = getServiceTemplateEnhancers()
101         doEnhancement(bluePrintRuntimeService, name, serviceTemplate, enhancers)
102     }
103
104     fun enhanceTopologyTemplate(
105         bluePrintRuntimeService: BlueprintRuntimeService<*>,
106         name: String,
107         topologyTemplate: TopologyTemplate
108     ) {
109         val enhancers = getTopologyTemplateEnhancers()
110         doEnhancement(bluePrintRuntimeService, name, topologyTemplate, enhancers)
111     }
112
113     fun enhanceWorkflow(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, workflow: Workflow) {
114         val enhancers = getWorkflowEnhancers()
115         doEnhancement(bluePrintRuntimeService, name, workflow, enhancers)
116     }
117
118     fun enhanceNodeTemplate(
119         bluePrintRuntimeService: BlueprintRuntimeService<*>,
120         name: String,
121         nodeTemplate: NodeTemplate
122     ) {
123         val enhancers = getNodeTemplateEnhancers()
124         doEnhancement(bluePrintRuntimeService, name, nodeTemplate, enhancers)
125     }
126
127     fun enhanceNodeType(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, nodeType: NodeType) {
128         val enhancers = getNodeTypeEnhancers()
129         doEnhancement(bluePrintRuntimeService, name, nodeType, enhancers)
130     }
131
132     fun enhanceRelationshipTemplate(
133         bluePrintRuntimeService: BlueprintRuntimeService<*>,
134         name: String,
135         relationshipTemplate: RelationshipTemplate
136     ) {
137         val enhancers = getRelationshipTemplateEnhancers()
138         doEnhancement(bluePrintRuntimeService, name, relationshipTemplate, enhancers)
139     }
140
141     fun enhanceRelationshipType(
142         bluePrintRuntimeService: BlueprintRuntimeService<*>,
143         name: String,
144         relationshipType: RelationshipType
145     ) {
146         val enhancers = getRelationshipTypeEnhancers()
147         doEnhancement(bluePrintRuntimeService, name, relationshipType, enhancers)
148     }
149
150     fun enhanceArtifactDefinition(
151         bluePrintRuntimeService: BlueprintRuntimeService<*>,
152         name: String,
153         artifactDefinition: ArtifactDefinition
154     ) {
155         val enhancers = getArtifactDefinitionEnhancers()
156         doEnhancement(bluePrintRuntimeService, name, artifactDefinition, enhancers)
157     }
158
159     fun enhancePolicyType(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, policyType: PolicyType) {
160         val enhancers = getPolicyTypeEnhancers()
161         doEnhancement(bluePrintRuntimeService, name, policyType, enhancers)
162     }
163
164     fun enhancePropertyDefinitions(
165         bluePrintRuntimeService: BlueprintRuntimeService<*>,
166         properties: MutableMap<String, PropertyDefinition>
167     ) {
168         val errorMap = linkedMapOf<String, BlueprintException>()
169         properties.forEach { propertyName, propertyDefinition ->
170             try {
171                 enhancePropertyDefinition(bluePrintRuntimeService, propertyName, propertyDefinition)
172             } catch (e: BlueprintException) {
173                 errorMap[propertyName] = e
174             }
175         }
176         if (errorMap.isNotEmpty()) {
177             val nestedErrors = errorMap.keys.map { "[ property: ${errorMap[it]?.message} ]" }.joinToString(";")
178             throw BlueprintException("Failed to enhance properties $nestedErrors")
179         }
180     }
181
182     fun enhancePropertyDefinition(
183         bluePrintRuntimeService: BlueprintRuntimeService<*>,
184         name: String,
185         propertyDefinition: PropertyDefinition
186     ) {
187         val enhancers = getPropertyDefinitionEnhancers()
188         doEnhancement(bluePrintRuntimeService, name, propertyDefinition, enhancers)
189     }
190
191     fun enhanceAttributeDefinitions(
192         bluePrintRuntimeService: BlueprintRuntimeService<*>,
193         attributes: MutableMap<String, AttributeDefinition>
194     ) {
195         val errorMap = linkedMapOf<String, BlueprintException>()
196         attributes.forEach { attributeName, attributeDefinition ->
197             try {
198                 enhanceAttributeDefinition(bluePrintRuntimeService, attributeName, attributeDefinition)
199             } catch (e: BlueprintException) {
200                 errorMap[attributeName] = e
201             }
202         }
203         if (errorMap.isNotEmpty()) {
204             val nestedErrors = errorMap.keys.map { "[ attribute: ${errorMap[it]?.message} ]" }.joinToString(";")
205             throw BlueprintException("Failed to enhance attributes $nestedErrors")
206         }
207     }
208
209     fun enhanceAttributeDefinition(
210         bluePrintRuntimeService: BlueprintRuntimeService<*>,
211         name: String,
212         attributeDefinition: AttributeDefinition
213     ) {
214         val enhancers = getAttributeDefinitionEnhancers()
215         doEnhancement(bluePrintRuntimeService, name, attributeDefinition, enhancers)
216     }
217
218     @Suppress("UNCHECKED_CAST")
219     private fun <T> doEnhancement(
220         bluePrintRuntimeService: BlueprintRuntimeService<*>,
221         name: String,
222         definition: Any,
223         enhancers: List<BlueprintEnhancer<T>>
224     ) {
225         if (enhancers.isNotEmpty()) {
226             val errorMap = linkedMapOf<String, BlueprintException>()
227             enhancers.forEach {
228                 try {
229                     it.enhance(bluePrintRuntimeService, name, definition as T)
230                 } catch (e: BlueprintException) {
231                     errorMap[name] = e
232                 }
233             }
234             if (errorMap.isNotEmpty()) {
235                 val nestedErrors = errorMap.keys.map {
236                     "${errorMap[it]?.message ?: errorMap[it].toString()}"
237                 }.joinToString(";")
238                 throw BlueprintException("$name-->$nestedErrors")
239             }
240         }
241     }
242 }