80967f29a0c0fa7bbdfdee02121eadbb097849bf
[ccsdk/cds.git] /
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.BluePrintError
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.DataType
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.Workflow
25 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService
26 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintWorkflowEnhancer
28 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
30 import org.onap.ccsdk.apps.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
34
35 @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: EELFLogger = EELFManager.getInstance().getLogger(BluePrintNodeTemplateEnhancerImpl::class.toString())
42
43     lateinit var bluePrintContext: BluePrintContext
44     lateinit var error: BluePrintError
45
46     private val workflowDataTypes: MutableMap<String, DataType> = hashMapOf()
47
48     override fun enhance(bluePrintContext: BluePrintContext, error: BluePrintError, name: String, workflow: Workflow) {
49         log.info("Enhancing Workflow($name)")
50         this.bluePrintContext = bluePrintContext
51         this.error = error
52
53         // Enrich Only for Resource Assignment and Dynamic Input Properties if any
54         //enhanceStepTargets(workflow)
55
56         // Enrich Workflow Inputs
57         //enhanceWorkflowInputs(name, workflow)
58     }
59
60     open fun enhanceWorkflowInputs(name: String, workflow: Workflow) {
61         val dynamicPropertyName = "$name-properties"
62         workflow.inputs?.let { inputs ->
63             // TODO("Filter Dynamic Properties")
64             bluePrintTypeEnhancerService.enhancePropertyDefinitions(bluePrintContext, error, inputs)
65         }
66     }
67
68     private fun enhanceStepTargets(workflow: Workflow) {
69
70         val workflowNodeTemplates = workflowTargets(workflow)
71
72         workflowNodeTemplates.forEach { nodeTemplate ->
73             val artifactFiles = bluePrintContext.nodeTemplateByName(nodeTemplate).artifacts?.filter {
74                 it.value.type == "artifact-mapping-resource"
75             }?.map {
76                 it.value.file
77             }
78
79             artifactFiles?.let { fileName ->
80                 val absoluteFilePath = "${bluePrintContext.rootPath}/$fileName"
81                 // Enhance Resource Assignment File
82                 enhanceResourceAssignmentFile(absoluteFilePath)
83
84             }
85         }
86     }
87
88     private fun workflowTargets(workflow: Workflow): List<String> {
89         return workflow.steps?.map {
90             it.value.target
91         }?.filterNotNull() ?: arrayListOf()
92     }
93
94     open fun enhanceResourceAssignmentFile(filePath: String) {
95         val resourceAssignments: MutableList<ResourceAssignment> = JacksonUtils.getListFromFile(filePath, ResourceAssignment::class.java)
96                 as? MutableList<ResourceAssignment>
97                 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
98         resourceAssignmentEnhancerService.enhanceBluePrint(bluePrintTypeEnhancerService, bluePrintContext, error, resourceAssignments)
99     }
100 }