2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.service.enhancer
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import kotlinx.coroutines.Deferred
22 import kotlinx.coroutines.async
23 import kotlinx.coroutines.runBlocking
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
25 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
28 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition
29 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils
30 import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService
31 import org.springframework.stereotype.Service
33 interface ResourceDefinitionEnhancerService {
35 @Throws(BluePrintException::class)
36 fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>)
40 class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) :
41 ResourceDefinitionEnhancerService {
43 private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionEnhancerService::class.toString())
46 const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource"
47 const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates"
50 // Enhance the Resource Definition
51 // 1. Get the Resource Mapping files from all NodeTemplates.
52 // 2. Get all the Unique Resource assignments from all mapping files
53 // 3. Collect the Resource Definition for Resource Assignment names from database.
54 // 4. Create the Resource Definition under blueprint base path.
55 override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) {
57 val blueprintContext = bluePrintRuntimeService.bluePrintContext()
59 val mappingFiles = getAllResourceMappingFiles(blueprintContext)
60 log.info("resources assignment files ($mappingFiles)")
61 if (mappingFiles != null) {
62 getResourceDefinition(blueprintContext, mappingFiles)
66 // Get all the Mapping files from all node templates.
67 private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List<String>? {
69 return blueprintContext.nodeTemplates?.mapNotNull { nodeTemplateMap ->
71 // Return only Mapping Artifact File Names
72 nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap ->
73 artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE
74 }?.mapNotNull { artifactDefinitionMap ->
75 artifactDefinitionMap.value.file
78 }?.single { it.isNotEmpty() }?.distinct()
81 // Convert file content to ResourceAssignments asynchronously
82 private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List<String>) {
84 val blueprintBasePath = blueprintContext.rootPath
85 val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
87 log.info("processing file ($file)")
88 deferredResourceAssignments += async {
89 ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
93 val resourceAssignments = mutableListOf<ResourceAssignment>()
94 for (deferredResourceAssignment in deferredResourceAssignments) {
95 resourceAssignments.addAll(deferredResourceAssignment.await())
98 val distinctResourceAssignments = resourceAssignments.distinctBy { it.name }
99 generateResourceDictionaryFile(blueprintBasePath, distinctResourceAssignments)
100 //log.info("distinct Resource assignment ($distinctResourceAssignments)")
104 // Read the Resource Definitions from the Database and write to type file.
105 private fun generateResourceDictionaryFile(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>) {
106 val resourcekeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct()
107 log.info("distinct resource keys ($resourcekeys)")
109 //TODO("Optimise DB single Query to multiple Query")
110 // Collect the Resource Definition from database and convert to map to save in file
111 val resourceDefinitionMap = resourcekeys.map { resourceKey ->
112 getResourceDefinition(resourceKey)
113 }.map { it.name to it }.toMap()
115 // Recreate the Resource Definition File
116 ResourceDictionaryUtils.writeResourceDefinitionTypes(blueprintBasePath, resourceDefinitionMap)
117 log.info("resource definition file created successfully")
120 // Get the Resource Definition from Database
121 private fun getResourceDefinition(name: String): ResourceDefinition {
122 return resourceDefinitionRepoService.getResourceDefinition(name)