2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer
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
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)
44 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
45 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
48 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
49 lateinit var bluePrintContext: BluePrintContext
51 private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
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()
58 val dynamicPropertyName = "$name-properties"
59 if (workflow.inputs == null) {
60 workflow.inputs = hashMapOf()
62 // Clean Dynamic Property Field, If present
63 workflow.inputs?.remove(dynamicPropertyName)
65 // Enrich Workflow Inputs
66 enhanceWorkflowInputs(name, workflow)
68 // Enrich Workflow Outputs
69 enhanceWorkflowOutputs(name, workflow)
71 // Enrich Only for Resource Assignment and Dynamic Input Properties if any
72 enhanceStepTargets(name, workflow)
77 open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
79 workflow.inputs?.let { inputs ->
80 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
84 open fun enhanceWorkflowOutputs(name: String, workflow: Workflow) {
85 workflow.outputs?.let { outputs ->
86 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, outputs)
90 private fun enhanceStepTargets(name: String, workflow: Workflow) {
92 // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
93 val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
95 val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
98 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
99 enhanceStepTargets(name, workflow, firstNodeTemplateName, false)
101 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
102 enhanceStepTargets(name, workflow, firstNodeTemplateName, true)
105 throw BluePrintProcessorException("couldn't execute workflow($name) step mapped " +
106 "to node template($firstNodeTemplateName) derived from($derivedFrom)")
112 private fun enhanceStepTargets(name: String, workflow: Workflow, nodeTemplateName: String, isDG: Boolean) {
114 val dependencyNodeTemplates: List<String>
116 val dgNodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)
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) ")
122 dependencyNodeTemplates =
123 JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
125 dependencyNodeTemplates = listOf(nodeTemplateName)
128 log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
130 // Check and Get Resource Assignment File
131 val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
132 log.info("identified workflow($name) targets($componentNodeTemplateName)")
134 val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
136 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
138 log.info("resource assignment artifacts(${it.key}) for NodeType(${componentNodeTemplateName})")
141 resourceAssignmentArtifacts
144 log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
146 if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
148 // Add Workflow Dynamic Property
149 addWorkFlowDynamicPropertyDefinitions(name, workflow)
151 resourceAssignmentArtifacts.forEach { fileName ->
152 // Enhance Resource Assignment File
153 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
154 // Add Workflow Dynamic DataType
155 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
160 // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
161 private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
163 val filePath = "${bluePrintContext.rootPath}/$fileName"
165 log.info("enriching artifacts file(${filePath}")
167 val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
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)")
173 val alreadyEnhancedKey = "enhanced-$fileName"
174 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
176 log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
178 if (!alreadyEnhanced) {
179 // Call Resource Assignment Enhancer
180 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
181 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
184 resourceAssignments.forEach { resourceAssignment ->
185 resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
187 return resourceAssignmentProperties
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)
200 private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
202 val dataTypeName = "dt-$workflowName-properties"
204 var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
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
213 val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
214 dynamicDataType.properties = dataTypeProperties
216 // Overwrite WorkFlow DataType
217 bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
220 log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
222 // Merge all the Recipe Properties
223 mappingProperties.forEach { (propertyName, propertyDefinition) ->
224 dynamicDataType.properties?.put(propertyName, propertyDefinition)