2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.service.enhancer
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
30 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
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
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())
48 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
49 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
52 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
53 lateinit var bluePrintContext: BluePrintContext
55 private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
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()
62 val dynamicPropertyName = "$name-properties"
63 if (workflow.inputs == null) {
64 workflow.inputs = hashMapOf()
66 // Clean Dynamic Property Field, If present
67 workflow.inputs?.remove(dynamicPropertyName)
69 // Enrich Only for Resource Assignment and Dynamic Input Properties if any
70 enhanceStepTargets(name, workflow)
72 // Enrich Workflow Inputs
73 enhanceWorkflowInputs(name, workflow)
76 open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
78 workflow.inputs?.let { inputs ->
79 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
83 private fun enhanceStepTargets(name: String, workflow: Workflow) {
85 // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
86 val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
88 val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
91 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
94 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_DG, true) -> {
95 enhanceDGStepTargets(name, workflow, firstNodeTemplateName)
98 throw BluePrintProcessorException("couldn't execute workflow($name) step mapped " +
99 "to node template($firstNodeTemplateName) derived from($derivedFrom)")
105 private fun enhanceDGStepTargets(name: String, workflow: Workflow, dgNodeTemplateName: String) {
107 val dgNodeTemplate = bluePrintContext.nodeTemplateByName(dgNodeTemplateName)
109 // Get the Dependent Component Node Template Names
110 val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
111 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
113 val dependencyNodeTemplates = JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
115 log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
117 // Check and Get Resource Assignment File
118 val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
119 log.info("identified workflow($name) targets($componentNodeTemplateName)")
121 val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
123 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
125 log.info("resource assignment artifacts(${it.key}) for NodeType(${componentNodeTemplateName})")
128 resourceAssignmentArtifacts
131 log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
133 if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
135 // Add Workflow Dynamic Property
136 addWorkFlowDynamicPropertyDefinitions(name, workflow)
138 resourceAssignmentArtifacts.forEach { fileName ->
139 // Enhance Resource Assignment File
140 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
141 // Add Workflow Dynamic DataType
142 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
147 // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
148 private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
150 val filePath = "${bluePrintContext.rootPath}/$fileName"
152 log.info("enriching artifacts file(${filePath}")
154 val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
156 val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
157 as? MutableList<ResourceAssignment>
158 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
160 val alreadyEnhancedKey = "enhanced-$fileName"
161 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
163 log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
165 if (!alreadyEnhanced) {
166 // Call Resource Assignment Enhancer
167 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
168 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
171 resourceAssignments.forEach { resourceAssignment ->
172 resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
174 return resourceAssignmentProperties
177 private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
178 val dynamicPropertyName = "$name-properties"
179 val propertyDefinition = PropertyDefinition()
180 propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
181 propertyDefinition.type = "dt-$dynamicPropertyName"
182 propertyDefinition.required = true
183 // Add to Workflow Inputs
184 workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
187 private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
189 val dataTypeName = "dt-$workflowName-properties"
191 var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
193 if (dynamicDataType == null) {
194 log.info("dataType not present for the recipe({})", dataTypeName)
195 dynamicDataType = DataType()
196 dynamicDataType.version = "1.0.0"
197 dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
198 dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
200 val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
201 dynamicDataType.properties = dataTypeProperties
203 // Overwrite WorkFlow DataType
204 bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
207 log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
209 // Merge all the Recipe Properties
210 mappingProperties.forEach { propertyName, propertyDefinition ->
211 dynamicDataType.properties?.put(propertyName, propertyDefinition)