Resource Resolution Service: Primary DB
authorSingal, Kapil (ks220y) <ks220y@att.com>
Thu, 17 Jan 2019 09:49:30 +0000 (04:49 -0500)
committerKAPIL SINGAL <ks220y@att.com>
Thu, 17 Jan 2019 14:47:40 +0000 (14:47 +0000)
Adding Primary-DB Resource Resolution Processor Service to resolve Resources of source primary-db

Change-Id: I156f7958b681e51b7c3bfdee92b6fbd196591e73
Issue-ID: CCSDK-942
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/PrimaryDataResourceAssignmentProcessor.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentTest.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties [new file with mode: 0644]

index b141025..9bad099 100644 (file)
@@ -33,8 +33,8 @@ open class DefaultResourceSource : ResourceSourceProperties() {
 open class DatabaseResourceSource : ResourceSourceProperties() {
     lateinit var type: String
     lateinit var query: String
-    var inputKeyMapping: MutableList<String>? = null
-    var outputKeyMapping: MutableList<String>? = null
+    var inputKeyMapping: MutableMap<String, String>? = null
+    var outputKeyMapping: MutableMap<String, String>? = null
     lateinit var keyDependencies: MutableList<String>
 }
 
@@ -43,8 +43,8 @@ open class RestResourceSource : ResourceSourceProperties() {
     lateinit var urlPath: String
     lateinit var path: String
     lateinit var expressionType: String
-    var inputKeyMapping: MutableList<String>? = null
-    var outputKeyMapping: MutableList<String>? = null
+    var inputKeyMapping: MutableMap<String, String>? = null
+    var outputKeyMapping: MutableMap<String, String>? = null
     lateinit var keyDependencies: MutableList<String>
 }
 
@@ -53,7 +53,7 @@ open class CapabilityResourceSource : ResourceSourceProperties() {
     lateinit var instanceName: String
     lateinit var path: String
     lateinit var expressionType: String
-    var inputKeyMapping: MutableList<String>? = null
-    var outputKeyMapping: MutableList<String>? = null
+    var inputKeyMapping: MutableMap<String, String>? = null
+    var outputKeyMapping: MutableMap<String, String>? = null
     lateinit var keyDependencies: MutableList<String>
 }
\ No newline at end of file
index ec98d09..2b3270e 100644 (file)
 
 package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor
 
+import com.fasterxml.jackson.databind.node.JsonNodeFactory
+import com.fasterxml.jackson.databind.node.NullNode
+import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.service.PrimaryDBLibGenericService
+import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource
+import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils
+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
+import java.util.*
 
 /**
  * PrimaryDataResourceAssignmentProcessor
@@ -26,15 +36,129 @@ import org.springframework.stereotype.Service
  * @author Brinda Santh
  */
 @Service("resource-assignment-processor-primary-db")
-open class PrimaryDataResourceAssignmentProcessor : ResourceAssignmentProcessor(){
+open class PrimaryDataResourceAssignmentProcessor(private val primaryDBLibGenericService: PrimaryDBLibGenericService)
+    : ResourceAssignmentProcessor() {
+
+    private val logger = LoggerFactory.getLogger(PrimaryDataResourceAssignmentProcessor::class.java)
 
     override fun getName(): String {
         return "resource-assignment-processor-primary-db"
     }
 
-    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)
+                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)
+            }
+
+            // 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)
+        }
+    }
+
+    @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_DB, resourceAssignment.dictionarySource) {
+            "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_DB} but it is ${resourceAssignment.dictionarySource}"
+        }
+    }
+
+    private fun populateNamedParameter(inputKeyMapping: Map<String, String>): Map<String, Any> {
+        val namedParameters = HashMap<String, Any>()
+        inputKeyMapping.forEach {
+            val expressionValue = raRuntimeService.getDictionaryStore(it.value)
+            logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)")
+            namedParameters[it.key] = expressionValue
+        }
+        logger.info("Parameter information : ({})", namedParameters)
+        return namedParameters
+    }
+
+    @Throws(BluePrintProcessorException::class)
+    private fun processDBResults(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) {
+        val dName = resourceAssignment.dictionaryName
+        val dSource = resourceAssignment.dictionarySource
+        val type = nullToEmpty(resourceAssignment.property?.type)
+
+        val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" }
+        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)
+            }
+
+            if (checkNotEmptyOrThrow(entrySchemaType, "Entry schema is not defined for dictionary ($dName) info")) {
+                val arrayNode = JsonNodeFactory.instance.arrayNode()
+                rows.forEach {
+                    if (BluePrintTypes.validPrimitiveTypes().contains(entrySchemaType)) {
+                        val dbColumnValue = it[outputKeyMapping[dName]]
+                        // Add Array JSON
+                        JacksonUtils.populatePrimitiveValues(dbColumnValue!!, entrySchemaType, arrayNode)
+                    } else {
+                        val arrayChildNode = JsonNodeFactory.instance.objectNode()
+                        for (mapping in outputKeyMapping.entries) {
+                            val dbColumnValue = checkNotNull(it[mapping.key])
+                            val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, entrySchemaType, mapping.key)
+                            JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, arrayChildNode)
+                        }
+                        arrayNode.add(arrayChildNode)
+                    }
+                }
+                // 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)
+            }
+            ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, objectNode)
+        }
     }
 
-    override fun recover(runtimeException: RuntimeException, executionRequest: ResourceAssignment) {
+    override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) {
     }
 }
