Change code in appc dispatcher for new LCMs in R6
[appc.git] / appc-dispatcher / appc-dispatcher-common / appc-data-access-lib / src / main / java / org / onap / appc / dao / util / AppcDatabaseConnectionPool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dao.util;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.configuration.Configuration;
30 import org.onap.appc.configuration.ConfigurationFactory;
31 import org.onap.appc.dao.util.api.DBConnectionPoolService;
32 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
33 import org.onap.appc.dao.util.exception.DBConnectionPoolException;
34
35 import java.sql.Connection;
36 import java.sql.SQLException;
37 import java.util.Map;
38
39 /**
40  * This class implements
41  *
42  * @see org.onap.appc.dao.util.dbcp.DBConnectionPool
43  * that provides a concrete implementation of appc database access
44  * with basic setup data extracted from the global configuration.
45  * @see org.onap.appc.configuration.Configuration
46  * <p>
47  * The singleton instance of this class has been instantiated by blueprint.
48  * An example is shown in the {@link DBConnectionPoolService}
49  */
50 public class AppcDatabaseConnectionPool implements DBConnectionPoolService {
51
52     private final EELFLogger logger = EELFManager.getInstance().getLogger(AppcDatabaseConnectionPool.class);
53
54     private DBConnectionPool dbConnectionPool;
55     private String dbName;
56
57     public AppcDatabaseConnectionPool() {
58         // do nothing
59     }
60
61     public AppcDatabaseConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
62         dbConnectionPool = new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
63     }
64
65     /**
66      * Injected by blueprint
67      *
68      * @param dbName
69      */
70     public void setDbName(String dbName) {
71         this.dbName = dbName;
72     }
73
74     /**
75      * Bean init method used by blueprint
76      */
77     public void init() {
78         Configuration configuration = ConfigurationFactory.getConfiguration();
79         String dbUrl = getConnectionProperty(configuration, PropertyPattern.DBURL);
80         String userName = getConnectionProperty(configuration, PropertyPattern.USERNAME);
81         String password = getConnectionProperty(configuration, PropertyPattern.PASSWORD);
82         String jdbcDriver = getJDBCDriver(configuration);
83
84         dbConnectionPool = getDBConnectionPool(dbUrl, userName, password, jdbcDriver);
85         // a simple health check
86         Connection connection = null;
87         try {
88             connection = dbConnectionPool.getConnection();
89         } catch (DBConnectionPoolException e) {
90             logger.error("DB connection pool creation failed."
91                     + " Please make sure the provided information is correct.");
92         }
93
94         if (connection != null) {
95             try {
96                 connection.close();
97             } catch (SQLException e) {
98                 logger.error("DB connection cannot be closed:", e.getMessage());
99             }
100         }
101     }
102
103     /**
104      * Bean destroy method used by blueprint
105      */
106     public void destroy() {
107         if (dbConnectionPool != null) {
108             dbConnectionPool.shutdown();
109         }
110     }
111
112     /**
113      * Get the connection from connection pool.
114      *
115      * @return Connection. If the provided db information is not correct,
116      * the return value might be null.
117      */
118     @Override
119     public Connection getConnection() throws DBConnectionPoolException {
120         return dbConnectionPool.getConnection();
121     }
122
123     /**
124      * Get dbcp status like active_status.
125      * <p>
126      * More details about status of DBConnectionPool,
127      * go check {@link org.onap.appc.dao.util.dbcp.DBConnectionPool#getDataSourceStatus()}
128      *
129      * @return a map contains some dbcp information.
130      */
131     @Override
132     public Map<String, Integer> getDataSourceStatus() {
133         return dbConnectionPool.getDataSourceStatus();
134     }
135
136     private String getConnectionProperty(Configuration configuration, PropertyPattern propertyPattern) {
137         String property = configuration.getProperty(String.format(propertyPattern.getPattern(), dbName), "");
138         return property;
139     }
140
141     private String getJDBCDriver(Configuration configuration) {
142         return configuration.getProperty(PropertyPattern.DRIVER.getPattern(), "");
143     }
144
145     protected DBConnectionPool getDBConnectionPool(String dbUrl, String userName, String password, String jdbcDriver) {
146         return new DBConnectionPool(dbUrl, userName, password, jdbcDriver);
147     }
148 }