2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.core.interfaces
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
35 interface BluePrintEnhancer<T> {
36 fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, type: T)
39 interface BluePrintServiceTemplateEnhancer : BluePrintEnhancer<ServiceTemplate>
41 interface BluePrintTopologyTemplateEnhancer : BluePrintEnhancer<TopologyTemplate>
43 interface BluePrintWorkflowEnhancer : BluePrintEnhancer<Workflow>
45 interface BluePrintNodeTemplateEnhancer : BluePrintEnhancer<NodeTemplate>
47 interface BluePrintNodeTypeEnhancer : BluePrintEnhancer<NodeType>
49 interface BluePrintRelationshipTemplateEnhancer : BluePrintEnhancer<RelationshipTemplate>
51 interface BluePrintRelationshipTypeEnhancer : BluePrintEnhancer<RelationshipType>
53 interface BluePrintArtifactDefinitionEnhancer : BluePrintEnhancer<ArtifactDefinition>
55 interface BluePrintPolicyTypeEnhancer : BluePrintEnhancer<PolicyType>
57 interface BluePrintPropertyDefinitionEnhancer : BluePrintEnhancer<PropertyDefinition>
59 interface BluePrintAttributeDefinitionEnhancer : BluePrintEnhancer<AttributeDefinition>
61 interface BluePrintEnhancerService {
63 @Throws(BluePrintException::class)
64 suspend fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext
66 @Throws(BluePrintException::class)
67 suspend fun enhance(basePath: String): BluePrintContext
70 interface BluePrintTypeEnhancerService {
72 fun getServiceTemplateEnhancers(): List<BluePrintServiceTemplateEnhancer>
74 fun getTopologyTemplateEnhancers(): List<BluePrintTopologyTemplateEnhancer>
76 fun getWorkflowEnhancers(): List<BluePrintWorkflowEnhancer>
78 fun getNodeTemplateEnhancers(): List<BluePrintNodeTemplateEnhancer>
80 fun getNodeTypeEnhancers(): List<BluePrintNodeTypeEnhancer>
82 fun getRelationshipTemplateEnhancers(): List<BluePrintRelationshipTemplateEnhancer>
84 fun getRelationshipTypeEnhancers(): List<BluePrintRelationshipTypeEnhancer>
86 fun getArtifactDefinitionEnhancers(): List<BluePrintArtifactDefinitionEnhancer>
88 fun getPolicyTypeEnhancers(): List<BluePrintPolicyTypeEnhancer>
90 fun getPropertyDefinitionEnhancers(): List<BluePrintPropertyDefinitionEnhancer>
92 fun getAttributeDefinitionEnhancers(): List<BluePrintAttributeDefinitionEnhancer>
94 fun enhanceServiceTemplate(
95 bluePrintRuntimeService: BluePrintRuntimeService<*>,
97 serviceTemplate: ServiceTemplate
99 val enhancers = getServiceTemplateEnhancers()
100 doEnhancement(bluePrintRuntimeService, name, serviceTemplate, enhancers)
103 fun enhanceTopologyTemplate(
104 bluePrintRuntimeService: BluePrintRuntimeService<*>,
106 topologyTemplate: TopologyTemplate
108 val enhancers = getTopologyTemplateEnhancers()
109 doEnhancement(bluePrintRuntimeService, name, topologyTemplate, enhancers)
112 fun enhanceWorkflow(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
113 val enhancers = getWorkflowEnhancers()
114 doEnhancement(bluePrintRuntimeService, name, workflow, enhancers)
117 fun enhanceNodeTemplate(
118 bluePrintRuntimeService: BluePrintRuntimeService<*>,
120 nodeTemplate: NodeTemplate
122 val enhancers = getNodeTemplateEnhancers()
123 doEnhancement(bluePrintRuntimeService, name, nodeTemplate, enhancers)
126 fun enhanceNodeType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, nodeType: NodeType) {
127 val enhancers = getNodeTypeEnhancers()
128 doEnhancement(bluePrintRuntimeService, name, nodeType, enhancers)
131 fun enhanceRelationshipTemplate(
132 bluePrintRuntimeService: BluePrintRuntimeService<*>,
134 relationshipTemplate: RelationshipTemplate
136 val enhancers = getRelationshipTemplateEnhancers()
137 doEnhancement(bluePrintRuntimeService, name, relationshipTemplate, enhancers)
140 fun enhanceRelationshipType(
141 bluePrintRuntimeService: BluePrintRuntimeService<*>,
143 relationshipType: RelationshipType
145 val enhancers = getRelationshipTypeEnhancers()
146 doEnhancement(bluePrintRuntimeService, name, relationshipType, enhancers)
149 fun enhanceArtifactDefinition(
150 bluePrintRuntimeService: BluePrintRuntimeService<*>,
152 artifactDefinition: ArtifactDefinition
154 val enhancers = getArtifactDefinitionEnhancers()
155 doEnhancement(bluePrintRuntimeService, name, artifactDefinition, enhancers)
158 fun enhancePolicyType(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, policyType: PolicyType) {
159 val enhancers = getPolicyTypeEnhancers()
160 doEnhancement(bluePrintRuntimeService, name, policyType, enhancers)
163 fun enhancePropertyDefinitions(
164 bluePrintRuntimeService: BluePrintRuntimeService<*>,
165 properties: MutableMap<String, PropertyDefinition>
167 val errorMap = linkedMapOf<String, BluePrintException>()
168 properties.forEach { propertyName, propertyDefinition ->
170 enhancePropertyDefinition(bluePrintRuntimeService, propertyName, propertyDefinition)
171 } catch (e: BluePrintException) {
172 errorMap[propertyName] = e
175 if (errorMap.isNotEmpty()) {
176 val nestedErrors = errorMap.keys.map { "[ property: ${errorMap[it]?.message} ]" }.joinToString(";")
177 throw BluePrintException("Failed to enhance properties $nestedErrors")
181 fun enhancePropertyDefinition(
182 bluePrintRuntimeService: BluePrintRuntimeService<*>,
184 propertyDefinition: PropertyDefinition
186 val enhancers = getPropertyDefinitionEnhancers()
187 doEnhancement(bluePrintRuntimeService, name, propertyDefinition, enhancers)
190 fun enhanceAttributeDefinitions(
191 bluePrintRuntimeService: BluePrintRuntimeService<*>,
192 attributes: MutableMap<String, AttributeDefinition>
194 val errorMap = linkedMapOf<String, BluePrintException>()
195 attributes.forEach { attributeName, attributeDefinition ->
197 enhanceAttributeDefinition(bluePrintRuntimeService, attributeName, attributeDefinition)
198 } catch (e: BluePrintException) {
199 errorMap[attributeName] = e
202 if (errorMap.isNotEmpty()) {
203 val nestedErrors = errorMap.keys.map { "[ attribute: ${errorMap[it]?.message} ]" }.joinToString(";")
204 throw BluePrintException("Failed to enhance attributes $nestedErrors")
208 fun enhanceAttributeDefinition(
209 bluePrintRuntimeService: BluePrintRuntimeService<*>,
211 attributeDefinition: AttributeDefinition
213 val enhancers = getAttributeDefinitionEnhancers()
214 doEnhancement(bluePrintRuntimeService, name, attributeDefinition, enhancers)
217 @Suppress("UNCHECKED_CAST")
218 private fun <T> doEnhancement(
219 bluePrintRuntimeService: BluePrintRuntimeService<*>,
222 enhancers: List<BluePrintEnhancer<T>>
224 if (enhancers.isNotEmpty()) {
225 val errorMap = linkedMapOf<String, BluePrintException>()
228 it.enhance(bluePrintRuntimeService, name, definition as T)
229 } catch (e: BluePrintException) {
233 if (errorMap.isNotEmpty()) {
234 val nestedErrors = errorMap.keys.map {
235 "${errorMap[it]?.message ?: errorMap[it].toString()}"
237 throw BluePrintException("$name-->$nestedErrors")