From 5045877d046ebcddc80a8b83c14351a0d9fe6a49 Mon Sep 17 00:00:00 2001 From: "Muthuramalingam, Brinda Santh(bs2796)" Date: Fri, 14 Dec 2018 16:41:37 -0500 Subject: Add blueprint resource definition enrichment. Change-Id: I01234093028ffdc8bf1688e41baba20fae7da5ce Issue-ID: CCSDK-747 Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) --- .../enhancer/BluePrintEnhancerServiceImpl.kt | 31 ++++-- .../enhancer/ResourceDefinitionEnhancerService.kt | 124 +++++++++++++++++++++ 2 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt (limited to 'ms/controllerblueprints/modules/service/src/main/kotlin') 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 0b42a26e..6c7b6e08 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 @@ -24,6 +24,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhance 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.core.utils.BluePrintFileUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils import org.springframework.stereotype.Service @@ -31,37 +32,42 @@ import java.util.* @Service open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePrintRepoService, - private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService) : BluePrintEnhancerService { + private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService, + private val resourceDefinitionEnhancerService: ResourceDefinitionEnhancerService) : BluePrintEnhancerService { private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintEnhancerServiceImpl::class.toString()) override fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext { + // Copy the Blueprint Content to Target Location BluePrintFileUtils.copyBluePrint(basePath, enrichedBasePath) // Enhance the Blueprint - val enhancedBluePrintContext = enhance(enrichedBasePath) - - // Delete the Old Type files - BluePrintFileUtils.deleteBluePrintTypes(enrichedBasePath) - - // Write the Type File Definitions - BluePrintFileUtils.writeBluePrintTypes(enhancedBluePrintContext) - return enhancedBluePrintContext + return enhance(enrichedBasePath) } @Throws(BluePrintException::class) override fun enhance(basePath: String): BluePrintContext { + log.info("Enhancing blueprint($basePath)") - val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(UUID.randomUUID().toString(), basePath) + val blueprintRuntimeService = BluePrintMetadataUtils + .getBaseEnhancementBluePrintRuntime(UUID.randomUUID().toString(), basePath) + try { bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template", blueprintRuntimeService.bluePrintContext().serviceTemplate) + // Write the Type File Definitions + BluePrintFileUtils.writeBluePrintTypes(blueprintRuntimeService.bluePrintContext()) + + // Enhance Resource Dictionary + enhanceResourceDefinition(blueprintRuntimeService) + if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) { throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString()) } + } catch (e: Exception) { log.error("failed in blueprint enhancement", e) } @@ -69,5 +75,10 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePr return blueprintRuntimeService.bluePrintContext() } + private fun enhanceResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>) { + + resourceDefinitionEnhancerService.enhance(blueprintRuntimeService) + } + } 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 new file mode 100644 index 00000000..ab5ca74c --- /dev/null +++ b/ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt @@ -0,0 +1,124 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.apps.controllerblueprints.service.enhancer + +import com.att.eelf.configuration.EELFLogger +import com.att.eelf.configuration.EELFManager +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.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.springframework.stereotype.Service + +interface ResourceDefinitionEnhancerService { + + @Throws(BluePrintException::class) + fun enhance(bluePrintRuntimeService: BluePrintRuntimeService<*>) +} + +@Service +class ResourceDefinitionEnhancerServiceImpl(private val resourceDefinitionRepoService: ResourceDefinitionRepoService) : + ResourceDefinitionEnhancerService { + + private val log: EELFLogger = EELFManager.getInstance().getLogger(ResourceDefinitionEnhancerService::class.toString()) + + 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 + // 1. Get the Resource Mapping files from all NodeTemplates. + // 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<*>) { + + val blueprintContext = bluePrintRuntimeService.bluePrintContext() + + val mappingFiles = getAllResourceMappingFiles(blueprintContext) + log.info("resources assignment files ($mappingFiles)") + if (mappingFiles != null) { + getResourceDefinition(blueprintContext, mappingFiles) + } + } + + // Get all the Mapping files from all node templates. + private fun getAllResourceMappingFiles(blueprintContext: BluePrintContext): List? { + + return blueprintContext.nodeTemplates?.mapNotNull { nodeTemplateMap -> + + // Return only Mapping Artifact File Names + nodeTemplateMap.value.artifacts?.filter { artifactDefinitionMap -> + artifactDefinitionMap.value.type == ARTIFACT_TYPE_MAPPING_SOURCE + }?.mapNotNull { artifactDefinitionMap -> + artifactDefinitionMap.value.file + } + + }?.single { it.isNotEmpty() }?.distinct() + } + + // 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()) + } + + val distinctResourceAssignments = resourceAssignments.distinctBy { it.name } + generateResourceDictionaryFile(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) { + val resourcekeys = resourceAssignments.mapNotNull { it.dictionaryName }.distinct() + 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 -> + getResourceDefinition(resourceKey) + }.map { it.name to it }.toMap() + + // Recreate the Resource Definition File + ResourceDictionaryUtils.writeResourceDefinitionTypes(blueprintBasePath, resourceDefinitionMap) + log.info("resource definition file created successfully") + } + + // Get the Resource Definition from Database + private fun getResourceDefinition(name: String): ResourceDefinition { + return resourceDefinitionRepoService.getResourceDefinition(name) + } +} \ No newline at end of file -- cgit 1.2.3-korg