2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 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 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
35 interface ResourceDefinitionEnhancerService {
37 @Throws(BluePrintException::class)
39 bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
40 bluePrintRuntimeService: BluePrintRuntimeService<*>
41 ): List<ResourceDefinition>
45 class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
46 ResourceDefinitionEnhancerService {
48 private val log = logger(ResourceDefinitionEnhancerService::class)
52 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
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.
61 bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
62 bluePrintRuntimeService: BluePrintRuntimeService<*>
63 ): List<ResourceDefinition> {
65 var resourceDefinitions: List<ResourceDefinition> = mutableListOf()
67 val blueprintContext = bluePrintRuntimeService.bluePrintContext()
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)
76 return resourceDefinitions
79 // Get all the Mapping files from all node templates.
80 private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List<String>? {
82 return blueprintContext.nodeTemplates()?.mapNotNull { nodeTemplateMap ->
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
90 }?.flatten()?.distinct()
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>>>()
98 log.info("processing file ($file)")
99 deferredResourceAssignments += async {
100 ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
104 val resourceAssignments = mutableListOf<ResourceAssignment>()
105 for (deferredResourceAssignment in deferredResourceAssignments) {
106 resourceAssignments.addAll(deferredResourceAssignment.await())
109 val distinctResourceAssignments = resourceAssignments.distinctBy { it.name }
110 generateResourceDictionary(blueprintBasePath, distinctResourceAssignments)
111 // log.info("distinct Resource assignment ($distinctResourceAssignments)")
114 // Read the Resource Definitions from the Database and write to type file.
115 private fun generateResourceDictionary(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>):
116 List<ResourceDefinition> {
117 val resourceKeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct().sorted()
118 log.info("distinct resource keys ($resourceKeys)")
120 // TODO("Optimise DB single Query to multiple Query")
121 return resourceKeys.map { resourceKey ->
122 getResourceDefinition(resourceKey)
126 private fun enrichResourceDefinitionSources(
127 bluePrintContext: BluePrintContext,
128 resourceDefinitions: List<ResourceDefinition>
130 val sources = resourceDefinitions
134 .map { nodeTemplate ->
138 .flatten().distinct()
139 log.info("Enriching Resource Definition sources Node Template: $sources")
141 BluePrintEnhancerUtils.populateNodeType(bluePrintContext, resourceDefinitionRepoService, it)
145 // Get the Resource Definition from Database
146 private fun getResourceDefinition(name: String): ResourceDefinition {
147 return resourceDefinitionRepoService.getResourceDefinition(name)