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