Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / primary / BluePrintDBLibPropertyService.kt
1 /*
2  * Copyright © 2019 Bell Canada Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.db.primary
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintPropertiesService
21 import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibGenericService
22 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBDataSourceProperties
23 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.MARIA_DB
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.MYSQL_DB
25 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.MSSQL_DB
26 import org.onap.ccsdk.cds.blueprintsprocessor.db.DBLibConstants.Companion.PROCESSOR_DB
27 import org.onap.ccsdk.cds.blueprintsprocessor.db.MariaDataSourceProperties
28 import org.onap.ccsdk.cds.blueprintsprocessor.db.MySqlDataSourceProperties
29 import org.onap.ccsdk.cds.blueprintsprocessor.db.MSSqlDataSourceProperties
30 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
32 import org.springframework.stereotype.Service
33
34 @Service
35 class BluePrintDBLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
36
37     fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService =
38         blueprintDBDataSourceService(dBDataSourceProperties(jsonNode))
39
40     fun JdbcTemplate(selector: String): BluePrintDBLibGenericService =
41         blueprintDBDataSourceService(dBDataSourceProperties("blueprintsprocessor.db.$selector"))
42
43     private fun dBDataSourceProperties(jsonNode: JsonNode): DBDataSourceProperties =
44         when (val type = jsonNode.get("type").textValue()) {
45             MYSQL_DB -> JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)
46             MARIA_DB -> JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)
47             MSSQL_DB -> JacksonUtils.readValue(jsonNode, MSSqlDataSourceProperties::class.java)
48             else -> {
49                 throw BluePrintProcessorException(
50                     "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $MSSQL_DB"
51                 )
52             }
53         }!!
54
55     private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties =
56         bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java).let {
57             return when (it) {
58                 MARIA_DB, PROCESSOR_DB -> mariaDBConnectionProperties(prefix)
59                 MYSQL_DB -> mySqlDBConnectionProperties(prefix)
60                 MSSQL_DB -> mssqlDBConnectionProperties(prefix)
61                 else -> {
62                     throw BluePrintProcessorException(
63                         "DB type ($it) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $PROCESSOR_DB"
64                     )
65                 }
66             }
67         }
68
69     private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService =
70         when (dBConnetionProperties) {
71             is MariaDataSourceProperties -> MariaDatabaseConfiguration(dBConnetionProperties)
72             is MySqlDataSourceProperties -> MySqlDatabaseConfiguration(dBConnetionProperties)
73             is MSSqlDataSourceProperties -> MSSqlDatabaseConfiguration(dBConnetionProperties)
74             else -> throw BluePrintProcessorException(
75                 "Failed to create db configuration for ${dBConnetionProperties.url}"
76             )
77         }
78
79     private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties =
80         bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
81
82     private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties =
83         bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
84
85     private fun mssqlDBConnectionProperties(prefix: String): MSSqlDataSourceProperties =
86         bluePrintPropertiesService.propertyBeanType(prefix, MSSqlDataSourceProperties::class.java)
87 }