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.apps.controllerblueprints.service.enhancer
20 import com.att.eelf.configuration.EELFLogger
21 import com.att.eelf.configuration.EELFManager
22 import kotlinx.coroutines.Deferred
23 import kotlinx.coroutines.async
24 import kotlinx.coroutines.runBlocking
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
27 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
30 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
31 import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService
32 import org.springframework.stereotype.Service
34 interface ResourceDefinitionEnhancerService {
36 @Throws(BluePrintException::class)
37 fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>)
41 class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
42 ResourceDefinitionEnhancerService {
44 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionEnhancerService::class.toString())
47 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
48 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
51 // Enhance the Resource Definition
52 // 1. Get the Resource Mapping files from all NodeTemplates.
53 // 2. Get all the Unique Resource assignments from all mapping files
54 // 3. Collect the Resource Definition for Resource Assignment names from database.
55 // 4. Create the Resource Definition under blueprint base path.
56 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) {
58 val blueprintContext = bluePrintRuntimeService.bluePrintContext()
60 val mappingFiles = getAllResourceMappingFiles(blueprintContext)
61 log.info("resources assignment files ($mappingFiles)")
62 if (mappingFiles != null) {
63 getResourceDefinition(blueprintContext, mappingFiles)
67 // Get all the Mapping files from all node templates.
68 private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List<String>? {
70 return blueprintContext.nodeTemplates?.mapNotNull { nodeTemplateMap ->
72 // Return only Mapping Artifact File Names
73 nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap ->
74 artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
75 }?.mapNotNull { artifactDefinitionMap ->
76 artifactDefinitionMap.value.file
79 }?.flatten()?.distinct()
82 // Convert file content to ResourceAssignments asynchronously
83 private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List<String>) {
85 val blueprintBasePath = blueprintContext.rootPath
86 val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
88 log.info("processing file ($file)")
89 deferredResourceAssignments += async {
90 ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
94 val resourceAssignments = mutableListOf<ResourceAssignment>()
95 for (deferredResourceAssignment in deferredResourceAssignments) {
96 resourceAssignments.addAll(deferredResourceAssignment.await())
99 val distinctResourceAssignments = resourceAssignments.distinctBy { it.name }
100 generateResourceDictionaryFile(blueprintBasePath, distinctResourceAssignments)
101 //log.info("distinct Resource assignment ($distinctResourceAssignments)")
105 // Read the Resource Definitions from the Database and write to type file.
106 private fun generateResourceDictionaryFile(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>) {
107 val resourceKeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct().sorted()
108 log.info("distinct resource keys ($resourceKeys)")
110 //TODO("Optimise DB single Query to multiple Query")
111 // Collect the Resource Definition from database and convert to map to save in file
112 val resourceDefinitionMap = resourceKeys.map { resourceKey ->
113 getResourceDefinition(resourceKey)
114 }.map { it.name to it }.toMap()
116 // Recreate the Resource Definition File
117 ResourceDictionaryUtils.writeResourceDefinitionTypes(blueprintBasePath, resourceDefinitionMap)
118 log.info("resource definition file created successfully")
121 // Get the Resource Definition from Database
122 private fun getResourceDefinition(name: String): ResourceDefinition {
123 return resourceDefinitionRepoService.getResourceDefinition(name)