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.service.enhancer
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
24 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
31 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
33 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
34 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
35 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
36 import org.springframework.beans.factory.config.ConfigurableBeanFactory
37 import org.springframework.context.annotation.Scope
38 import org.springframework.stereotype.Service
41 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
42 open class BluePrintWorkflowEnhancerImpl(private val bluePrintRepoService: BluePrintRepoService,
43 private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
44 private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService)
45 : BluePrintWorkflowEnhancer {
46 private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintWorkflowEnhancerImpl::class.toString())
49 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
50 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
53 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
54 lateinit var bluePrintContext: BluePrintContext
56 private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
58 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
59 log.info("##### Enhancing Workflow($name)")
60 this.bluePrintRuntimeService = bluePrintRuntimeService
61 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
63 val dynamicPropertyName = "$name-properties"
64 if (workflow.inputs == null) {
65 workflow.inputs = hashMapOf()
67 // Clean Dynamic Property Field, If present
68 workflow.inputs?.remove(dynamicPropertyName)
70 // Enrich Workflow Inputs
71 enhanceWorkflowInputs(name, workflow)
73 // Enrich Workflow Outputs
74 enhanceWorkflowOutputs(name, workflow)
76 // Enrich Only for Resource Assignment and Dynamic Input Properties if any
77 enhanceStepTargets(name, workflow)
82 open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
84 workflow.inputs?.let { inputs ->
85 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
89 open fun enhanceWorkflowOutputs(name: String, workflow: Workflow) {
90 workflow.outputs?.let { outputs ->
91 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, outputs)
95 private fun enhanceStepTargets(name: String, workflow: Workflow) {
97 // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
98 val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
100 val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
103 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
106 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
107 enhanceDGStepTargets(name, workflow, firstNodeTemplateName)
110 throw BluePrintProcessorException("couldn't execute workflow($name) step mapped " +
111 "to node template($firstNodeTemplateName) derived from($derivedFrom)")
117 private fun enhanceDGStepTargets(name: String, workflow: Workflow, dgNodeTemplateName: String) {
119 val dgNodeTemplate = bluePrintContext.nodeTemplateByName(dgNodeTemplateName)
121 // Get the Dependent Component Node Template Names
122 val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
123 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
125 val dependencyNodeTemplates = JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
127 log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
129 // Check and Get Resource Assignment File
130 val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
131 log.info("identified workflow($name) targets($componentNodeTemplateName)")
133 val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
135 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
137 log.info("resource assignment artifacts(${it.key}) for NodeType(${componentNodeTemplateName})")
140 resourceAssignmentArtifacts
143 log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
145 if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
147 // Add Workflow Dynamic Property
148 addWorkFlowDynamicPropertyDefinitions(name, workflow)
150 resourceAssignmentArtifacts.forEach { fileName ->
151 // Enhance Resource Assignment File
152 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
153 // Add Workflow Dynamic DataType
154 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
159 // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
160 private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
162 val filePath = "${bluePrintContext.rootPath}/$fileName"
164 log.info("enriching artifacts file(${filePath}")
166 val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
168 val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
169 as? MutableList<ResourceAssignment>
170 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
172 val alreadyEnhancedKey = "enhanced-$fileName"
173 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
175 log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
177 if (!alreadyEnhanced) {
178 // Call Resource Assignment Enhancer
179 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
180 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
183 resourceAssignments.forEach { resourceAssignment ->
184 resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
186 return resourceAssignmentProperties
189 private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
190 val dynamicPropertyName = "$name-properties"
191 val propertyDefinition = PropertyDefinition()
192 propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
193 propertyDefinition.type = "dt-$dynamicPropertyName"
194 propertyDefinition.required = true
195 // Add to Workflow Inputs
196 workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
199 private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
201 val dataTypeName = "dt-$workflowName-properties"
203 var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
205 if (dynamicDataType == null) {
206 log.info("dataType not present for the recipe({})", dataTypeName)
207 dynamicDataType = DataType()
208 dynamicDataType.version = "1.0.0"
209 dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
210 dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
212 val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
213 dynamicDataType.properties = dataTypeProperties
215 // Overwrite WorkFlow DataType
216 bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
219 log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
221 // Merge all the Recipe Properties
222 mappingProperties.forEach { propertyName, propertyDefinition ->
223 dynamicDataType.properties?.put(propertyName, propertyDefinition)