Add MS SQL driver in CDS blueprints and 20/121320/6
authorPrudence Au <prudence.au@amdocs.com>
Wed, 12 May 2021 17:23:29 +0000 (13:23 -0400)
committerPrudence Au <prudence.au@amdocs.com>
Thu, 13 May 2021 23:47:11 +0000 (19:47 -0400)
DB resolution fails on an empty dataset with required template mapping.

Issue-ID: CCSDK-3293
Issue-ID: CCSDK-3294
Signed-off-by: Prudence Au <prudence.au@amdocs.com>
Change-Id: I9f302d12e7359b62180fcdfddbd8ae2c993a8324

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/pom.xml
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/BlueprintDBLibData.kt
ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/primary/BlueprintDBLibPropertyService.kt
ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/primary/MSSqlDatabaseConfiguration.kt [new file with mode: 0644]

index db5307f..114b5b8 100644 (file)
@@ -112,11 +112,11 @@ open class DatabaseResourceAssignmentProcessor(
         val jdbcTemplate = blueprintDBLibService(sourceProperties, dSource)
 
         val rows = jdbcTemplate.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)
+        if (rows.isEmpty()) {
+            logger.warn("Emptyset from dictionary-source($dSource) for dictionary name ($dName) the query ($sql).")
         }
+        logger.debug("Query returned ${rows.size} values")
+        populateResource(resourceAssignment, sourceProperties, rows)
     }
 
     private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource, selector: String): BlueprintDBLibGenericService {
index d9c6193..de3960c 100644 (file)
             <artifactId>hibernate-testing</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>com.microsoft.sqlserver</groupId>
+            <artifactId>mssql-jdbc</artifactId>
+            <version>9.2.1.jre11</version>
+        </dependency>
     </dependencies>
 </project>
index f11832b..ff1072c 100644 (file)
@@ -71,11 +71,13 @@ class DBLibConstants {
         const val MYSQL_DB: String = "mysql-db"
         const val ORACLE_DB: String = "oracle-db"
         const val POSTGRES_DB: String = "postgres-db"
+        const val MSSQL_DB: String = "mssql"
 
         // List of database drivers
         const val DRIVER_MARIA_DB = "org.mariadb.jdbc.Driver"
         const val DRIVER_MYSQL_DB = "com.mysql.jdbc.Driver"
         const val DRIVER_ORACLE_DB = "oracle.jdbc.driver.OracleDriver"
         const val DRIVER_POSTGRES_DB = "org.postgresql.Driver"
+        const val DRIVER_MSSQL_DB = "com.microsoft.sqlserver.jdbc.SQLServerDriver"
     }
 }
index 7023106..2da8382 100644 (file)
@@ -50,3 +50,11 @@ open class MySqlDataSourceProperties : DBDataSourceProperties() {
     lateinit var hibernateDialect: String
     override var driverClassName = DBLibConstants.DRIVER_MYSQL_DB
 }
+
+open class MSSqlDataSourceProperties : DBDataSourceProperties() {
+    lateinit var hibernateHbm2ddlAuto: String
+    lateinit var hibernateDDLAuto: String
+    lateinit var hibernateNamingStrategy: String
+    lateinit var hibernateDialect: String
+    override var driverClassName = DBLibConstants.DRIVER_MSSQL_DB
+}
index b272a97..3dbd76c 100644 (file)
@@ -22,9 +22,11 @@ import org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintDBLibGenericService
 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBDataSourceProperties
 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.MSSQL_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.MSSqlDataSourceProperties
 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.springframework.stereotype.Service
@@ -42,9 +44,10 @@ class BlueprintDBLibPropertyService(private var bluePrintPropertiesService: Blue
         when (val type = jsonNode.get("type").textValue()) {
             MYSQL_DB -> JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)
             MARIA_DB -> JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)
+            MSSQL_DB -> JacksonUtils.readValue(jsonNode, MSSqlDataSourceProperties::class.java)
             else -> {
                 throw BlueprintProcessorException(
-                    "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB"
+                    "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $MSSQL_DB"
                 )
             }
         }!!
@@ -54,6 +57,7 @@ class BlueprintDBLibPropertyService(private var bluePrintPropertiesService: Blue
             return when (it) {
                 MARIA_DB, PROCESSOR_DB -> mariaDBConnectionProperties(prefix)
                 MYSQL_DB -> mySqlDBConnectionProperties(prefix)
+                MSSQL_DB -> mssqlDBConnectionProperties(prefix)
                 else -> {
                     throw BlueprintProcessorException(
                         "DB type ($it) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $PROCESSOR_DB"
@@ -66,6 +70,7 @@ class BlueprintDBLibPropertyService(private var bluePrintPropertiesService: Blue
         when (dBConnetionProperties) {
             is MariaDataSourceProperties -> MariaDatabaseConfiguration(dBConnetionProperties)
             is MySqlDataSourceProperties -> MySqlDatabaseConfiguration(dBConnetionProperties)
+            is MSSqlDataSourceProperties -> MSSqlDatabaseConfiguration(dBConnetionProperties)
             else -> throw BlueprintProcessorException(
                 "Failed to create db configuration for ${dBConnetionProperties.url}"
             )
@@ -76,4 +81,7 @@ class BlueprintDBLibPropertyService(private var bluePrintPropertiesService: Blue
 
     private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties =
         bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
+
+    private fun mssqlDBConnectionProperties(prefix: String): MSSqlDataSourceProperties =
+        bluePrintPropertiesService.propertyBeanType(prefix, MSSqlDataSourceProperties::class.java)
 }
diff --git a/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/primary/MSSqlDatabaseConfiguration.kt b/ms/blueprintsprocessor/modules/commons/db-lib/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/db/primary/MSSqlDatabaseConfiguration.kt
new file mode 100644 (file)
index 0000000..d0ff5b6
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright © 2021 Bell Canada 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.cds.blueprintsprocessor.db.primary
+
+import org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintDBLibGenericService
+import org.onap.ccsdk.cds.blueprintsprocessor.db.MSSqlDataSourceProperties
+import org.slf4j.LoggerFactory
+import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
+import org.springframework.jdbc.datasource.DriverManagerDataSource
+import javax.sql.DataSource
+
+class MSSqlDatabaseConfiguration(private val msSqlDataSourceProperties: MSSqlDataSourceProperties) : BlueprintDBLibGenericService {
+    override fun namedParameterJdbcTemplate(): NamedParameterJdbcTemplate {
+        return msSqlNamedParameterJdbcTemplate(msSqlDataSource())
+    }
+
+    override fun query(sql: String, params: Map<String, Any>): List<Map<String, Any>> {
+        return msSqlNamedParameterJdbcTemplate(msSqlDataSource()).queryForList(sql, params)
+    }
+
+    override fun update(sql: String, params: Map<String, Any>): Int {
+        return msSqlNamedParameterJdbcTemplate(msSqlDataSource()).update(sql, params)
+    }
+
+    val log = LoggerFactory.getLogger(PrimaryDatabaseConfiguration::class.java)!!
+
+    fun msSqlDataSource(): DataSource {
+        val dataSource = DriverManagerDataSource()
+        dataSource.setDriverClassName(msSqlDataSourceProperties.driverClassName)
+        dataSource.url = msSqlDataSourceProperties.url
+        dataSource.username = msSqlDataSourceProperties.username
+        dataSource.password = msSqlDataSourceProperties.password
+        return dataSource
+    }
+
+    fun msSqlNamedParameterJdbcTemplate(msSqlDataSource: DataSource): NamedParameterJdbcTemplate {
+        return NamedParameterJdbcTemplate(msSqlDataSource)
+    }
+}