Add blueprint resource definition enrichment.
authorMuthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>
Fri, 14 Dec 2018 21:41:37 +0000 (16:41 -0500)
committerMuthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>
Fri, 14 Dec 2018 21:41:37 +0000 (16:41 -0500)
Change-Id: I01234093028ffdc8bf1688e41baba20fae7da5ce
Issue-ID: CCSDK-747
Signed-off-by: Muthuramalingam, Brinda Santh(bs2796) <bs2796@att.com>
ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java [deleted file]
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/ResourceDefinitionEnhancerService.kt [new file with mode: 0644]
ms/controllerblueprints/modules/service/src/test/java/org/onap/ccsdk/apps/controllerblueprints/service/enhancer/BluePrintEnhancerServiceImplTest.java
ms/controllerblueprints/modules/service/src/test/resources/logback.xml

diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BluePrintEnhancerService.java
deleted file mode 100644 (file)
index 930c88d..0000000
+++ /dev/null
@@ -1,174 +0,0 @@
-/*\r
- * Copyright © 2017-2018 AT&T Intellectual Property.\r
- * Modifications Copyright © 2018 IBM.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *     http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.onap.ccsdk.apps.controllerblueprints.service;\r
-\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-import com.fasterxml.jackson.databind.JsonNode;\r
-import com.google.common.base.Preconditions;\r
-import org.apache.commons.collections.MapUtils;\r
-import org.apache.commons.lang3.StringUtils;\r
-import org.jetbrains.annotations.NotNull;\r
-import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
-import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;\r
-import org.onap.ccsdk.apps.controllerblueprints.core.data.*;\r
-import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
-import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;\r
-import org.onap.ccsdk.apps.controllerblueprints.service.enhancer.ResourceAssignmentEnhancerService;\r
-\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-\r
-/**\r
- * BluePrintEnhancerService\r
- *\r
- * @author Brinda Santh DATE : 8/8/2018\r
- */\r
-\r
-@Deprecated\r
-public class BluePrintEnhancerService {\r
-\r
-    private static EELFLogger log = EELFManager.getInstance().getLogger(BluePrintEnhancerService.class);\r
-\r
-    private ResourceAssignmentEnhancerService resourceAssignmentEnhancerService;\r
-\r
-    private Map<String, DataType> recipeDataTypes = new HashMap<>();\r
-\r
-\r
-    private void populateArtifactTemplateMappingDataType(@NotNull String nodeTemplateName, @NotNull NodeTemplate nodeTemplate)\r
-            throws BluePrintException {\r
-        log.info("****** Processing Artifact Node Template : {}", nodeTemplateName);\r
-\r
-        if (nodeTemplate.getProperties() != null) {\r
-\r
-            if (!nodeTemplate.getProperties().containsKey(ConfigModelConstant.PROPERTY_RECIPE_NAMES)) {\r
-                throw new BluePrintException("Node Template (" + nodeTemplateName + ") doesn't have "\r
-                        + ConfigModelConstant.PROPERTY_RECIPE_NAMES + " property.");\r
-            }\r
-\r
-            // Modified for ONAP converted Object to JsonNode\r
-            JsonNode recipeNames = nodeTemplate.getProperties().get(ConfigModelConstant.PROPERTY_RECIPE_NAMES);\r
-\r
-            log.info("Processing Recipe Names : {} ", recipeNames);\r
-\r
-            if (recipeNames != null && recipeNames.isArray() && recipeNames.size() > 0) {\r
-\r
-                Map<String, PropertyDefinition> mappingProperties =\r
-                        getCapabilityMappingProperties(nodeTemplateName, nodeTemplate);\r
-\r
-                for (JsonNode recipeNameNode : recipeNames) {\r
-                    String recipeName = recipeNameNode.textValue();\r
-                    processRecipe(nodeTemplateName, mappingProperties, recipeName);\r
-                }\r
-            }\r
-        }\r
-    }\r
-\r
-    private void processRecipe(@NotNull String nodeTemplateName, Map<String, PropertyDefinition> mappingProperties, String recipeName) {\r
-        if (StringUtils.isNotBlank(recipeName)) {\r
-            DataType recipeDataType = this.recipeDataTypes.get(recipeName);\r
-            if (recipeDataType == null) {\r
-                log.info("DataType not present for the recipe({})", recipeName);\r
-                recipeDataType = new DataType();\r
-                recipeDataType.setVersion("1.0.0");\r
-                recipeDataType.setDescription(\r
-                        "This is Dynamic Data type definition generated from resource mapping for the config template name "\r
-                                + nodeTemplateName + ".");\r
-                recipeDataType.setDerivedFrom(ConfigModelConstant.MODEL_TYPE_DATA_TYPE_DYNAMIC);\r
-                Map<String, PropertyDefinition> dataTypeProperties = new HashMap<>();\r
-                recipeDataType.setProperties(dataTypeProperties);\r
-            } else {\r
-                log.info("DataType Already present for the recipe({})", recipeName);\r
-            }\r
-\r
-            // Merge all the Recipe Properties\r
-            mergeDataTypeProperties(recipeDataType, mappingProperties);\r
-\r
-            // Overwrite Recipe DataType\r
-            this.recipeDataTypes.put(recipeName, recipeDataType);\r
-\r
-        }\r
-    }\r
-\r
-    private Map<String, PropertyDefinition> getCapabilityMappingProperties(String nodeTemplateName,\r
-                                                                           NodeTemplate nodeTemplate) throws BluePrintException {\r
-\r
-        Map<String, PropertyDefinition> dataTypeProperties = null;\r
-        if (nodeTemplate != null && MapUtils.isNotEmpty(nodeTemplate.getCapabilities())) {\r
-            CapabilityAssignment capability =\r
-                    nodeTemplate.getCapabilities().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING);\r
-\r
-            if (capability != null && capability.getProperties() != null) {\r
-\r
-                String resourceAssignmentContent = JacksonUtils\r
-                        .getJson(capability.getProperties().get(ConfigModelConstant.CAPABILITY_PROPERTY_MAPPING));\r
-\r
-                List<ResourceAssignment> resourceAssignments =\r
-                        JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment.class);\r
-\r
-                Preconditions.checkNotNull(resourceAssignments, "Failed to Processing Resource Mapping " + resourceAssignmentContent);\r
-                // Enhance Resource Assignment TODO("Plug Resource Assignment Enhancer Service")\r
-                //resourceAssignmentEnhancerService.enhanceBluePrint(this, resourceAssignments);\r
-\r
-                dataTypeProperties = new HashMap<>();\r
-\r
-                for (ResourceAssignment resourceAssignment : resourceAssignments) {\r
-                    if (resourceAssignment != null\r
-                            // && Boolean.valueOf(resourceAssignment.getInputParameter())\r
-                            && resourceAssignment.getProperty() != null\r
-                            && StringUtils.isNotBlank(resourceAssignment.getName())) {\r
-\r
-                        dataTypeProperties.put(resourceAssignment.getName(), resourceAssignment.getProperty());\r
-\r
-                    }\r
-                }\r
-\r
-            }\r
-        }\r
-        return dataTypeProperties;\r
-    }\r
-\r
-    private void mergeDataTypeProperties(DataType dataType, Map<String, PropertyDefinition> mergeProperties) {\r
-        if (dataType != null && dataType.getProperties() != null && mergeProperties != null) {\r
-            // Add the Other Template Properties\r
-            mergeProperties.forEach((mappingKey, propertyDefinition) -> dataType.getProperties().put(mappingKey, propertyDefinition));\r
-        }\r
-    }\r
-\r
-    private void populateRecipeInputs(ServiceTemplate serviceTemplate) {\r
-        if (serviceTemplate.getTopologyTemplate() != null\r
-                && MapUtils.isNotEmpty(serviceTemplate.getTopologyTemplate().getInputs())\r
-                && MapUtils.isNotEmpty(this.recipeDataTypes)\r
-                && MapUtils.isNotEmpty(serviceTemplate.getDataTypes())) {\r
-            this.recipeDataTypes.forEach((recipeName, recipeDataType) -> {\r
-                String dataTypePrefix = recipeName.replace("-action", "") + "-request";\r
-                String dataTypeName = "dt-" + dataTypePrefix;\r
-\r
-                serviceTemplate.getDataTypes().put(dataTypeName, recipeDataType);\r
-\r
-                PropertyDefinition customInputProperty = new PropertyDefinition();\r
-                customInputProperty.setDescription("This is Dynamic Data type for the receipe " + recipeName + ".");\r
-                customInputProperty.setRequired(Boolean.FALSE);\r
-                customInputProperty.setType(dataTypeName);\r
-                serviceTemplate.getTopologyTemplate().getInputs().put(dataTypePrefix, customInputProperty);\r
-\r
-            });\r
-        }\r
-    }\r
-}\r
index 0b42a26..6c7b6e0 100644 (file)
@@ -24,6 +24,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintEnhance
 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintRepoService\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.springframework.stereotype.Service\r
