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