Fix enrichment for multiplicated assignments
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / designer-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / designer / api / enhancer / ResourceDefinitionEnhancerService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018 IBM.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.enhancer
19
20 import kotlinx.coroutines.Deferred
21 import kotlinx.coroutines.async
22 import kotlinx.coroutines.runBlocking
23 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.service.ResourceDefinitionRepoService
24 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.utils.BluePrintEnhancerUtils
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService
27 import org.onap.ccsdk.cds.controllerblueprints.core.logger
28 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
29 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
30 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
31 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
32 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
33 import org.springframework.stereotype.Service
34
35 interface ResourceDefinitionEnhancerService {
36
37     @Throws(BluePrintException::class)
38     fun enhance(
39         bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
40         bluePrintRuntimeService: BluePrintRuntimeService<*>
41     ): List<ResourceDefinition>
42 }
43
44 @Service
45 class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
46     ResourceDefinitionEnhancerService {
47
48     private val log = logger(ResourceDefinitionEnhancerService::class)
49
50     companion object {
51
52         const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
53     }
54
55     // Enhance the Resource Definition
56     // 1. Get the Resource Mapping files from all NodeTemplates.
57     // 2. Get all the Unique Resource assignments from all mapping files
58     // 3. Collect the Resource Definition for Resource Assignment names from database.
59     // 4. Create the Resource Definition under blueprint base path.
60     override fun enhance(
61         bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
62         bluePrintRuntimeService: BluePrintRuntimeService<*>
63     ): List<ResourceDefinition> {
64
65         var resourceDefinitions: List<ResourceDefinition> = mutableListOf()
66
67         val blueprintContext = bluePrintRuntimeService.bluePrintContext()
68
69         val mappingFiles = getAllResourceMappingFiles(blueprintContext)
70         log.info("resources assignment files ($mappingFiles)")
71         if (mappingFiles != null) {
72             resourceDefinitions = getResourceDefinition(blueprintContext, mappingFiles)
73             // Enriching Resource Definition Sources
74             enrichResourceDefinitionSources(bluePrintRuntimeService.bluePrintContext(), resourceDefinitions)
75         }
76         return resourceDefinitions
77     }
78
79     // Get all the Mapping files from all node templates.
80     private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List<String>? {
81
82         return blueprintContext.nodeTemplates()?.mapNotNull { nodeTemplateMap ->
83
84             // Return only Mapping Artifact File Names
85             nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap ->
86                 artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
87             }?.mapNotNull { artifactDefinitionMap ->
88                 artifactDefinitionMap.value.file
89             }
90         }?.flatten()?.distinct()
91     }
92
93     // Convert file content to ResourceAssignments asynchronously
94     private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List<String>) = runBlocking {
95         val blueprintBasePath = blueprintContext.rootPath
96         val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
97         for (file in files) {
98             log.info("processing file ($file)")
99             deferredResourceAssignments += async {
100                 ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
101             }
102         }
103
104         val resourceAssignments = mutableListOf<ResourceAssignment>()
105         for (deferredResourceAssignment in deferredResourceAssignments) {
106             resourceAssignments.addAll(deferredResourceAssignment.await())
107         }
108
109         generateResourceDictionary(blueprintBasePath, resourceAssignments)
110     }
111
112     // Read the Resource Definitions from the Database and write to type file.
113     private fun generateResourceDictionary(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>):
114         List<ResourceDefinition> {
115             val resourceKeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct().sorted()
116             log.info("distinct resource keys ($resourceKeys)")
117
118             // TODO("Optimise DB single Query to multiple Query")
119             return resourceKeys.map { resourceKey ->
120                 getResourceDefinition(resourceKey)
121             }
122         }
123
124     private fun enrichResourceDefinitionSources(
125         bluePrintContext: BluePrintContext,
126         resourceDefinitions: List<ResourceDefinition>
127     ) {
128         val sources = resourceDefinitions
129             .map { it.sources }
130             .map {
131                 it.values
132                     .map { nodeTemplate ->
133                         nodeTemplate.type
134                     }
135             }
136             .flatten().distinct()
137         log.info("Enriching Resource Definition sources Node Template: $sources")
138         sources.forEach {
139             BluePrintEnhancerUtils.populateNodeType(bluePrintContext, resourceDefinitionRepoService, it)
140         }
141     }
142
143     // Get the Resource Definition from Database
144     private fun getResourceDefinition(name: String): ResourceDefinition {
145         return resourceDefinitionRepoService.getResourceDefinition(name)
146     }
147 }