@@ -31,37 +32,42 @@ import java.util.*
 \r
 @Service\r
 open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePrintRepoService,\r
-                                        private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService) : BluePrintEnhancerService {\r
+                                        private val bluePrintTypeEnhancerService: BluePrintTypeEnhancerService,\r
+                                        private val resourceDefinitionEnhancerService: ResourceDefinitionEnhancerService) : BluePrintEnhancerService {\r
 \r
     private val log: EELFLogger = EELFManager.getInstance().getLogger(BluePrintEnhancerServiceImpl::class.toString())\r
 \r
     override fun enhance(basePath: String, enrichedBasePath: String): BluePrintContext {\r
+\r
         // Copy the Blueprint Content to Target Location\r
         BluePrintFileUtils.copyBluePrint(basePath, enrichedBasePath)\r
 \r
         // Enhance the Blueprint\r
-        val enhancedBluePrintContext = enhance(enrichedBasePath)\r
-\r
-        // Delete the Old Type files\r
-        BluePrintFileUtils.deleteBluePrintTypes(enrichedBasePath)\r
-\r
-        // Write the Type File Definitions\r
-        BluePrintFileUtils.writeBluePrintTypes(enhancedBluePrintContext)\r
-        return enhancedBluePrintContext\r
+        return enhance(enrichedBasePath)\r
     }\r
 \r
     @Throws(BluePrintException::class)\r
     override fun enhance(basePath: String): BluePrintContext {\r
+\r
         log.info("Enhancing blueprint($basePath)")\r
-        val blueprintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(UUID.randomUUID().toString(), basePath)\r
+        val blueprintRuntimeService = BluePrintMetadataUtils\r
+                .getBaseEnhancementBluePrintRuntime(UUID.randomUUID().toString(), basePath)\r
+\r
         try {\r
 \r
             bluePrintTypeEnhancerService.enhanceServiceTemplate(blueprintRuntimeService, "service_template",\r
                     blueprintRuntimeService.bluePrintContext().serviceTemplate)\r
 \r
+            // Write the Type File Definitions\r
+            BluePrintFileUtils.writeBluePrintTypes(blueprintRuntimeService.bluePrintContext())\r
+\r
+            // Enhance Resource Dictionary\r
+            enhanceResourceDefinition(blueprintRuntimeService)\r
+\r
             if (blueprintRuntimeService.getBluePrintError().errors.isNotEmpty()) {\r
                 throw BluePrintException(blueprintRuntimeService.getBluePrintError().errors.toString())\r
             }\r
+\r
         } catch (e: Exception) {\r
             log.error("failed in blueprint enhancement", e)\r
         }\r
@@ -69,5 +75,10 @@ open class BluePrintEnhancerServiceImpl(private val bluePrintRepoService: BluePr
         return blueprintRuntimeService.bluePrintContext()\r
     }\r
 \r
+    private fun enhanceResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>) {\r
+\r
+        resourceDefinitionEnhancerService.enhance(blueprintRuntimeService)\r
+    }\r
+\r
 }\r
 \r
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 (file)
index 0000000..ab5ca74
--- /dev/null
@@ -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<String>? {
+
+        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<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())
+            }
+
+            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<ResourceAssignment>) {
+        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
index 7d9c2e1..06f2f90 100644 (file)
@@ -67,7 +67,7 @@ public class BluePrintEnhancerServiceImplTest {
         Assert.assertNotNull("failed to get blueprintContext ", bluePrintContext);
 
         // Validate the Generated BluePrints
-
-        bluePrintValidatorService.validateBluePrints(targetPath);
+        Boolean valid = bluePrintValidatorService.validateBluePrints(targetPath);
+        Assert.assertTrue("blueprint validation failed ", valid);
     }
 }
\ No newline at end of file
index 7b7ef75..fc1f669 100644 (file)
@@ -17,7 +17,7 @@
 
 <configuration>
 
-    <property name="localPattern" value="%d{HH:mm:ss.SSS} %-5level %logger{100} - %msg%n" />
+    <property name="localPattern" value="[%-8thread] %d{HH:mm:ss.SSS} %-5level %logger{100} - %msg%n" />
 
     <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
         <!-- encoders are assigned the type