2  * Copyright © 2017-2018 AT&T 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.apps.blueprintsprocessor.db.primary
 
  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
 
  28 open class DBLibGenericService(primaryNamedParameterJdbcTemplate: NamedParameterJdbcTemplate)
 
  29     : AbstractDBLibGenericService(primaryNamedParameterJdbcTemplate) {
 
  31     fun primaryJdbcTemplate():NamedParameterJdbcTemplate{
 
  32         return namedParameterJdbcTemplate()
 
  35     fun remoteJdbcTemplate(jsonNode: JsonNode): NamedParameterJdbcTemplate {
 
  36         val type = jsonNode.get("type").textValue()
 
  40             DBLibConstants.MARIA_DB -> {
 
  41                 driverDB = DBLibConstants.DRIVER_MARIA_DB
 
  42                 jdbcTemplate(jsonNode, driverDB)
 
  44             DBLibConstants.MYSQL_DB -> {
 
  45                 driverDB = DBLibConstants.DRIVER_MYSQL_DB
 
  46                 jdbcTemplate(jsonNode, driverDB)
 
  48             DBLibConstants.ORACLE_DB -> {
 
  49                 driverDB = DBLibConstants.DRIVER_ORACLE_DB
 
  50                 jdbcTemplate(jsonNode, driverDB)
 
  52             DBLibConstants.POSTGRES_DB -> {
 
  53                 driverDB = DBLibConstants.DRIVER_POSTGRES_DB
 
  54                 jdbcTemplate(jsonNode, driverDB)
 
  57                 throw BluePrintProcessorException("Rest adaptor($type) is not supported")
 
  62     fun jdbcTemplate(jsonNode: JsonNode, driver: String): NamedParameterJdbcTemplate {
 
  63         val dataSourceBuilder = DataSourceBuilder
 
  65                 .username(jsonNode.get("username").textValue())
 
  66                 .password(jsonNode.get("password").textValue())
 
  67                 .url(jsonNode.get("url").textValue())
 
  68                 .driverClassName(driver)
 
  70         return NamedParameterJdbcTemplate(dataSourceBuilder)