35baf9314eb6c55cd1fd9bd95c94c03eb4ed96ae
[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
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.MariaDataSourceProperties
25 import org.onap.ccsdk.cds.blueprintsprocessor.db.MySqlDataSourceProperties
26 import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDataSourceProperties
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.springframework.stereotype.Service
30
31 @Service
32 class BluePrintDBLibPropertyService(private var bluePrintPropertiesService: BluePrintPropertiesService) {
33
34     fun JdbcTemplate(jsonNode: JsonNode): BluePrintDBLibGenericService {
35         val dBConnetionProperties = dBDataSourceProperties(jsonNode)
36         return blueprintDBDataSourceService(dBConnetionProperties)
37     }
38
39     fun JdbcTemplate(selector: String): BluePrintDBLibGenericService {
40         val prefix = "blueprintsprocessor.db.$selector"
41         val dBConnetionProperties = dBDataSourceProperties(prefix)
42         return blueprintDBDataSourceService(dBConnetionProperties)
43     }
44
45     private fun dBDataSourceProperties(jsonNode: JsonNode): DBDataSourceProperties {
46         val type = jsonNode.get("type").textValue()
47         return when (type) {
48             DBLibConstants.MYSQL_DB -> {
49                 JacksonUtils.readValue(jsonNode, MySqlDataSourceProperties::class.java)!!
50             }
51             DBLibConstants.MARIA_DB -> {
52                 JacksonUtils.readValue(jsonNode, MariaDataSourceProperties::class.java)!!
53             }
54             else -> {
55                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
56             }
57         }
58     }
59
60     private fun dBDataSourceProperties(prefix: String): DBDataSourceProperties {
61         val type = bluePrintPropertiesService.propertyBeanType("$prefix.type", String::class.java)
62         return when (type) {
63             DBLibConstants.MARIA_DB -> {
64                 mariaDBConnectionProperties(prefix)
65             }
66             DBLibConstants.MYSQL_DB -> {
67                 mySqlDBConnectionProperties(prefix)
68             }
69             DBLibConstants.ORACLE_DB -> {
70                 TODO("not implemented")
71             }
72             DBLibConstants.POSTGRES_DB -> {
73                 TODO("not implemented")
74             }
75             DBLibConstants.PRIMARY_DB -> {
76                 primaryDBConnectionProperties(prefix)
77             }
78             else -> {
79                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
80             }
81         }
82     }
83
84     private fun blueprintDBDataSourceService(dBConnetionProperties: DBDataSourceProperties): BluePrintDBLibGenericService {
85         when (dBConnetionProperties) {
86             is MariaDataSourceProperties -> {
87                 return MariaDatabaseConfiguration(dBConnetionProperties)
88             }
89             is MySqlDataSourceProperties -> {
90                 return MySqlDatabaseConfiguration(dBConnetionProperties)
91             }
92             else -> {
93                 throw BluePrintProcessorException("couldn't get rest service for")
94             }
95         }
96     }
97
98     private fun mySqlDBConnectionProperties(prefix: String): MySqlDataSourceProperties {
99         return bluePrintPropertiesService.propertyBeanType(prefix, MySqlDataSourceProperties::class.java)
100     }
101
102     private fun mariaDBConnectionProperties(prefix: String): MariaDataSourceProperties {
103         return bluePrintPropertiesService.propertyBeanType(prefix, MariaDataSourceProperties::class.java)
104     }
105
106     private fun primaryDBConnectionProperties(prefix: String): PrimaryDataSourceProperties {
107         return bluePrintPropertiesService.propertyBeanType(prefix, PrimaryDataSourceProperties::class.java)
108     }
109 }