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