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.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
27 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintRepoService
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
30 import org.onap.ccsdk.cds.controllerblueprints.core.logger
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(
42 private val bluePrintRepoService: BluePrintRepoService,
43 private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
44 private val resourceAssignmentEnhancerService: ResourceAssignmentEnhancerService
46 BluePrintWorkflowEnhancer {
48 private val log = logger(BluePrintWorkflowEnhancerImpl::class)
51 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
52 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
55 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
56 lateinit var bluePrintContext: BluePrintContext
58 private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
60 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
61 log.info("##### Enhancing Workflow($name)")
62 this.bluePrintRuntimeService = bluePrintRuntimeService
63 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
65 val dynamicPropertyName = "$name-properties"
66 if (workflow.inputs == null) {
67 workflow.inputs = hashMapOf()
69 // Clean Dynamic Property Field, If present
70 workflow.inputs?.remove(dynamicPropertyName)
72 // Enrich Workflow Inputs
73 enhanceWorkflowInputs(name, workflow)
75 // Enrich Workflow Outputs
76 enhanceWorkflowOutputs(name, workflow)
78 // Enrich Only for Resource Assignment and Dynamic Input Properties if any
79 enhanceStepTargets(name, workflow)
82 open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
84 workflow.inputs?.let { inputs ->
85 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
89 open fun enhanceWorkflowOutputs(name: String, workflow: Workflow) {
90 workflow.outputs?.let { outputs ->
91 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, outputs)
95 private fun enhanceStepTargets(name: String, workflow: Workflow) {
97 // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
98 val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
100 val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
103 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
104 enhanceStepTargets(name, workflow, firstNodeTemplateName, false)
106 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
107 enhanceStepTargets(name, workflow, firstNodeTemplateName, true)
110 throw BluePrintProcessorException(
111 "couldn't execute workflow($name) step mapped " +
112 "to node template($firstNodeTemplateName) derived from($derivedFrom)"
118 private fun enhanceStepTargets(name: String, workflow: Workflow, nodeTemplateName: String, isDG: Boolean) {
120 val dependencyNodeTemplates: List<String>
122 val dgNodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)
124 // Get the Dependent Component Node Template Names
125 val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
126 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
128 dependencyNodeTemplates =
129 JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
131 dependencyNodeTemplates = listOf(nodeTemplateName)
134 log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
136 // Check and Get Resource Assignment File
137 val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
138 log.info("identified workflow($name) targets($componentNodeTemplateName)")
140 val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
142 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
144 log.info("resource assignment artifacts(${it.key}) for NodeType($componentNodeTemplateName)")
147 resourceAssignmentArtifacts
150 log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
152 if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
154 // Add Workflow Dynamic Property
155 addWorkFlowDynamicPropertyDefinitions(name, workflow)
157 resourceAssignmentArtifacts.forEach { fileName ->
158 // Enhance Resource Assignment File
159 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
160 // Add Workflow Dynamic DataType
161 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
166 // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
167 private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
169 val filePath = "${bluePrintContext.rootPath}/$fileName"
171 log.info("enriching artifacts file($filePath")
173 val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
175 val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
176 as? MutableList<ResourceAssignment>
177 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
179 val alreadyEnhancedKey = "enhanced-$fileName"
180 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
182 log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
184 if (!alreadyEnhanced) {
185 // Call Resource Assignment Enhancer
186 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
187 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
190 resourceAssignments.forEach { resourceAssignment ->
191 resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
193 return resourceAssignmentProperties
196 private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
197 val dynamicPropertyName = "$name-properties"
198 val propertyDefinition = PropertyDefinition()
199 propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
200 propertyDefinition.type = "dt-$dynamicPropertyName"
201 propertyDefinition.required = true
202 // Add to Workflow Inputs
203 workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
206 private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
208 val dataTypeName = "dt-$workflowName-properties"
210 var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
212 if (dynamicDataType == null) {
213 log.info("dataType not present for the recipe({})", dataTypeName)
214 dynamicDataType = DataType()
215 dynamicDataType.version = "1.0.0"
216 dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
217 dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
219 val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
220 dynamicDataType.properties = dataTypeProperties
222 // Overwrite WorkFlow DataType
223 bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
225 log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
227 // Merge all the Recipe Properties
228 mappingProperties.forEach { (propertyName, propertyDefinition) ->
229 dynamicDataType.properties?.put(propertyName, propertyDefinition)