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> {
37 fun enhance(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, type: T)
40 interface BlueprintServiceTemplateEnhancer : BlueprintEnhancer<ServiceTemplate>
42 interface BlueprintTopologyTemplateEnhancer : BlueprintEnhancer<TopologyTemplate>
44 interface BlueprintWorkflowEnhancer : BlueprintEnhancer<Workflow>
46 interface BlueprintNodeTemplateEnhancer : BlueprintEnhancer<NodeTemplate>
48 interface BlueprintNodeTypeEnhancer : BlueprintEnhancer<NodeType>
50 interface BlueprintRelationshipTemplateEnhancer : BlueprintEnhancer<RelationshipTemplate>
52 interface BlueprintRelationshipTypeEnhancer : BlueprintEnhancer<RelationshipType>
54 interface BlueprintArtifactDefinitionEnhancer : BlueprintEnhancer<ArtifactDefinition>
56 interface BlueprintPolicyTypeEnhancer : BlueprintEnhancer<PolicyType>
58 interface BlueprintPropertyDefinitionEnhancer : BlueprintEnhancer<PropertyDefinition>
60 interface BlueprintAttributeDefinitionEnhancer : BlueprintEnhancer<AttributeDefinition>
62 interface BlueprintEnhancerService {
64 @Throws(BlueprintException::class)
65 suspend fun enhance(basePath: String, enrichedBasePath: String): BlueprintContext
67 @Throws(BlueprintException::class)
68 suspend fun enhance(basePath: String): BlueprintContext
71 interface BlueprintTypeEnhancerService {
73 fun getServiceTemplateEnhancers(): List<BlueprintServiceTemplateEnhancer>
75 fun getTopologyTemplateEnhancers(): List<BlueprintTopologyTemplateEnhancer>
77 fun getWorkflowEnhancers(): List<BlueprintWorkflowEnhancer>
79 fun getNodeTemplateEnhancers(): List<BlueprintNodeTemplateEnhancer>
81 fun getNodeTypeEnhancers(): List<BlueprintNodeTypeEnhancer>
83 fun getRelationshipTemplateEnhancers(): List<BlueprintRelationshipTemplateEnhancer>
85 fun getRelationshipTypeEnhancers(): List<BlueprintRelationshipTypeEnhancer>
87 fun getArtifactDefinitionEnhancers(): List<BlueprintArtifactDefinitionEnhancer>
89 fun getPolicyTypeEnhancers(): List<BlueprintPolicyTypeEnhancer>
91 fun getPropertyDefinitionEnhancers(): List<BlueprintPropertyDefinitionEnhancer>
93 fun getAttributeDefinitionEnhancers(): List<BlueprintAttributeDefinitionEnhancer>
95 fun enhanceServiceTemplate(
96 bluePrintRuntimeService: BlueprintRuntimeService<*>,
98 serviceTemplate: ServiceTemplate
100 val enhancers = getServiceTemplateEnhancers()
101 doEnhancement(bluePrintRuntimeService, name, serviceTemplate, enhancers)
104 fun enhanceTopologyTemplate(
105 bluePrintRuntimeService: BlueprintRuntimeService<*>,
107 topologyTemplate: TopologyTemplate
109 val enhancers = getTopologyTemplateEnhancers()
110 doEnhancement(bluePrintRuntimeService, name, topologyTemplate, enhancers)
113 fun enhanceWorkflow(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, workflow: Workflow) {
114 val enhancers = getWorkflowEnhancers()
115 doEnhancement(bluePrintRuntimeService, name, workflow, enhancers)
118 fun enhanceNodeTemplate(
119 bluePrintRuntimeService: BlueprintRuntimeService<*>,
121 nodeTemplate: NodeTemplate
123 val enhancers = getNodeTemplateEnhancers()
124 doEnhancement(bluePrintRuntimeService, name, nodeTemplate, enhancers)
127 fun enhanceNodeType(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, nodeType: NodeType) {
128 val enhancers = getNodeTypeEnhancers()
129 doEnhancement(bluePrintRuntimeService, name, nodeType, enhancers)
132 fun enhanceRelationshipTemplate(
133 bluePrintRuntimeService: BlueprintRuntimeService<*>,
135 relationshipTemplate: RelationshipTemplate
137 val enhancers = getRelationshipTemplateEnhancers()
138 doEnhancement(bluePrintRuntimeService, name, relationshipTemplate, enhancers)
141 fun enhanceRelationshipType(
142 bluePrintRuntimeService: BlueprintRuntimeService<*>,
144 relationshipType: RelationshipType
146 val enhancers = getRelationshipTypeEnhancers()
147 doEnhancement(bluePrintRuntimeService, name, relationshipType, enhancers)
150 fun enhanceArtifactDefinition(
151 bluePrintRuntimeService: BlueprintRuntimeService<*>,
153 artifactDefinition: ArtifactDefinition
155 val enhancers = getArtifactDefinitionEnhancers()
156 doEnhancement(bluePrintRuntimeService, name, artifactDefinition, enhancers)
159 fun enhancePolicyType(bluePrintRuntimeService: BlueprintRuntimeService<*>, name: String, policyType: PolicyType) {
160 val enhancers = getPolicyTypeEnhancers()
161 doEnhancement(bluePrintRuntimeService, name, policyType, enhancers)
164 fun enhancePropertyDefinitions(
165 bluePrintRuntimeService: BlueprintRuntimeService<*>,
166 properties: MutableMap<String, PropertyDefinition>
168 val errorMap = linkedMapOf<String, BlueprintException>()
169 properties.forEach { propertyName, propertyDefinition ->
171 enhancePropertyDefinition(bluePrintRuntimeService, propertyName, propertyDefinition)
172 } catch (e: BlueprintException) {
173 errorMap[propertyName] = e
176 if (errorMap.isNotEmpty()) {
177 val nestedErrors = errorMap.keys.map { "[ property: ${errorMap[it]?.message} ]" }.joinToString(";")
178 throw BlueprintException("Failed to enhance properties $nestedErrors")
182 fun enhancePropertyDefinition(
183 bluePrintRuntimeService: BlueprintRuntimeService<*>,
185 propertyDefinition: PropertyDefinition
187 val enhancers = getPropertyDefinitionEnhancers()
188 doEnhancement(bluePrintRuntimeService, name, propertyDefinition, enhancers)
191 fun enhanceAttributeDefinitions(
192 bluePrintRuntimeService: BlueprintRuntimeService<*>,
193 attributes: MutableMap<String, AttributeDefinition>
195 val errorMap = linkedMapOf<String, BlueprintException>()
196 attributes.forEach { attributeName, attributeDefinition ->
198 enhanceAttributeDefinition(bluePrintRuntimeService, attributeName, attributeDefinition)
199 } catch (e: BlueprintException) {
200 errorMap[attributeName] = e
203 if (errorMap.isNotEmpty()) {
204 val nestedErrors = errorMap.keys.map { "[ attribute: ${errorMap[it]?.message} ]" }.joinToString(";")
205 throw BlueprintException("Failed to enhance attributes $nestedErrors")
209 fun enhanceAttributeDefinition(
210 bluePrintRuntimeService: BlueprintRuntimeService<*>,
212 attributeDefinition: AttributeDefinition
214 val enhancers = getAttributeDefinitionEnhancers()
215 doEnhancement(bluePrintRuntimeService, name, attributeDefinition, enhancers)
218 @Suppress("UNCHECKED_CAST")
219 private fun <T> doEnhancement(
220 bluePrintRuntimeService: BlueprintRuntimeService<*>,
223 enhancers: List<BlueprintEnhancer<T>>
225 if (enhancers.isNotEmpty()) {
226 val errorMap = linkedMapOf<String, BlueprintException>()
229 it.enhance(bluePrintRuntimeService, name, definition as T)
230 } catch (e: BlueprintException) {
234 if (errorMap.isNotEmpty()) {
235 val nestedErrors = errorMap.keys.map {
236 "${errorMap[it]?.message ?: errorMap[it].toString()}"
238 throw BlueprintException("$name-->$nestedErrors")