From b2717e28c9e4a2d2531ff05571c04376c3b05bf7 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh" Date: Fri, 8 Mar 2019 14:25:20 -0500 Subject: Change resource definition population Change-Id: I95d3bcddb6b2027a523ca7411d0c6c7f98fa0f63 Issue-ID: CCSDK-1110 Signed-off-by: Muthuramalingam, Brinda Santh --- .../enhancer/BluePrintEnhancerServiceImpl.kt | 15 ++--- .../enhancer/BluePrintNodeTemplateEnhancerImpl.kt | 17 +---- .../enhancer/ResourceDefinitionEnhancerService.kt | 76 ++++++++++++++-------- 3 files changed, 58 insertions(+), 50 deletions(-) (limited to 'ms/controllerblueprints/modules/service') diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt index 63171de6..fb49dc46 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt @@ -23,9 +23,9 @@ import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext -import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils +import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils import org.springframework.stereotype.Service import java.util.* @@ -56,11 +56,15 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintTypeEnhancerService bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template", blueprintRuntimeService.bluePrintContext().serviceTemplate) + log.info("##### Enhancing blueprint Resource Definitions") + val resourceDefinitions = resourceDefinitionEnhancerService.enhance(bluePrintTypeEnhancerService, + blueprintRuntimeService) + // Write the Enhanced Blueprint Definitions BluePrintFileUtils.writeEnhancedBluePrint(blueprintRuntimeService.bluePrintContext()) - // Enhance Resource Dictionary - enhanceResourceDefinition(blueprintRuntimeService) + // Write the Enhanced Blueprint Resource Definitions + ResourceDictionaryUtils.writeResourceDefinitionTypes(basePath, resourceDefinitions) if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) { throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString()) @@ -73,10 +77,5 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintTypeEnhancerService return blueprintRuntimeService.bluePrintContext() } - private fun enhanceResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>) { - log.info("##### Enhancing blueprint Resource Definitions") - resourceDefinitionEnhancerService.enhance(blueprintRuntimeService) - } - } diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintNodeTemplateEnhancerImpl.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintNodeTemplateEnhancerImpl.kt index fb6c06af..0765f903 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintNodeTemplateEnhancerImpl.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintNodeTemplateEnhancerImpl.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 IBM. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,15 +19,13 @@ package org.onap.ccsdk.apps.controllerblueprints.service.enhancer import com.att.eelf.configuration.EELFLogger import com.att.eelf.configuration.EELFManager -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate -import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType -import org.onap.ccsdk.apps.controllerblueprints.core.format import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintNodeTemplateEnhancer import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService +import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.annotation.Scope import org.springframework.stereotype.Service @@ -51,7 +50,7 @@ open class BluePrintNodeTemplateEnhancerImpl(private val bluePrintRepoService: B val nodeTypeName = nodeTemplate.type // Get NodeType from Repo and Update Service Template - val nodeType = populateNodeType(nodeTypeName) + val nodeType = BluePrintEnhancerUtils.populateNodeType(bluePrintContext, bluePrintRepoService, nodeTypeName) // Enrich NodeType bluePrintTypeEnhancerService.enhanceNodeType(bluePrintRuntimeService, nodeTypeName, nodeType) @@ -60,16 +59,6 @@ open class BluePrintNodeTemplateEnhancerImpl(private val bluePrintRepoService: B enhanceNodeTemplateArtifactDefinition(name, nodeTemplate) } - - open fun populateNodeType(nodeTypeName: String): NodeType { - - val nodeType = bluePrintContext.serviceTemplate.nodeTypes?.get(nodeTypeName) - ?: bluePrintRepoService.getNodeType(nodeTypeName) - ?: throw BluePrintException(format("Couldn't get NodeType({}) from repo.", nodeTypeName)) - bluePrintContext.serviceTemplate.nodeTypes?.put(nodeTypeName, nodeType) - return nodeType - } - open fun enhanceNodeTemplateArtifactDefinition(nodeTemplateName: String, nodeTemplate: NodeTemplate) { nodeTemplate.artifacts?.forEach { artifactDefinitionName, artifactDefinition -> diff --git a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt index 43eb019e..6171687f 100644 --- a/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt @@ -23,18 +23,21 @@ import kotlinx.coroutines.Deferred import kotlinx.coroutines.async import kotlinx.coroutines.runBlocking import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils import org.onap.ccsdk.apps.controllerblueprints.service.ResourceDefinitionRepoService +import org.onap.ccsdk.apps.controllerblueprints.service.utils.BluePrintEnhancerUtils import org.springframework.stereotype.Service interface ResourceDefinitionEnhancerService { @Throws(BluePrintException::class) - fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) + fun enhance(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService, + bluePrintRuntimeService: BluePrintRuntimeService<*>): List } @Service @@ -45,7 +48,6 @@ class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoSe companion object { const val ARTIFACT_TYPE_MAPPING_SOURCE: String = "artifact-mapping-resource" - const val PROPERTY_DEPENDENCY_NODE_TEMPLATES = "dependency-node-templates" } // Enhance the Resource Definition @@ -53,15 +55,21 @@ class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoSe // 2. Get all the Unique Resource assignments from all mapping files // 3. Collect the Resource Definition for Resource Assignment names from database. // 4. Create the Resource Definition under blueprint base path. - override fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) { + override fun enhance(bluePrintTypeEnhancerService: BluePrintTypeEnhancerService, + bluePrintRuntimeService: BluePrintRuntimeService<*>): List { + + var resourceDefinitions: List = mutableListOf() val blueprintContext = bluePrintRuntimeService.bluePrintContext() val mappingFiles = getAllResourceMappingFiles(blueprintContext) log.info("resources assignment files ($mappingFiles)") if (mappingFiles != null) { - getResourceDefinition(blueprintContext, mappingFiles) + resourceDefinitions = getResourceDefinition(blueprintContext, mappingFiles) + // Enriching Resource Definition Sources + enrichResourceDefinitionSources(bluePrintRuntimeService.bluePrintContext(), resourceDefinitions) } + return resourceDefinitions } // Get all the Mapping files from all node templates. @@ -80,42 +88,54 @@ class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoSe } // Convert file content to ResourceAssignments asynchronously - private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List) { - runBlocking { - val blueprintBasePath = blueprintContext.rootPath - val deferredResourceAssignments = mutableListOf>>() - for (file in files) { - log.info("processing file ($file)") - deferredResourceAssignments += async { - ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file") - } - } - - val resourceAssignments = mutableListOf() - for (deferredResourceAssignment in deferredResourceAssignments) { - resourceAssignments.addAll(deferredResourceAssignment.await()) + private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List) = runBlocking { + val blueprintBasePath = blueprintContext.rootPath + val deferredResourceAssignments = mutableListOf>>() + for (file in files) { + log.info("processing file ($file)") + deferredResourceAssignments += async { + ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file") } + } - val distinctResourceAssignments = resourceAssignments.distinctBy { it.name } - generateResourceDictionaryFile(blueprintBasePath, distinctResourceAssignments) - //log.info("distinct Resource assignment ($distinctResourceAssignments)") + val resourceAssignments = mutableListOf() + for (deferredResourceAssignment in deferredResourceAssignments) { + resourceAssignments.addAll(deferredResourceAssignment.await()) } + + val distinctResourceAssignments = resourceAssignments.distinctBy { it.name } + generateResourceDictionary(blueprintBasePath, distinctResourceAssignments) + //log.info("distinct Resource assignment ($distinctResourceAssignments)") } + // Read the Resource Definitions from the Database and write to type file. - private fun generateResourceDictionaryFile(blueprintBasePath: String, resourceAssignments: List) { + private fun generateResourceDictionary(blueprintBasePath: String, resourceAssignments: List) + : List { val resourceKeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct().sorted() log.info("distinct resource keys ($resourceKeys)") //TODO("Optimise DB single Query to multiple Query") - // Collect the Resource Definition from database and convert to map to save in file - val resourceDefinitionMap = resourceKeys.map { resourceKey -> + return resourceKeys.map { resourceKey -> getResourceDefinition(resourceKey) - }.map { it.name to it }.toMap() + } + } - // Recreate the Resource Definition File - ResourceDictionaryUtils.writeResourceDefinitionTypes(blueprintBasePath, resourceDefinitionMap) - log.info("resource definition file created successfully") + private fun enrichResourceDefinitionSources(bluePrintContext: BluePrintContext, + resourceDefinitions: List) { + val sources = resourceDefinitions + .map { it.sources } + .map { + it.values + .map { nodeTemplate -> + nodeTemplate.type + } + } + .flatten().distinct() + log.info("Enriching Resource Definition sources Node Template: $sources") + sources.forEach { + BluePrintEnhancerUtils.populateNodeType(bluePrintContext, resourceDefinitionRepoService, it) + } } // Get the Resource Definition from Database -- cgit 1.2.3-korg