Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / enhancer / BluePrintWorkflowEnhancerImpl.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.blueprintsprocessor.designer.api.enhancer
19
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
32 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
33 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
34 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
35 import org.springframework.beans.factory.config.ConfigurableBeanFactory
36 import org.springframework.context.annotation.Scope
37 import org.springframework.stereotype.Service
38
39 @Service
40 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
41 open class BluePrintWorkflowEnhancerImpl(
42     private val bluePrintRepoService: BluePrintRepoService,
43     private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
44     private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService
45 ) :
46     BluePrintWorkflowEnhancer {
47
48     private val log = logger(BluePrintWorkflowEnhancerImpl::class)
49
50     companion object {
51
52         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
53         const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
54     }
55
56     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
57     lateinit var bluePrintContext: BluePrintContext
58
59     private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
60
61     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
62         log.info("##### Enhancing Workflow($name)")
63         this.bluePrintRuntimeService = bluePrintRuntimeService
64         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
65
66         val dynamicPropertyName = "$name-properties"
67         if (workflow.inputs == null) {
68             workflow.inputs = hashMapOf()
69         }
70         // Clean Dynamic Property Field, If present
71         workflow.inputs?.remove(dynamicPropertyName)
72
73         // Enrich Workflow Inputs
74         enhanceWorkflowInputs(name, workflow)
75
76         // Enrich Workflow Outputs
77         enhanceWorkflowOutputs(name, workflow)
78
79         // Enrich Only for Resource Assignment and Dynamic Input Properties if any
80         enhanceStepTargets(name, workflow)
81     }
82
83     open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
84
85         workflow.inputs?.let { inputs ->
86             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
87         }
88     }
89
90     open fun enhanceWorkflowOutputs(name: String, workflow: Workflow) {
91         workflow.outputs?.let { outputs ->
92             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, outputs)
93         }
94     }
95
96     private fun enhanceStepTargets(name: String, workflow: Workflow) {
97
98         // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
99         val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
100
101         val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
102
103         when {
104             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
105                 enhanceStepTargets(name, workflow, firstNodeTemplateName, false)
106             }
107             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
108                 enhanceStepTargets(name, workflow, firstNodeTemplateName, true)
109             }
110             else -> {
111                 throw BluePrintProcessorException(
112                     "couldn't execute workflow($name) step mapped " +
113                         "to node template($firstNodeTemplateName) derived from($derivedFrom)"
114                 )
115             }
116         }
117     }
118
119     private fun enhanceStepTargets(name: String, workflow: Workflow, nodeTemplateName: String, isDG: Boolean) {
120
121         val dependencyNodeTemplates: List<String>
122         if (isDG) {
123             val dgNodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)
124
125             // Get the Dependent Component Node Template Names
126             val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
127                 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
128
129             dependencyNodeTemplates =
130                 JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
131         } else {
132             dependencyNodeTemplates = listOf(nodeTemplateName)
133         }
134
135         log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
136
137         // Check and Get Resource Assignment File
138         val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
139             log.info("identified workflow($name) targets($componentNodeTemplateName)")
140
141             val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
142                 .artifacts?.filter {
143                     it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
144                 }?.map {
145                     log.info("resource assignment artifacts(${it.key}) for NodeType($componentNodeTemplateName)")
146                     it.value.file
147                 }
148             resourceAssignmentArtifacts
149         }?.flatten()
150
151         log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
152
153         if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
154
155             // Add Workflow Dynamic Property
156             addWorkFlowDynamicPropertyDefinitions(name, workflow)
157
158             resourceAssignmentArtifacts.forEach { fileName ->
159                 // Enhance Resource Assignment File
160                 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
161                 // Add Workflow Dynamic DataType
162                 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
163             }
164         }
165     }
166
167     // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
168     private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
169
170         val filePath = "${bluePrintContext.rootPath}/$fileName"
171
172         log.info("enriching artifacts file($filePath")
173
174         val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
175
176         val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
177             as? MutableList<ResourceAssignment>
178             ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
179
180         val alreadyEnhancedKey = "enhanced-$fileName"
181         val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
182
183         log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
184
185         if (!alreadyEnhanced) {
186             // Call Resource Assignment Enhancer
187             resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
188             bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
189         }
190
191         resourceAssignments.forEach { resourceAssignment ->
192             resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
193         }
194         return resourceAssignmentProperties
195     }
196
197     private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
198         val dynamicPropertyName = "$name-properties"
199         val propertyDefinition = PropertyDefinition()
200         propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
201         propertyDefinition.type = "dt-$dynamicPropertyName"
202         propertyDefinition.required = true
203         // Add to Workflow Inputs
204         workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
205     }
206
207     private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
208
209         val dataTypeName = "dt-$workflowName-properties"
210
211         var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
212
213         if (dynamicDataType == null) {
214             log.info("dataType not present for the recipe({})", dataTypeName)
215             dynamicDataType = DataType()
216             dynamicDataType.version = "1.0.0"
217             dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
218             dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
219
220             val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
221             dynamicDataType.properties = dataTypeProperties
222
223             // Overwrite WorkFlow DataType
224             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
225         } else {
226             log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
227         }
228         // Merge all the Recipe Properties
229         mappingProperties.forEach { (propertyName, propertyDefinition) ->
230             dynamicDataType.properties?.put(propertyName, propertyDefinition)
231         }
232     }
233 }