af7ab0547972fae6d265e6138cb167f0b89b07f4
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T 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.apps.blueprintsprocessor.db.primary
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.apps.blueprintsprocessor.db.AbstractDBLibGenericService
21 import org.onap.ccsdk.apps.blueprintsprocessor.db.DBLibConstants
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.springframework.boot.jdbc.DataSourceBuilder
24 import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
25 import org.springframework.stereotype.Service
26
27 @Service
28 open class DBLibGenericService(primaryNamedParameterJdbcTemplate: NamedParameterJdbcTemplate)
29     : AbstractDBLibGenericService(primaryNamedParameterJdbcTemplate) {
30
31     fun primaryJdbcTemplate():NamedParameterJdbcTemplate{
32         return namedParameterJdbcTemplate()
33     }
34
35     fun remoteJdbcTemplate(jsonNode: JsonNode): NamedParameterJdbcTemplate {
36         val type = jsonNode.get("type").textValue()
37         val driverDB: String
38
39         return when (type) {
40             DBLibConstants.MARIA_DB -> {
41                 driverDB = DBLibConstants.DRIVER_MARIA_DB
42                 jdbcTemplate(jsonNode, driverDB)
43             }
44             DBLibConstants.MYSQL_DB -> {
45                 driverDB = DBLibConstants.DRIVER_MYSQL_DB
46                 jdbcTemplate(jsonNode, driverDB)
47             }
48             DBLibConstants.ORACLE_DB -> {
49                 driverDB = DBLibConstants.DRIVER_ORACLE_DB
50                 jdbcTemplate(jsonNode, driverDB)
51             }
52             DBLibConstants.POSTGRES_DB -> {
53                 driverDB = DBLibConstants.DRIVER_POSTGRES_DB
54                 jdbcTemplate(jsonNode, driverDB)
55             }
56             else -> {
57                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
58             }
59         }
60     }
61
62     fun jdbcTemplate(jsonNode: JsonNode, driver: String): NamedParameterJdbcTemplate {
63         val dataSourceBuilder = DataSourceBuilder
64                 .create()
65                 .username(jsonNode.get("username").textValue())
66                 .password(jsonNode.get("password").textValue())
67                 .url(jsonNode.get("url").textValue())
68                 .driverClassName(driver)
69                 .build()
70         return NamedParameterJdbcTemplate(dataSourceBuilder)
71     }
72 }