Change resource definition population 71/81871/1
authorMuthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
Fri, 8 Mar 2019 19:25:20 +0000 (14:25 -0500)
committerMuthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
Fri, 8 Mar 2019 19:25:20 +0000 (14:25 -0500)
Change-Id: I95d3bcddb6b2027a523ca7411d0c6c7f98fa0f63
Issue-ID: CCSDK-1110
Signed-off-by: Muthuramalingam, Brinda Santh <brindasanth@in.ibm.com>
ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/utils/ResourceDictionaryUtils.kt
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImpl.kt
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintNodeTemplateEnhancerImpl.kt
ms/controllerblueprints/modules/service/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/ResourceDefinitionEnhancerService.kt

index 1aeda0b..69ee1cf 100644 (file)
@@ -85,6 +85,12 @@ object ResourceDictionaryUtils {
                 ?: throw BluePrintProcessorException("couldn't get ResourceAssignment definitions for the file($filePath)")
     }
 
+    fun writeResourceDefinitionTypes(basePath: String, resourceDefinitions: List<ResourceDefinition>) {
+        val resourceDefinitionMap = resourceDefinitions.map { it.name to it }.toMap()
+        writeResourceDefinitionTypes(basePath, resourceDefinitionMap)
+
+    }
+
     fun writeResourceDefinitionTypes(basePath: String, resourceDefinitionMap: Map<String, ResourceDefinition>) {
         val typePath = basePath.plus(File.separator).plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR)
                 .plus(File.separator).plus("${ResourceDictionaryConstants.PATH_RESOURCE_DEFINITION_TYPE}.json")
index 63171de..fb49dc4 100644 (file)
@@ -23,9 +23,9 @@ import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhancerService\r
 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintTypeEnhancerService\r
 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext\r
-import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService\r
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils\r
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils\r
+import org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils.ResourceDictionaryUtils\r
 import org.springframework.stereotype.Service\r
 import java.util.*\r
 \r
@@ -56,11 +56,15 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintTypeEnhancerService
             bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template",\r
                     blueprintRuntimeService.bluePrintContext().serviceTemplate)\r
 \r
+            log.info("##### Enhancing blueprint Resource Definitions")\r
+            val resourceDefinitions = resourceDefinitionEnhancerService.enhance(bluePrintTypeEnhancerService,\r
+                    blueprintRuntimeService)\r
+\r
             // Write the Enhanced Blueprint Definitions\r
             BluePrintFileUtils.writeEnhancedBluePrint(blueprintRuntimeService.bluePrintContext())\r
 \r
-            // Enhance Resource Dictionary\r
-            enhanceResourceDefinition(blueprintRuntimeService)\r
+            // Write the Enhanced Blueprint Resource Definitions\r
+            ResourceDictionaryUtils.writeResourceDefinitionTypes(basePath, resourceDefinitions)\r
 \r
             if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) {\r
                 throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString())\r
@@ -73,10 +77,5 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintTypeEnhancerService
         return blueprintRuntimeService.bluePrintContext()\r
     }\r
 \r
-    private fun enhanceResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>) {\r
-        log.info("##### Enhancing blueprint Resource Definitions")\r
-        resourceDefinitionEnhancerService.enhance(blueprintRuntimeService)\r
-    }\r
-\r
 }\r
 \r
index fb6c06a..0765f90 100644 (file)
@@ -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 ->
index 43eb019..6171687 100644 (file)
@@ -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<ResourceDefinition>
 }
 
 @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<ResourceDefinition> {
+
+        var resourceDefinitions: List<ResourceDefinition> = 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<String>) {
-        runBlocking {
-            val blueprintBasePath = blueprintContext.rootPath
-            val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
-            for (file in files) {
-                log.info("processing file ($file)")
-                deferredResourceAssignments += async {
-                    ResourceDictionaryUtils.getResourceAssignmentFromFile("$blueprintBasePath/$file")
-                }
-            }
-
-            val resourceAssignments = mutableListOf<ResourceAssignment>()
-            for (deferredResourceAssignment in deferredResourceAssignments) {
-                resourceAssignments.addAll(deferredResourceAssignment.await())
+    private fun getResourceDefinition(blueprintContext: BluePrintContext, files: List<String>) = runBlocking {
+        val blueprintBasePath = blueprintContext.rootPath
+        val deferredResourceAssignments = mutableListOf<Deferred<List<ResourceAssignment>>>()
+        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<ResourceAssignment>()
+        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<ResourceAssignment>) {
+    private fun generateResourceDictionary(blueprintBasePath: String, resourceAssignments: List<ResourceAssignment>)
+            : List<ResourceDefinition> {
         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<ResourceDefinition>) {
+        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