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