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)
52 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
53 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
56 lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
57 lateinit var bluePrintContext: BluePrintContext
59 private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
61 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>, name: String, workflow: Workflow) {
62 log.info("##### Enhancing Workflow($name)")
63 this.bluePrintRuntimeService = bluePrintRuntimeService
64 this.bluePrintContext = bluePrintRuntimeService.bluePrintContext()
66 val dynamicPropertyName = "$name-properties"
67 if (workflow.inputs == null) {
68 workflow.inputs = hashMapOf()
70 // Clean Dynamic Property Field, If present
71 workflow.inputs?.remove(dynamicPropertyName)
73 // Enrich Workflow Inputs
74 enhanceWorkflowInputs(name, workflow)
76 // Enrich Workflow Outputs
77 enhanceWorkflowOutputs(name, workflow)
79 // Enrich Only for Resource Assignment and Dynamic Input Properties if any
80 enhanceStepTargets(name, workflow)
83 open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
85 workflow.inputs?.let { inputs ->
86 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, inputs)
90 open fun enhanceWorkflowOutputs(name: String, workflow: Workflow) {
91 workflow.outputs?.let { outputs ->
92 bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintRuntimeService, outputs)
96 private fun enhanceStepTargets(name: String, workflow: Workflow) {
98 // Get the first Step Target NodeTemplate name( It may be Component or DG Node Template)
99 val firstNodeTemplateName = bluePrintContext.workflowFirstStepNodeTemplate(name)
101 val derivedFrom = bluePrintContext.nodeTemplateNodeType(firstNodeTemplateName).derivedFrom
104 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, true) -> {
105 enhanceStepTargets(name, workflow, firstNodeTemplateName, false)
107 derivedFrom.startsWith(BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, true) -> {
108 enhanceStepTargets(name, workflow, firstNodeTemplateName, true)
111 throw BluePrintProcessorException(
112 "couldn't execute workflow($name) step mapped " +
113 "to node template($firstNodeTemplateName) derived from($derivedFrom)"
119 private fun enhanceStepTargets(name: String, workflow: Workflow, nodeTemplateName: String, isDG: Boolean) {
121 val dependencyNodeTemplates: List<String>
123 val dgNodeTemplate = bluePrintContext.nodeTemplateByName(nodeTemplateName)
125 // Get the Dependent Component Node Template Names
126 val dependencyNodeTemplateNodes = dgNodeTemplate.properties?.get(PROPERTY_DEPENDENCY_NODE_TEMPLATES)
127 ?: throw BluePrintException("couldn't get property($PROPERTY_DEPENDENCY_NODE_TEMPLATES) ")
129 dependencyNodeTemplates =
130 JacksonUtils.getListFromJsonNode(dependencyNodeTemplateNodes, String::class.java)
132 dependencyNodeTemplates = listOf(nodeTemplateName)
135 log.info("workflow($name) dependent component NodeTemplates($dependencyNodeTemplates)")
137 // Check and Get Resource Assignment File
138 val resourceAssignmentArtifacts = dependencyNodeTemplates?.mapNotNull { componentNodeTemplateName ->
139 log.info("identified workflow($name) targets($componentNodeTemplateName)")
141 val resourceAssignmentArtifacts = bluePrintContext.nodeTemplateByName(componentNodeTemplateName)
143 it.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
145 log.info("resource assignment artifacts(${it.key}) for NodeType($componentNodeTemplateName)")
148 resourceAssignmentArtifacts
151 log.info("workflow($name) resource assignment files($resourceAssignmentArtifacts")
153 if (resourceAssignmentArtifacts != null && resourceAssignmentArtifacts.isNotEmpty()) {
155 // Add Workflow Dynamic Property
156 addWorkFlowDynamicPropertyDefinitions(name, workflow)
158 resourceAssignmentArtifacts.forEach { fileName ->
159 // Enhance Resource Assignment File
160 val resourceAssignmentProperties = enhanceResourceAssignmentFile(fileName!!)
161 // Add Workflow Dynamic DataType
162 addWorkFlowDynamicDataType(name, resourceAssignmentProperties)
167 // Enhancement for Dynamic Properties, Resource Assignment Properties, Resource Sources
168 private fun enhanceResourceAssignmentFile(fileName: String): MutableMap<String, PropertyDefinition> {
170 val filePath = "${bluePrintContext.rootPath}/$fileName"
172 log.info("enriching artifacts file($filePath")
174 val resourceAssignmentProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
176 val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
177 as? MutableList<ResourceAssignment>
178 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
180 val alreadyEnhancedKey = "enhanced-$fileName"
181 val alreadyEnhanced = bluePrintRuntimeService.check(alreadyEnhancedKey)
183 log.info("enhancing workflow resource mapping file($fileName) already enhanced($alreadyEnhanced)")
185 if (!alreadyEnhanced) {
186 // Call Resource Assignment Enhancer
187 resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintRuntimeService, resourceAssignments)
188 bluePrintRuntimeService.put(alreadyEnhancedKey, true.asJsonPrimitive())
191 resourceAssignments.forEach { resourceAssignment ->
192 resourceAssignmentProperties[resourceAssignment.name] = resourceAssignment.property!!
194 return resourceAssignmentProperties
197 private fun addWorkFlowDynamicPropertyDefinitions(name: String, workflow: Workflow) {
198 val dynamicPropertyName = "$name-properties"
199 val propertyDefinition = PropertyDefinition()
200 propertyDefinition.description = "Dynamic PropertyDefinition for workflow($name)."
201 propertyDefinition.type = "dt-$dynamicPropertyName"
202 propertyDefinition.required = true
203 // Add to Workflow Inputs
204 workflow.inputs?.put(dynamicPropertyName, propertyDefinition)
207 private fun addWorkFlowDynamicDataType(workflowName: String, mappingProperties: MutableMap<String, PropertyDefinition>) {
209 val dataTypeName = "dt-$workflowName-properties"
211 var dynamicDataType: DataType? = bluePrintContext.serviceTemplate.dataTypes?.get(dataTypeName)
213 if (dynamicDataType == null) {
214 log.info("dataType not present for the recipe({})", dataTypeName)
215 dynamicDataType = DataType()
216 dynamicDataType.version = "1.0.0"
217 dynamicDataType.description = "Dynamic DataType definition for workflow($workflowName)."
218 dynamicDataType.derivedFrom = BluePrintConstants.MODEL_TYPE_DATA_TYPE_DYNAMIC
220 val dataTypeProperties: MutableMap<String, PropertyDefinition> = hashMapOf()
221 dynamicDataType.properties = dataTypeProperties
223 // Overwrite WorkFlow DataType
224 bluePrintContext.serviceTemplate.dataTypes?.put(dataTypeName, dynamicDataType)
226 log.info("dynamic dataType($dataTypeName) already present for workflow($workflowName).")
228 // Merge all the Recipe Properties
229 mappingProperties.forEach { (propertyName, propertyDefinition) ->
230 dynamicDataType.properties?.put(propertyName, propertyDefinition)