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