Fix default behavior for DatabaseResourceAssignmentProcessor 99/106099/1
authorJozsef Csongvai <jozsef.csongvai@bell.ca>
Thu, 16 Apr 2020 12:33:36 +0000 (08:33 -0400)
committerJozsef Csongvai <jozsef.csongvai@bell.ca>
Thu, 16 Apr 2020 12:33:36 +0000 (08:33 -0400)
If a data-dictionary did not declare an endpoint-selector, it would
default to primary cds-db instead of using data-dictionary source to
look up database configuration. Processor-db configurations were not
used because of this.

Also improved error messages and some refactoring.

Issue-ID: CCSDK-2315
Signed-off-by: Jozsef Csongvai <jozsef.csongvai@bell.ca>
Change-Id: I7122e1334951db4eec5b9ae8b01c1f66596636f6

ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt
ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/BluePrintDBLibConfiguration.kt
ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/primary/BluePrintDBLibPropertyService.kt

index a0fa97b..2640b5b 100644 (file)
@@ -107,7 +107,7 @@ open class DatabaseResourceAssignmentProcessor(
             "DatabaseResource ($dSource) dictionary information: " +
                     "Query:($sql), input-key-mapping:($inputKeyMapping), output-key-mapping:(${sourceProperties.outputKeyMapping})"
         )
-        val jdbcTemplate = blueprintDBLibService(sourceProperties)
+        val jdbcTemplate = blueprintDBLibService(sourceProperties, dSource)
 
         val rows = jdbcTemplate.query(sql, populateNamedParameter(inputKeyMapping))
         if (rows.isNullOrEmpty()) {
@@ -117,12 +117,12 @@ open class DatabaseResourceAssignmentProcessor(
         }
     }
 
-    private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): BluePrintDBLibGenericService {
+    private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource, selector: String): BluePrintDBLibGenericService {
         return if (isNotEmpty(sourceProperties.endpointSelector)) {
             val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!)
             bluePrintDBLibPropertyService.JdbcTemplate(dbPropertiesJson)
         } else {
-            primaryDBLibGenericService
+            bluePrintDBLibPropertyService.JdbcTemplate(selector)
         }
     }
 
index 0d737f4..6370319 100644 (file)
@@ -66,7 +66,7 @@ class DBLibConstants {
 
         // list of database
         const val MARIA_DB: String = "maria-db"
-        const val PRIMARY_DB: String = "processor-db"
+        const val PROCESSOR_DB: String = "processor-db"
         const val MYSQL_DB: String = "mysql-db"
         const val ORACLE_DB: String = "oracle-db"
         const val POSTGRES_DB: String = "postgres-db"
index 35baf93..e686e83 100644 (file)
@@ -20,10 +20,11 @@ import com.fasterxml.jackson.databind.JsonNode
 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibGenericService
 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBDataSourceProperties
-import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants
+import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.MARIA_DB
+import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.MYSQL_DB
+import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.PROCESSOR_DB
 import org.onap.ccsdk.cds.blueprintsprocessor.db.MariaDataSourceProperties
 import org.onap.ccsdk.cds.blueprintsprocessor.db.MySqlDataSourceProperties
-import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.springframework.stereotype.Service
@@ -31,79 +32,45 @@ import org.springframework.stereotype.Service
 @Service
 class BluePrintDBLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
 
-    fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService {
-        val dBConnetionProperties = dBDataSourceProperties(jsonNode)
-        return blueprintDBDataSourceService(dBConnetionProperties)
-    }
+    fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService =
+            blueprintDBDataSourceService(dBDataSourceProperties(jsonNode))
 
-    fun JdbcTemplate(selector: String): BluePrintDBLibGenericService {
-        val prefix = "blueprintsprocessor.db.$selector"
-        val dBConnetionProperties = dBDataSourceProperties(prefix)
-        return blueprintDBDataSourceService(dBConnetionProperties)
-    }
+    fun JdbcTemplate(selector: String): BluePrintDBLibGenericService =
+            blueprintDBDataSourceService(dBDataSourceProperties("blueprintsprocessor.db.$selector"))
 
-    private fun dBDataSourceProperties(jsonNode: JsonNode): DBDataSourceProperties {
-        val type = jsonNode.get("type").textValue()
-        return when (type) {
-            DBLibConstants.MYSQL_DB -> {
-                JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)!!
-            }
-            DBLibConstants.MARIA_DB -> {
-                JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)!!
-            }
-            else -> {
-                throw BluePrintProcessorException("Rest adaptor($type) is not supported")
-            }
-        }
-    }
+    private fun dBDataSourceProperties(jsonNode: JsonNode): DBDataSourceProperties =
+            when (val type = jsonNode.get("type").textValue()) {
+                MYSQL_DB -> JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)
+                MARIA_DB -> JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)
+                else -> {
+                    throw BluePrintProcessorException(
+                            "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB")
+                }
+            }!!
 
-    private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties {
-        val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
-        return when (type) {
-            DBLibConstants.MARIA_DB -> {
-                mariaDBConnectionProperties(prefix)
-            }
-            DBLibConstants.MYSQL_DB -> {
-                mySqlDBConnectionProperties(prefix)
-            }
-            DBLibConstants.ORACLE_DB -> {
-                TODO("not implemented")
-            }
-            DBLibConstants.POSTGRES_DB -> {
-                TODO("not implemented")
+    private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties =
+            bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java).let {
+                return when (it) {
+                    MARIA_DB, PROCESSOR_DB -> mariaDBConnectionProperties(prefix)
+                    MYSQL_DB -> mySqlDBConnectionProperties(prefix)
+                    else -> {
+                        throw BluePrintProcessorException(
+                                "DB type ($it) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $PROCESSOR_DB")
+                    }
+                }
             }
-            DBLibConstants.PRIMARY_DB -> {
-                primaryDBConnectionProperties(prefix)
-            }
-            else -> {
-                throw BluePrintProcessorException("Rest adaptor($type) is not supported")
-            }
-        }
-    }
 
-    private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService {
-        when (dBConnetionProperties) {
-            is MariaDataSourceProperties -> {
-                return MariaDatabaseConfiguration(dBConnetionProperties)
-            }
-            is MySqlDataSourceProperties -> {
-                return MySqlDatabaseConfiguration(dBConnetionProperties)
+    private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService =
+            when (dBConnetionProperties) {
+                is MariaDataSourceProperties -> MariaDatabaseConfiguration(dBConnetionProperties)
+                is MySqlDataSourceProperties -> MySqlDatabaseConfiguration(dBConnetionProperties)
+                else -> throw BluePrintProcessorException(
+                        "Failed to create db configuration for ${dBConnetionProperties.url}")
             }
-            else -> {
-                throw BluePrintProcessorException("couldn't get rest service for")
-            }
-        }
-    }
-
-    private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties {
-        return bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
-    }
 
-    private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties {
-        return bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
-    }
+    private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties =
+            bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
 
-    private fun primaryDBConnectionProperties(prefix: String): PrimaryDataSourceProperties {
-        return bluePrintPropertiesService.propertyBeanType(prefix, PrimaryDataSourceProperties::class.java)
-    }
+    private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties =
+            bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
 }