Merge "Map proto to pojo. implement blueprint processing"
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceAssignmentProcessor.kt
index 1c5455f..e389f36 100644
--- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceAssignmentProcessor.kt
+++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceAssignmentProcessor.kt
@@ -17,24 +17,50 @@
 
 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor
 
+import com.fasterxml.jackson.databind.node.NullNode
+import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
+import org.slf4j.LoggerFactory
 import org.springframework.stereotype.Service
 
 /**
  * DefaultResourceAssignmentProcessor
  *
- * @author Brinda Santh
+ * @author Kapil Singal
  */
 @Service("resource-assignment-processor-default")
 open class DefaultResourceAssignmentProcessor : ResourceAssignmentProcessor() {
 
+    private val logger = LoggerFactory.getLogger(DefaultResourceAssignmentProcessor::class.java)
+
     override fun getName(): String {
         return "resource-assignment-processor-default"
     }
 
-    override fun process(executionRequest: ResourceAssignment) {
+    override fun process(resourceAssignment: ResourceAssignment) {
+        try {
+            // Check if It has Input
+            var value: Any? = raRuntimeService.getInputValue(resourceAssignment.name)
+
+            // If value is null get it from default source
+            if (value == null || value is NullNode) {
+                logger.info("Looking for defaultValue as couldn't find value in input For template key (${resourceAssignment.name})")
+                value = resourceAssignment.property?.defaultValue
+            }
+
+            logger.info("For template key (${resourceAssignment.name}) setting value as ($value)")
+            ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
+
+            // Check the value has populated for mandatory case
+            ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
+        } catch (e: Exception) {
+            ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
+            throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
+        }
+
     }
 
-    override fun recover(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
+    override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
     }
 }
\ No newline at end of file
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/PrimaryDataResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/PrimaryDataResourceAssignmentProcessor.kt
index 2b3270e..9427183 100644
--- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/PrimaryDataResourceAssignmentProcessor.kt
+++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/PrimaryDataResourceAssignmentProcessor.kt
@@ -33,7 +33,7 @@
 /**
  * PrimaryDataResourceAssignmentProcessor
  *
- * @author Brinda Santh
+ * @author Kapil Singal
  */
 @Service("resource-assignment-processor-primary-db")
 open class PrimaryDataResourceAssignmentProcessor(private val primaryDBLibGenericService: PrimaryDBLibGenericService)
@@ -54,27 +54,27 @@
             if (value != null && value !is NullNode) {
                 logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
-                return
-            }
-
-            val dName = resourceAssignment.dictionaryName
-            val dSource = resourceAssignment.dictionarySource
-            val resourceDefinition = resourceDictionaries[dName]
-                    ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
-            val resourceSource = resourceDefinition.sources[dSource]
-                    ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
-            val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
-            val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
-            val sql = checkNotNull(sourceProperties.query) { "failed to get request query for $dName under $dSource properties" }
-            val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
-
-            logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
-
-            val rows = primaryDBLibGenericService.query(sql, populateNamedParameter(inputKeyMapping))
-            if (rows.isNullOrEmpty()) {
-                logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)")
             } else {
-                processDBResults(resourceAssignment, sourceProperties, rows)
+                val dName = resourceAssignment.dictionaryName
+                val dSource = resourceAssignment.dictionarySource
+                val resourceDefinition = resourceDictionaries[dName]
+                        ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
+                val resourceSource = resourceDefinition.sources[dSource]
+                        ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
+                val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
+                val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java)
+
+                val sql = checkNotNull(sourceProperties.query) { "failed to get request query for $dName under $dSource properties" }
+                val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
+
+                logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
+
+                val rows = primaryDBLibGenericService.query(sql, populateNamedParameter(inputKeyMapping))
+                if (rows.isNullOrEmpty()) {
+                    logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)")
+                } else {
+                    populateResource(resourceAssignment, sourceProperties, rows)
+                }
             }
 
             // Check the value has populated for mandatory case
@@ -106,7 +106,7 @@
     }
 
     @Throws(BluePrintProcessorException::class)
