9fbf0916a2699c476e461c4117d08c67394c77ff
[ccsdk/cds.git] / ms / controllerblueprints / modules / service / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / service / enhancer / BluePrintWorkflowEnhancerImpl.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.service.enhancer
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.Workflow
28 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
29 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
30 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
31 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
32 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
33 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
34 import org.onap.ccsdk.apps.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(private val bluePrintRepoService: BluePrintRepoService,
42                                          private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
43                                          private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService)
44     : BluePrintWorkflowEnhancer {
45     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintWorkflowEnhancerImpl::class.toString())
46
47     companion object {
48         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
49         const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
50     }
51
52     lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
53     lateinit var bluePrintContext: BluePrintContext
54
55     private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
56
57     override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
58         log.info("##### Enhancing Workflow($name)")
59         this.bluePrintRuntimeService = bluePrintRuntimeService
60         this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
61
62         val dynamicPropertyName = "$name-properties"
63         if (workflow.inputs == null) {
64             workflow.inputs = hashMapOf()
65         }
66         // Clean Dynamic Property Field, If present
67         workflow.inputs?.remove(dynamicPropertyName)
68
69         // Enrich Only for Resource Assignment and Dynamic Input Properties if any
70         enhanceStepTargets(name, workflow)
71
72         // Enrich Workflow Inputs
73         enhanceWorkflowInputs(name, workflow)
74     }
75
76     open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
77
78         workflow.inputs?.let { inputs ->
79             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
80         }
81     }
82
83     private fun enhanceStepTargets(name: String, workflow: Workflow) {
84
85         // Get the first Step Target NodeTemplate name( Since that is the DG Node Template)
86         val dgNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
87
88         val dgNodeTemplate = bluePrintContext.nodeTemplateByName(dgNodeTemplateName)
89
90         // Get the Dependent Component Node Template Names
91         val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
92                 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
93
94         val dependencyNodeTemplates = JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
95
96         log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
97
98         // Check and Get Resource Assignment File
99         val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
100             log.info("identified workflow($name) targets($componentNodeTemplateName)")
101
102             val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
103                     .artifacts?.filter {
104                 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
105             }?.map {
106                 log.info("resource assignment artifacts(${it.key}) for NodeType(${componentNodeTemplateName})")
107                 it.value.file
108             }
109             resourceAssignmentArtifacts
110         }?.flatten()
111
112         log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
113
114         if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
115
116             // Add Workflow Dynamic Property
117             addWorkFlowDynamicPropertyDefinitions(name, workflow)
118
119             resourceAssignmentArtifacts.forEach { fileName ->
120                 // Enhance Resource Assignment File
121                 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
122                 // Add Workflow Dynamic DataType
123                 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
124             }
125         }
126     }
127
128     // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
129     private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
130
131         val filePath = "${bluePrintContext.rootPath}/$fileName"
132
133         log.info("enriching artifacts file(${filePath}")
134
135         val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
136
137         val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
138                 as? MutableList<ResourceAssignment>
139                 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
140
141         val alreadyEnhancedKey = "enhanced-$fileName"
142         val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
143
144         log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
145
146         if (!alreadyEnhanced) {
147             // Call Resource Assignment Enhancer
148             resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
149             bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
150         }
151
152         resourceAssignments.forEach { resourceAssignment ->
153             resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
154         }
155         return resourceAssignmentProperties
156     }
157
158     private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
159         val dynamicPropertyName = "$name-properties"
160         val propertyDefinition = PropertyDefinition()
161         propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
162         propertyDefinition.type = "dt-$dynamicPropertyName"
163         propertyDefinition.required = true
164         // Add to Workflow Inputs
165         workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
166     }
167
168     private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
169
170         val dataTypeName = "dt-$workflowName-properties"
171
172         var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
173
174         if (dynamicDataType == null) {
175             log.info("dataType not present for the recipe({})", dataTypeName)
176             dynamicDataType = DataType()
177             dynamicDataType.version = "1.0.0"
178             dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
179             dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
180
181             val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
182             dynamicDataType.properties = dataTypeProperties
183
184             // Overwrite WorkFlow DataType
185             bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
186
187         } else {
188             log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
189         }
190         // Merge all the Recipe Properties
191         mappingProperties.forEach { propertyName, propertyDefinition ->
192             dynamicDataType.properties?.put(propertyName, propertyDefinition)
193         }
194     }
195 }