Merge "Improve Rest Service API"
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / service / 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.controllerblueprints.service.enhancer
19
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
39
40 @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())
47
48     companion object {
49         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
50         const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
51     }
52
53     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
54     lateinit var bluePrintContext: BluePrintContext
55
56     private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
57
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()
62
63         val dynamicPropertyName = "$name-properties"
64         if (workflow.inputs == null) {
65             workflow.inputs = hashMapOf()
66         }
67         // Clean Dynamic Property Field, If present
68         workflow.inputs?.remove(dynamicPropertyName)
69
70         // Enrich Workflow Inputs
71         enhanceWorkflowInputs(name, workflow)
72
73         // Enrich Workflow Outputs
74         enhanceWorkflowOutputs(name, workflow)
75
76         // Enrich Only for Resource Assignment and Dynamic Input Properties if any
77         enhanceStepTargets(name, workflow)
78
79
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                 // DO Nothing
105             }
106             derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
107                 enhanceDGStepTargets(name, workflow, firstNodeTemplateName)
108             }
109             else -> {
110                 throw BluePrintProcessorException("couldn't execute workflow($name) step mapped " +
111                         "to node template($firstNodeTemplateName) derived from($derivedFrom)")
112             }
113         }
114
115     }
116
117     private fun enhanceDGStepTargets(name: String, workflow: Workflow, dgNodeTemplateName: String) {
118
119         val dgNodeTemplate = bluePrintContext.nodeTemplateByName(dgNodeTemplateName)
120
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) ")
124
125         val dependencyNodeTemplates = JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
126
127         log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
128
129         // Check and Get Resource Assignment File
130         val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
131             log.info("identified workflow($name) targets($componentNodeTemplateName)")
132
133             val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
134                     .artifacts?.filter {
135                 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
136             }?.map {
137                 log.info("resource assignment artifacts(${it.key}) for NodeType(${componentNodeTemplateName})")
138                 it.value.file
139             }
140             resourceAssignmentArtifacts
141         }?.flatten()
142
143         log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
144
145         if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
146
147             // Add Workflow Dynamic Property
148             addWorkFlowDynamicPropertyDefinitions(name, workflow)
149
150             resourceAssignmentArtifacts.forEach { fileName ->
151                 // Enhance Resource Assignment File
152                 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
153                 // Add Workflow Dynamic DataType
154                 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
155             }
156         }
157     }
158
159     // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
160     private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
161
162         val filePath = "${bluePrintContext.rootPath}/$fileName"
163
164         log.info("enriching artifacts file(${filePath}")
165
166         val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
167
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)")
171
172         val alreadyEnhancedKey = "enhanced-$fileName"
173         val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
174
175         log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
176
177         if (!alreadyEnhanced) {
178             // Call Resource Assignment Enhancer
179             resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
180             bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
181         }
182
183         resourceAssignments.forEach { resourceAssignment ->
184             resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
185         }
186         return resourceAssignmentProperties
187     }
188
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)
197     }
198
199     private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
200
201         val dataTypeName = "dt-$workflowName-properties"
202
203         var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
204
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
211
212             val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
213             dynamicDataType.properties = dataTypeProperties
214
215             // Overwrite WorkFlow DataType
216             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
217
218         } else {
219             log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
220         }
221         // Merge all the Recipe Properties
222         mappingProperties.forEach { propertyName, propertyDefinition ->
223             dynamicDataType.properties?.put(propertyName, propertyDefinition)
224         }
225     }
226 }