-    private fun processDBResults(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) {
+    private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) {
         val dName = resourceAssignment.dictionaryName
         val dSource = resourceAssignment.dictionarySource
         val type = nullToEmpty(resourceAssignment.property?.type)
@@ -115,21 +115,17 @@
         logger.info("Response processing type($type)")
 
         // Primitive Types
-        if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
-            val dbColumnValue = rows[0][outputKeyMapping[dName]]
-            ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, dbColumnValue)
-        }
-        // Array Types
-        else if (BluePrintTypes.validCollectionTypes().contains(type)) {
-            lateinit var entrySchemaType: String
-            if (resourceAssignment.property?.entrySchema != null) {
-                entrySchemaType = nullToEmpty(resourceAssignment.property?.entrySchema?.type)
+        when(type) {
+            in BluePrintTypes.validPrimitiveTypes() -> {
+                val dbColumnValue = rows[0][outputKeyMapping[dName]]
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($dbColumnValue)")
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, dbColumnValue)
             }
-
-            if (checkNotEmptyOrThrow(entrySchemaType, "Entry schema is not defined for dictionary ($dName) info")) {
-                val arrayNode = JsonNodeFactory.instance.arrayNode()
+            in BluePrintTypes.validCollectionTypes() -> {
+                val entrySchemaType = returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
+                var arrayNode = JsonNodeFactory.instance.arrayNode()
                 rows.forEach {
-                    if (BluePrintTypes.validPrimitiveTypes().contains(entrySchemaType)) {
+                    if (entrySchemaType in BluePrintTypes.validPrimitiveTypes()) {
                         val dbColumnValue = it[outputKeyMapping[dName]]
                         // Add Array JSON
                         JacksonUtils.populatePrimitiveValues(dbColumnValue!!, entrySchemaType, arrayNode)
@@ -143,19 +139,22 @@
                         arrayNode.add(arrayChildNode)
                     }
                 }
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
                 // Set the List of Complex Values
                 ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
             }
-        } else {
-            // Complex Types
-            val row = rows[0]
-            val objectNode = JsonNodeFactory.instance.objectNode()
-            for (mapping in outputKeyMapping.entries) {
-                val dbColumnValue = checkNotNull(row[mapping.key])
-                val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, type, mapping.key)
-                JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, objectNode)
+            else -> {
+                // Complex Types
+                val row = rows[0]
+                var objectNode = JsonNodeFactory.instance.objectNode()
+                for (mapping in outputKeyMapping.entries) {
+                    val dbColumnValue = checkNotNull(row[mapping.key])
+                    val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, type, mapping.key)
+                    JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, objectNode)
+                }
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
             }
-            ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
         }
     }
 
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/SimpleRestResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/SimpleRestResourceAssignmentProcessor.kt
index e05261d..f1a4dbb 100644
--- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/SimpleRestResourceAssignmentProcessor.kt
+++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/SimpleRestResourceAssignmentProcessor.kt
@@ -17,24 +17,146 @@
 
 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor
 
+import com.fasterxml.jackson.databind.node.ArrayNode
+import com.fasterxml.jackson.databind.node.JsonNodeFactory
+import com.fasterxml.jackson.databind.node.NullNode
+import com.fasterxml.jackson.databind.node.ObjectNode
+import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.RestResourceSource
+import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
+import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
+import org.onap.ccsdk.apps.controllerblueprints.core.*
+import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
+import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants
+import org.slf4j.LoggerFactory
 import org.springframework.stereotype.Service
 
 /**
  * SimpleRestResourceAssignmentProcessor
  *
- * @author Brinda Santh
+ * @author Kapil Singal
  */
