2 * Copyright © 2019 Bell Canada Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.db.primary
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
33 class BluePrintDBLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
35 fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService =
36 blueprintDBDataSourceService(dBDataSourceProperties(jsonNode))
38 fun JdbcTemplate(selector: String): BluePrintDBLibGenericService =
39 blueprintDBDataSourceService(dBDataSourceProperties("blueprintsprocessor.db.$selector"))
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)
46 throw BluePrintProcessorException(
47 "DB type ($type) is not supported. Valid types: $MARIA_DB, $MYSQL_DB")
51 private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties =
52 bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java).let {
54 MARIA_DB, PROCESSOR_DB -> mariaDBConnectionProperties(prefix)
55 MYSQL_DB -> mySqlDBConnectionProperties(prefix)
57 throw BluePrintProcessorException(
58 "DB type ($it) is not supported. Valid types: $MARIA_DB, $MYSQL_DB, $PROCESSOR_DB")
63 private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService =
64 when (dBConnetionProperties) {
65 is MariaDataSourceProperties -> MariaDatabaseConfiguration(dBConnetionProperties)
66 is MySqlDataSourceProperties -> MySqlDatabaseConfiguration(dBConnetionProperties)
67 else -> throw BluePrintProcessorException(
68 "Failed to create db configuration for ${dBConnetionProperties.url}")
71 private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties =
72 bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
74 private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties =
75 bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)