Fixing Blueprint Typo's and docs
[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.PROCESSOR_DB
26 import org.onap.ccsdk.cds.blueprintsprocessor.db.MariaDataSourceProperties
27 import org.onap.ccsdk.cds.blueprintsprocessor.db.MySqlDataSourceProperties
28 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.springframework.stereotype.Service
31
32 @Service
33 class BluePrintDBLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
34
35     fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService =
36         blueprintDBDataSourceService(dBDataSourceProperties(jsonNode))
37
38     fun JdbcTemplate(selector: String): BluePrintDBLibGenericService =
39         blueprintDBDataSourceService(dBDataSourceProperties("blueprintsprocessor.db.$selector"))
40
41     private fun dBDataSourceProperties(jsonNode: JsonNode): DBDataSourceProperties =
42         when (val type = jsonNode.get("type").textValue()) {
43             MYSQL_DB -> JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)
44             MARIA_DB -> JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)
45             else -> {
46                 throw BluePrintProcessorException(
47                     "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB"
48                 )
49             }
50         }!!
51
52     private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties =
53         bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java).let {
54             return when (it) {
55                 MARIA_DB, PROCESSOR_DB -> mariaDBConnectionProperties(prefix)
56                 MYSQL_DB -> mySqlDBConnectionProperties(prefix)
57                 else -> {
58                     throw BluePrintProcessorException(
59                         "DB type ($it) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $PROCESSOR_DB"
60                     )
61                 }
62             }
63         }
64
65     private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService =
66         when (dBConnetionProperties) {
67             is MariaDataSourceProperties -> MariaDatabaseConfiguration(dBConnetionProperties)
68             is MySqlDataSourceProperties -> MySqlDatabaseConfiguration(dBConnetionProperties)
69             else -> throw BluePrintProcessorException(
70                 "Failed to create db configuration for ${dBConnetionProperties.url}"
71             )
72         }
73
74     private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties =
75         bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
76
77     private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties =
78         bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
79 }