-@Service("resource-assignment-processor-mdsal")
-open class SimpleRestResourceAssignmentProcessor : ResourceAssignmentProcessor() {
+@Service("resource-assignment-processor-primary-config-data")
+open class SimpleRestResourceAssignmentProcessor(private val blueprintRestLibPropertyService: BluePrintRestLibPropertyService)
+    : ResourceAssignmentProcessor() {
+
+    private val logger = LoggerFactory.getLogger(SimpleRestResourceAssignmentProcessor::class.java)
 
     override fun getName(): String {
-        return "resource-assignment-processor-mdsal"
+        return "resource-assignment-processor-primary-config-data"
     }
 
-    override fun process(executionRequest: ResourceAssignment) {
+    override fun process(resourceAssignment: ResourceAssignment) {
+        try {
+            validate(resourceAssignment)
+
+            // Check if It has Input
+            val value = raRuntimeService.getInputValue(resourceAssignment.name)
+            if (value != null && value !is NullNode) {
+                logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)")
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value)
+            } else {
+                val dName = resourceAssignment.dictionaryName
+                val dSource = resourceAssignment.dictionarySource
+                val resourceDefinition = resourceDictionaries[dName]
+                        ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName")
+                val resourceSource = resourceDefinition.sources[dSource]
+                        ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)")
+                val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " }
+                val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java)
+
+                val urlPath = checkNotNull(sourceProperties.urlPath) { "failed to get request urlPath for $dName under $dSource properties" }
+                val path = nullToEmpty(sourceProperties.path)
+                val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" }
+
+                logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})")
+
+                val restClientService = blueprintRestLibPropertyService.blueprintWebClientService("primary-config-data")
+                val response = restClientService.getResource(urlPath, String::class.java)
+                if (response.isNotBlank()) {
+                    logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)")
+                } else {
+                    populateResource(resourceAssignment, sourceProperties, response, path)
+                }
+            }
+            // Check the value has populated for mandatory case
+            ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment)
+        } catch (e: Exception) {
+            ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message)
+            throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e)
+        }
     }
 
-    override fun recover(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
+    @Throws(BluePrintProcessorException::class)
+    private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: RestResourceSource, restResponse: String, path: String) {
+        val dName = resourceAssignment.dictionaryName
+        val dSource = resourceAssignment.dictionarySource
+        val type = nullToEmpty(resourceAssignment.property?.type)
+        lateinit var entrySchemaType: String
+
+        val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
+        logger.info("Response processing type($type)")
+
+        val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) { "Failed to find path ($path) in response ($restResponse)" }
+        logger.info("populating value for output mapping ($outputKeyMapping), from json ($responseNode)")
+
+
+        when (type) {
+            in BluePrintTypes.validPrimitiveTypes() -> {
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($responseNode)")
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, responseNode)
+            }
+            in BluePrintTypes.validCollectionTypes() -> {
+                // Array Types
+                entrySchemaType = returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" }
+                val arrayNode = responseNode as ArrayNode
+
+                if (entrySchemaType !in BluePrintTypes.validPrimitiveTypes()) {
+                    val responseArrayNode = responseNode.toList()
+                    for (responseSingleJsonNode in responseArrayNode) {
+                        val arrayChildNode = JsonNodeFactory.instance.objectNode()
+                        outputKeyMapping.map {
+                            val responseKeyValue = responseSingleJsonNode.get(it.key)
+                            val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
+                            logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type  ({$propertyTypeForDataType})")
+                            JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, arrayChildNode)
+                        }
+                        arrayNode.add(arrayChildNode)
+                    }
+                }
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($arrayNode)")
+                // Set the List of Complex Values
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, arrayNode)
+            }
+            else -> {
+                // Complex Types
+                val objectNode = responseNode as ObjectNode
+                outputKeyMapping.map {
+                    val responseKeyValue = responseNode.get(it.key)
+                    val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, it.key)
+                    logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type  ({$propertyTypeForDataType})")
+                    JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode)
+                }
+
+                logger.info("For template key (${resourceAssignment.name}) setting value as ($objectNode)")
+                // Set the List of Complex Values
+                ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
+            }
+        }
     }
+
+    @Throws(BluePrintProcessorException::class)
+    private fun validate(resourceAssignment: ResourceAssignment) {
+        checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined")
+        checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
+        checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA, resourceAssignment.dictionarySource) {
+            "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_CONFIG_DATA} but it is ${resourceAssignment.dictionarySource}"
+        }
+        checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})")
+    }
+
+    override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
+    }
+
+
 }
\ No newline at end of file
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt
index 9a846d6..ef69eb5 100644
--- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt
+++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt
@@ -65,7 +65,7 @@
     fun testRegisteredSource() {
         val sources = resourceResolutionService.registeredResourceSources()
         assertNotNull(sources, "failed to get registered sources")
-        assertTrue(sources.containsAll(arrayListOf("input", "default", "primary-db", "mdsal")), "failed to get registered sources")
+        assertTrue(sources.containsAll(arrayListOf("input", "default", "primary-db", "primary-config-data")), "failed to get registered sources")
     }
 
     @Test