ebfd4739543e096d852eb98a5c358c9eaf90f5bf
[sdnc/core.git] / dblib / provider / src / main / java / org / openecomp / sdnc / sli / resource / dblib / jdbc / MySQLCachedDataSource.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openecomp
4  * ================================================================================
5  * Copyright (C) 2016 - 2017 AT&T
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.sli.resource.dblib.jdbc;
22
23 import java.sql.SQLFeatureNotSupportedException;
24 import java.util.Properties;
25
26 import org.openecomp.sdnc.sli.resource.dblib.CachedDataSource;
27 import org.openecomp.sdnc.sli.resource.dblib.DBConfigException;
28 import org.openecomp.sdnc.sli.resource.dblib.config.BaseDBConfiguration;
29 import org.openecomp.sdnc.sli.resource.dblib.config.JDBCConfiguration;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
34
35
36  
37
38 /**
39  * @version $Revision: 1.7 $
40  * Change Log
41  * Author         Date     Comments
42  * ============== ======== ====================================================
43  * Rich Tabedzki
44  */
45
46 public class MySQLCachedDataSource extends CachedDataSource
47 {
48         private String dbUserId;
49         private String dbPasswd;
50         private String dbUrl;
51         
52         private String minLimit;
53         private String maxLimit;
54         private String initialLimit;
55         
56         private static final String AS_CONF_ERROR = "AS_CONF_ERROR: ";
57         
58         private static Logger LOGGER = LoggerFactory.getLogger(MySQLCachedDataSource.class);
59
60         /**
61          * @param jdbcElem
62          * @param alarmLog 
63          * @param occManager 
64          * @throws Exception 
65          */
66         public MySQLCachedDataSource(BaseDBConfiguration jdbcElem)
67         {
68                         super(jdbcElem);
69         }
70
71         @Override
72         protected void configure(BaseDBConfiguration xmlElem) throws DBConfigException 
73         {
74                 BaseDBConfiguration jdbcConfig = (BaseDBConfiguration)xmlElem;
75                 if(jdbcConfig.getConnTimeout() > 0){
76                         this.CONN_REQ_TIMEOUT = jdbcConfig.getConnTimeout();
77                 }
78                 if(jdbcConfig.getRequestTimeout() > 0){
79                                 this.DATA_REQ_TIMEOUT = jdbcConfig.getRequestTimeout();
80                 }
81
82         // set connection pool name
83                 String dbConnectionName = jdbcConfig.getDbConnectionName();
84         super.setDbConnectionName(dbConnectionName);
85         // Configure the JDBC connection
86         dbUserId = jdbcConfig.getDbUserId();
87         if (dbUserId == null)
88         {
89                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbUserId attribute";
90                 LOGGER.error(AS_CONF_ERROR + errorMsg);
91             throw new DBConfigException(errorMsg);
92         }
93
94         dbPasswd = jdbcConfig.getDbPasswd();
95         if (dbPasswd == null)
96         {
97                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbPasswd attribute";
98                 LOGGER.error(AS_CONF_ERROR + errorMsg);
99             throw new DBConfigException(errorMsg);
100         }
101         /*
102         dbDriver = jdbcConfig.getDbDriver();
103         if (dbDriver == null)
104         {
105                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbDriver attribute";
106                 LOGGER.error(AS_CONF_ERROR + errorMsg);
107                 throw new ScpTblUpdateError(errorMsg);
108         }
109         */
110
111         minLimit = Integer.toString(jdbcConfig.getDbMinLimit());
112         if (minLimit == null)
113         {
114                 String errorMsg =  "Invalid XML contents: JDBC Connection missing minLimit attribute";
115                 LOGGER.error(AS_CONF_ERROR + errorMsg);
116                 throw new DBConfigException(errorMsg);
117         }
118         maxLimit =  Integer.toString(jdbcConfig.getDbMaxLimit());
119         if (maxLimit == null)
120         {
121                 String errorMsg =  "Invalid XML contents: JDBC Connection missing maxLimit attribute";
122                 LOGGER.error(AS_CONF_ERROR + errorMsg);
123                 throw new DBConfigException(errorMsg);
124         }
125         initialLimit =  Integer.toString(jdbcConfig.getDbInitialLimit());
126         if (initialLimit == null)
127         {
128                 String errorMsg =  "Invalid XML contents: JDBC Connection missing initialLimit attribute";
129                 LOGGER.error(AS_CONF_ERROR + errorMsg);
130                 throw new DBConfigException(errorMsg);
131         }
132
133         dbUrl = jdbcConfig.getDbUrl();
134         if(dbUrl == null){
135                 String errorMsg =  "Invalid XML contents: JDBCConnection missing dbUrl attribute";
136                 LOGGER.error(AS_CONF_ERROR + errorMsg);
137             throw new DBConfigException(errorMsg);
138         }
139         
140                 try {
141                         
142                         MysqlDataSource dataSource = new MysqlDataSource();
143                     dataSource.setUser(dbUserId);
144                     dataSource.setPassword(dbPasswd);
145                     dataSource.setURL(dbUrl);
146 //                  dataSource.setInitialSize(5);
147 //                  dataSource.setMaxTotal(60);
148 //                  dataSource.setMaxActive(100);
149 //                  dataSource.setMaxWait(10000);
150 //                  dataSource.setMaxIdle(10);
151
152                         Properties connAttr = new Properties();
153
154                         connAttr.setProperty("MinLimit", minLimit);
155                         connAttr.setProperty("MaxLimit", maxLimit);
156                         connAttr.setProperty("InitialLimit", initialLimit);
157                         connAttr.setProperty("TRANSACTION_ISOLATION","SERIALIZABLE");
158                         connAttr.setProperty("CONNECTION_TAG", dbConnectionName.toUpperCase()+"_CONNECTION");
159                         connAttr.setProperty("InactivityTimeout", "900");
160                         connAttr.setProperty("AbandonedConnectionTimeout", "600");
161                         connAttr.setProperty("PropertyCheckInterval", "60");
162                         connAttr.setProperty("ValidateConnection", "true");
163                         
164
165                         synchronized(this)
166                         {
167                                 this.ds = dataSource;
168
169                                 initialized = true;
170                                 LOGGER.info("MySQLDataSource <"+dbConnectionName+"> configured successfully. Using URL: "+dbUrl);
171                         }
172
173 //              } catch (SQLException exc) {
174 //                      initialized = false;
175 //                      StringBuffer sb = new StringBuffer();
176 //                      sb.append("Failed to initialize MySQLDataSource<");
177 //                      sb.append(dbConnectionName).append(">. Reason: ");
178 //                      sb.append(exc.getMessage());
179 //                      LOGGER.error("AS_CONF_ERROR: " + sb.toString());
180 ////                    throw new DBConfigException(e.getMessage());
181                 } catch (Exception exc) {
182                 initialized = false;
183                         StringBuffer sb = new StringBuffer();
184                         sb.append("Failed to initialize MySQLCachedDataSource <");
185                         sb.append(dbConnectionName).append(">. Reason: ");
186                         sb.append(exc.getMessage());
187                         LOGGER.error("AS_CONF_ERROR: " + sb.toString());
188 //              throw new DBConfigException(e.getMessage());
189         }
190     }
191
192         public final String getDbUrl()
193         {
194                 return dbUrl;
195         }
196
197         public final String getDbUserId()
198         {
199                 return dbUserId;
200         }
201
202         public final String getDbPasswd()
203         {
204                 return dbPasswd;
205         }
206
207         public static MySQLCachedDataSource createInstance(BaseDBConfiguration config) /*throws Exception*/ {
208                 return new MySQLCachedDataSource(config);
209         }
210         
211         public String toString(){
212                 return getDbConnectionName();
213         }
214
215         public java.util.logging.Logger getParentLogger()
216                         throws SQLFeatureNotSupportedException {
217                 // TODO Auto-generated method stub
218                 return null;
219         }
220 }