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.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
35 interface ResourceDefinitionEnhancerService {
37 @Throws(BluePrintException::class)
38 fun enhance(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,
39 bluePrintRuntimeService: BluePrintRuntimeService<*>): List<ResourceDefinition>
43 class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
44 ResourceDefinitionEnhancerService {
46 private val log = logger(ResourceDefinitionEnhancerService::class)
49 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
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> {
60 var resourceDefinitions: List<ResourceDefinition> = mutableListOf()
62 val blueprintContext = bluePrintRuntimeService.bluePrintContext()
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)
71 return resourceDefinitions
74 // Get all the Mapping files from all node templates.
75 private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List<String>? {
77 return blueprintContext.nodeTemplates()?.mapNotNull { nodeTemplateMap ->
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
86 }?.flatten()?.distinct()
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>>>()
94 log.info("processing file ($file)")
95 deferredResourceAssignments += async {
96 ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
100 val resourceAssignments = mutableListOf<ResourceAssignment>()
101 for (deferredResourceAssignment in deferredResourceAssignments) {
102 resourceAssignments.addAll(deferredResourceAssignment.await())
105 val distinctResourceAssignments = resourceAssignments.distinctBy { it.name }
106 generateResourceDictionary(blueprintBasePath, distinctResourceAssignments)
107 //log.info("distinct Resource assignment ($distinctResourceAssignments)")
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)")
117 //TODO("Optimise DB single Query to multiple Query")
118 return resourceKeys.map { resourceKey ->
119 getResourceDefinition(resourceKey)
123 private fun enrichResourceDefinitionSources(bluePrintContext: BluePrintContext,
124 resourceDefinitions: List<ResourceDefinition>) {
125 val sources = resourceDefinitions
129 .map { nodeTemplate ->
133 .flatten().distinct()
134 log.info("Enriching Resource Definition sources Node Template: $sources")
136 BluePrintEnhancerUtils.populateNodeType(bluePrintContext, resourceDefinitionRepoService, it)
140 // Get the Resource Definition from Database
141 private fun getResourceDefinition(name: String): ResourceDefinition {
142 return resourceDefinitionRepoService.getResourceDefinition(name)