b657199c7d63c77248c61b9144732b4b0aacfb81
[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     fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, type: T)
37 }
38
39 interface BluePrintServiceTemplateEnhancer : BluePrintEnhancer<ServiceTemplate>
40
41 interface BluePrintTopologyTemplateEnhancer : BluePrintEnhancer<TopologyTemplate>
42
43 interface BluePrintWorkflowEnhancer : BluePrintEnhancer<Workflow>
44
45 interface BluePrintNodeTemplateEnhancer : BluePrintEnhancer<NodeTemplate>
46
47 interface BluePrintNodeTypeEnhancer : BluePrintEnhancer<NodeType>
48
49 interface BluePrintRelationshipTemplateEnhancer : BluePrintEnhancer<RelationshipTemplate>
50
51 interface BluePrintRelationshipTypeEnhancer : BluePrintEnhancer<RelationshipType>
52
53 interface BluePrintArtifactDefinitionEnhancer : BluePrintEnhancer<ArtifactDefinition>
54
55 interface BluePrintPolicyTypeEnhancer : BluePrintEnhancer<PolicyType>
56
57 interface BluePrintPropertyDefinitionEnhancer : BluePrintEnhancer<PropertyDefinition>
58
59 interface BluePrintAttributeDefinitionEnhancer : BluePrintEnhancer<AttributeDefinition>
60
61 interface BluePrintEnhancerService {
62
63     @Throws(BluePrintException::class)
64     suspend fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext
65
66     @Throws(BluePrintException::class)
67     suspend fun enhance(basePath: String): BluePrintContext
68 }
69
70 interface BluePrintTypeEnhancerService {
71
72     fun getServiceTemplateEnhancers(): List<BluePrintServiceTemplateEnhancer>
73
74     fun getTopologyTemplateEnhancers(): List<BluePrintTopologyTemplateEnhancer>
75
76     fun getWorkflowEnhancers(): List<BluePrintWorkflowEnhancer>
77
78     fun getNodeTemplateEnhancers(): List<BluePrintNodeTemplateEnhancer>
79
80     fun getNodeTypeEnhancers(): List<BluePrintNodeTypeEnhancer>
81
82     fun getRelationshipTemplateEnhancers(): List<BluePrintRelationshipTemplateEnhancer>
83
84     fun getRelationshipTypeEnhancers(): List<BluePrintRelationshipTypeEnhancer>
85
86     fun getArtifactDefinitionEnhancers(): List<BluePrintArtifactDefinitionEnhancer>
87
88     fun getPolicyTypeEnhancers(): List<BluePrintPolicyTypeEnhancer>
89
90     fun getPropertyDefinitionEnhancers(): List<BluePrintPropertyDefinitionEnhancer>
91
92     fun getAttributeDefinitionEnhancers(): List<BluePrintAttributeDefinitionEnhancer>
93
94     fun enhanceServiceTemplate(
95         bluePrintRuntimeService: BluePrintRuntimeService<*>,
96         name: String,
97         serviceTemplate: ServiceTemplate
98     ) {
99         val enhancers = getServiceTemplateEnhancers()
100         doEnhancement(bluePrintRuntimeService, name, serviceTemplate, enhancers)
101     }
102
103     fun enhanceTopologyTemplate(
104         bluePrintRuntimeService: BluePrintRuntimeService<*>,
105         name: String,
106         topologyTemplate: TopologyTemplate
107     ) {
108         val enhancers = getTopologyTemplateEnhancers()
109         doEnhancement(bluePrintRuntimeService, name, topologyTemplate, enhancers)
110     }
111
112     fun enhanceWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
113         val enhancers = getWorkflowEnhancers()
114         doEnhancement(bluePrintRuntimeService, name, workflow, enhancers)
115     }
116
117     fun enhanceNodeTemplate(
118         bluePrintRuntimeService: BluePrintRuntimeService<*>,
119         name: String,
120         nodeTemplate: NodeTemplate
121     ) {
122         val enhancers = getNodeTemplateEnhancers()
123         doEnhancement(bluePrintRuntimeService, name, nodeTemplate, enhancers)
124     }
125
126     fun enhanceNodeType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeType: NodeType) {
127         val enhancers = getNodeTypeEnhancers()
128         doEnhancement(bluePrintRuntimeService, name, nodeType, enhancers)
129     }
130
131     fun enhanceRelationshipTemplate(
132         bluePrintRuntimeService: BluePrintRuntimeService<*>,
133         name: String,
134         relationshipTemplate: RelationshipTemplate
135     ) {
136         val enhancers = getRelationshipTemplateEnhancers()
137         doEnhancement(bluePrintRuntimeService, name, relationshipTemplate, enhancers)
138     }
139
140     fun enhanceRelationshipType(
141         bluePrintRuntimeService: BluePrintRuntimeService<*>,
142         name: String,
143         relationshipType: RelationshipType
144     ) {
145         val enhancers = getRelationshipTypeEnhancers()
146         doEnhancement(bluePrintRuntimeService, name, relationshipType, enhancers)
147     }
148
149     fun enhanceArtifactDefinition(
150         bluePrintRuntimeService: BluePrintRuntimeService<*>,
151         name: String,
152         artifactDefinition: ArtifactDefinition
153     ) {
154         val enhancers = getArtifactDefinitionEnhancers()
155         doEnhancement(bluePrintRuntimeService, name, artifactDefinition, enhancers)
156     }
157
158     fun enhancePolicyType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, policyType: PolicyType) {
159         val enhancers = getPolicyTypeEnhancers()
160         doEnhancement(bluePrintRuntimeService, name, policyType, enhancers)
161     }
162
163     fun enhancePropertyDefinitions(
164         bluePrintRuntimeService: BluePrintRuntimeService<*>,
165         properties: MutableMap<String, PropertyDefinition>
166     ) {
167         val errorMap = linkedMapOf<String, BluePrintException>()
168         properties.forEach { propertyName, propertyDefinition ->
169             try {
170                 enhancePropertyDefinition(bluePrintRuntimeService, propertyName, propertyDefinition)
171             } catch (e: BluePrintException) {
172                 errorMap[propertyName] = e
173             }
174         }
175         if (errorMap.isNotEmpty()) {
176             val nestedErrors = errorMap.keys.map { "[ property: ${errorMap[it]?.message} ]" }.joinToString(";")
177             throw BluePrintException("Failed to enhance properties $nestedErrors")
178         }
179     }
180
181     fun enhancePropertyDefinition(
182         bluePrintRuntimeService: BluePrintRuntimeService<*>,
183         name: String,
184         propertyDefinition: PropertyDefinition
185     ) {
186         val enhancers = getPropertyDefinitionEnhancers()
187         doEnhancement(bluePrintRuntimeService, name, propertyDefinition, enhancers)
188     }
189
190     fun enhanceAttributeDefinitions(
191         bluePrintRuntimeService: BluePrintRuntimeService<*>,
192         attributes: MutableMap<String, AttributeDefinition>
193     ) {
194         val errorMap = linkedMapOf<String, BluePrintException>()
195         attributes.forEach { attributeName, attributeDefinition ->
196             try {
197                 enhanceAttributeDefinition(bluePrintRuntimeService, attributeName, attributeDefinition)
198             } catch (e: BluePrintException) {
199                 errorMap[attributeName] = e
200             }
201         }
202         if (errorMap.isNotEmpty()) {
203             val nestedErrors = errorMap.keys.map { "[ attribute: ${errorMap[it]?.message} ]" }.joinToString(";")
204             throw BluePrintException("Failed to enhance attributes $nestedErrors")
205         }
206     }
207
208     fun enhanceAttributeDefinition(
209         bluePrintRuntimeService: BluePrintRuntimeService<*>,
210         name: String,
211         attributeDefinition: AttributeDefinition
212     ) {
213         val enhancers = getAttributeDefinitionEnhancers()
214         doEnhancement(bluePrintRuntimeService, name, attributeDefinition, enhancers)
215     }
216
217     @Suppress("UNCHECKED_CAST")
218     private fun <T> doEnhancement(
219         bluePrintRuntimeService: BluePrintRuntimeService<*>,
220         name: String,
221         definition: Any,
222         enhancers: List<BluePrintEnhancer<T>>
223     ) {
224         if (enhancers.isNotEmpty()) {
225             val errorMap = linkedMapOf<String, BluePrintException>()
226             enhancers.forEach {
227                 try {
228                     it.enhance(bluePrintRuntimeService, name, definition as T)
229                 } catch (e: BluePrintException) {
230                     errorMap[name] = e
231                 }
232             }
233             if (errorMap.isNotEmpty()) {
234                 val nestedErrors = errorMap.keys.map {
235                     "${errorMap[it]?.message ?: errorMap[it].toString()}"
236                 }.joinToString(";")
237                 throw BluePrintException("$name-->$nestedErrors")
238             }
239         }
240     }
241 }