\ No newline at end of file
index 8adde86..574d235 100644 (file)
@@ -19,23 +19,36 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution
 import com.fasterxml.jackson.databind.JsonNode
 import org.junit.Test
 import org.junit.runner.RunWith
+import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties
+import org.onap.ccsdk.apps.blueprintsprocessor.core.BlueprintPropertyConfiguration
 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.apps.blueprintsprocessor.core.utils.PayloadUtils
+import org.onap.ccsdk.apps.blueprintsprocessor.db.BluePrintDBLibConfiguration
+import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.service.PrimaryDBLibGenericService
 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor.*
 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
 import org.onap.ccsdk.apps.controllerblueprints.core.asJsonNode
+import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
 import org.onap.ccsdk.apps.controllerblueprints.core.putJsonElement
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
 import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration
+import org.springframework.context.annotation.ComponentScan
 import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.TestPropertySource
 import org.springframework.test.context.junit4.SpringRunner
 
 @RunWith(SpringRunner::class)
-@ContextConfiguration(classes = [ResourceResolutionComponent::class, ResourceResolutionService::class,
+@ContextConfiguration(classes = [ResourceResolutionService::class,
     InputResourceAssignmentProcessor::class, DefaultResourceAssignmentProcessor::class,
     PrimaryDataResourceAssignmentProcessor::class, SimpleRestResourceAssignmentProcessor::class,
-    CapabilityResourceAssignmentProcessor::class])
+    CapabilityResourceAssignmentProcessor::class, PrimaryDBLibGenericService::class,
+    BlueprintPropertyConfiguration::class, BluePrintProperties::class,
+    BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class])
+@TestPropertySource(locations = ["classpath:application-test.properties"])
+@ComponentScan(basePackages = ["org.onap.ccsdk.apps.blueprintsprocessor", "org.onap.ccsdk.apps.controllerblueprints"])
+@EnableAutoConfiguration
 class ResourceResolutionComponentTest {
 
     @Autowired
index 6c68675..9a846d6 100644 (file)
@@ -19,14 +19,22 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution
 import org.junit.Assert
 import org.junit.Test
 import org.junit.runner.RunWith
+import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintProperties
+import org.onap.ccsdk.apps.blueprintsprocessor.core.BlueprintPropertyConfiguration
 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
 import org.onap.ccsdk.apps.blueprintsprocessor.core.utils.PayloadUtils
+import org.onap.ccsdk.apps.blueprintsprocessor.db.BluePrintDBLibConfiguration
+import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.service.PrimaryDBLibGenericService
 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor.*
+import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
 import org.slf4j.LoggerFactory
 import org.springframework.beans.factory.annotation.Autowired
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration
+import org.springframework.context.annotation.ComponentScan
 import org.springframework.test.context.ContextConfiguration
+import org.springframework.test.context.TestPropertySource
 import org.springframework.test.context.junit4.SpringRunner
 import kotlin.test.assertNotNull
 import kotlin.test.assertTrue
@@ -40,7 +48,12 @@ import kotlin.test.assertTrue
 @ContextConfiguration(classes = [ResourceResolutionService::class,
     InputResourceAssignmentProcessor::class, DefaultResourceAssignmentProcessor::class,
     PrimaryDataResourceAssignmentProcessor::class, SimpleRestResourceAssignmentProcessor::class,
-    CapabilityResourceAssignmentProcessor::class])
+    CapabilityResourceAssignmentProcessor::class, PrimaryDBLibGenericService::class,
+    BlueprintPropertyConfiguration::class, BluePrintProperties::class,
+    BluePrintDBLibConfiguration::class, BluePrintLoadConfiguration::class])
+@TestPropertySource(locations = ["classpath:application-test.properties"])
+@ComponentScan(basePackages = ["org.onap.ccsdk.apps.blueprintsprocessor", "org.onap.ccsdk.apps.controllerblueprints"])
+@EnableAutoConfiguration
 class ResourceResolutionServiceTest {
 
     private val log = LoggerFactory.getLogger(ResourceResolutionServiceTest::class.java)
@@ -48,7 +61,6 @@ class ResourceResolutionServiceTest {
     @Autowired
     lateinit var resourceResolutionService: ResourceResolutionService
 
-
     @Test
     fun testRegisteredSource() {
         val sources = resourceResolutionService.registeredResourceSources()
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/application-test.properties
new file mode 100644 (file)
index 0000000..b7cf00a
--- /dev/null
@@ -0,0 +1,27 @@
+# suppress inspection "UnusedProperty" for whole file
+#
+# 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.
+#
+blueprintsprocessor.db.primary.url=jdbc:h2:mem:testdb;DB_CLOSE_ON_EXIT=FALSE
+blueprintsprocessor.db.primary.username=sa
+blueprintsprocessor.db.primary.password=
+blueprintsprocessor.db.primary.driverClassName=org.h2.Driver
+blueprintsprocessor.db.primary.hibernateHbm2ddlAuto=create-drop
+blueprintsprocessor.db.primary.hibernateDDLAuto=update
+blueprintsprocessor.db.primary.hibernateNamingStrategy=org.hibernate.cfg.ImprovedNamingStrategy
+blueprintsprocessor.db.primary.hibernateDialect=org.hibernate.dialect.H2Dialect
+# Controller Blueprints Core Configuration
+blueprintsprocessor.blueprintDeployPath=./target/blueprints/deploy
+blueprintsprocessor.blueprintArchivePath=./target/blueprints/archive
\ No newline at end of file