Fixing code as part of dmaap-775
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / onap / dmaap / datarouter / provisioning / utils / DB.java
1 /*******************************************************************************\r
2  * ============LICENSE_START==================================================\r
3  * * org.onap.dmaap\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * *\r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * *\r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 \r
24 \r
25 package org.onap.dmaap.datarouter.provisioning.utils;\r
26 \r
27 import java.io.File;\r
28 import java.io.FileInputStream;\r
29 import java.io.FileReader;\r
30 import java.io.IOException;\r
31 import java.io.LineNumberReader;\r
32 import java.sql.Connection;\r
33 import java.sql.DatabaseMetaData;\r
34 import java.sql.DriverManager;\r
35 import java.sql.ResultSet;\r
36 import java.sql.SQLException;\r
37 import java.sql.Statement;\r
38 import java.util.HashSet;\r
39 import java.util.LinkedList;\r
40 import java.util.NoSuchElementException;\r
41 import java.util.Properties;\r
42 import java.util.Queue;\r
43 import java.util.Set;\r
44 import org.apache.log4j.Logger;\r
45 \r
46 /**\r
47  * Load the DB JDBC driver, and manage a simple pool of connections to the DB.\r
48  *\r
49  * @author Robert Eby\r
50  * @version $Id$\r
51  */\r
52 public class DB {\r
53 \r
54     private static Logger intlogger = Logger\r
55         .getLogger("org.onap.dmaap.datarouter.provisioning.internal");\r
56 \r
57     private static String DB_URL;\r
58     private static String DB_LOGIN;\r
59     private static String DB_PASSWORD;\r
60     private static Properties props;\r
61     private static final Queue<Connection> queue = new LinkedList<>();\r
62 \r
63     private static String HTTPS_PORT;\r
64     private static String HTTP_PORT;\r
65 \r
66     /**\r
67      * Construct a DB object.  If this is the very first creation of this object, it will load a copy of the properties\r
68      * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the\r
69      * properties file or the DB driver is missing), the JVM will exit.\r
70      */\r
71     public DB() {\r
72         if (props == null) {\r
73             props = new Properties();\r
74             try {\r
75                 props.load(new FileInputStream(System.getProperty(\r
76                     "org.onap.dmaap.datarouter.provserver.properties",\r
77                     "/opt/app/datartr/etc/provserver.properties")));\r
78                 String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver");\r
79                 DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url");\r
80                 DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login");\r
81                 DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password");\r
82                 HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");\r
83                 HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");\r
84                 Class.forName(DB_DRIVER);\r
85             } catch (IOException e) {\r
86                 intlogger.fatal("PROV9003 Opening properties: " + e.getMessage());\r
87                 System.exit(1);\r
88             } catch (ClassNotFoundException e) {\r
89                 intlogger.fatal("PROV9004 cannot find the DB driver: " + e);\r
90                 System.exit(1);\r
91             }\r
92         }\r
93     }\r
94 \r
95     /**\r
96      * Get the provisioning server properties (loaded from provserver.properties).\r
97      *\r
98      * @return the Properties object\r
99      */\r
100     public Properties getProperties() {\r
101         return props;\r
102     }\r
103 \r
104     /**\r
105      * Get a JDBC connection to the DB from the pool.  Creates a new one if none are available.\r
106      *\r
107      * @return the Connection\r
108      */\r
109     @SuppressWarnings("resource")\r
110     public Connection getConnection() throws SQLException {\r
111         Connection connection = null;\r
112         while (connection == null) {\r
113             synchronized (queue) {\r
114                 try {\r
115                     connection = queue.remove();\r
116                 } catch (NoSuchElementException nseEx) {\r
117                     int n = 0;\r
118                     do {\r
119                         // Try up to 3 times to get a connection\r
120                         try {\r
121                             connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);\r
122                         } catch (SQLException sqlEx) {\r
123                             if (++n >= 3) {\r
124                                 throw sqlEx;\r
125                             }\r
126                         } finally {\r
127                             if (connection != null && !connection.isValid(1)) {\r
128                                 connection.close();\r
129                                 connection = null;\r
130                             }\r
131                         }\r
132                     } while (connection == null);\r
133                 }\r
134             }\r
135 \r
136         }\r
137         return connection;\r
138     }\r
139 \r
140     /**\r
141      * Returns a JDBC connection to the pool.\r
142      *\r
143      * @param connection the Connection to return\r
144      */\r
145     public void release(Connection connection) {\r
146         if (connection != null) {\r
147             synchronized (queue) {\r
148                 if (!queue.contains(connection)) {\r
149                     queue.add(connection);\r
150                 }\r
151             }\r
152         }\r
153     }\r
154 \r
155     /**\r
156      * Run all necessary retrofits required to bring the database up to the level required for this version of the\r
157      * provisioning server.  This should be run before the server itself is started.\r
158      *\r
159      * @return true if all retrofits worked, false otherwise\r
160      */\r
161     public boolean runRetroFits() {\r
162         return retroFit1();\r
163     }\r
164 \r
165 \r
166     public static String getHttpsPort() {\r
167         return HTTPS_PORT;\r
168     }\r
169 \r
170     public static String getHttpPort() {\r
171         return HTTP_PORT;\r
172     }\r
173 \r
174     /**\r
175      * Retrofit 1 - Make sure the expected tables are in DB and are initialized. Uses sql_init_01.sql to setup the DB.\r
176      *\r
177      * @return true if the retrofit worked, false otherwise\r
178      */\r
179     private boolean retroFit1() {\r
180         final String[] expectedTables = {\r
181             "FEEDS", "FEED_ENDPOINT_ADDRS", "FEED_ENDPOINT_IDS", "PARAMETERS",\r
182             "SUBSCRIPTIONS", "LOG_RECORDS", "INGRESS_ROUTES", "EGRESS_ROUTES",\r
183             "NETWORK_ROUTES", "NODESETS", "NODES", "GROUPS"\r
184         };\r
185         Connection connection = null;\r
186         try {\r
187             connection = getConnection();\r
188             Set<String> actualTables = getTableSet(connection);\r
189             boolean initialize = false;\r
190             for (String table : expectedTables) {\r
191                 initialize |= !actualTables.contains(table.toLowerCase());\r
192             }\r
193             if (initialize) {\r
194                 intlogger.info("PROV9001: First time startup; The database is being initialized.");\r
195                 runInitScript(connection, 1);\r
196             }\r
197         } catch (SQLException e) {\r
198             intlogger\r
199                 .fatal("PROV9000: The database credentials are not working: " + e.getMessage());\r
200             return false;\r
201         } finally {\r
202             if (connection != null) {\r
203                 release(connection);\r
204             }\r
205         }\r
206         return true;\r
207     }\r
208 \r
209     /**\r
210      * Get a set of all table names in the DB.\r
211      *\r
212      * @param connection a DB connection\r
213      * @return the set of table names\r
214      */\r
215     private Set<String> getTableSet(Connection connection) {\r
216         Set<String> tables = new HashSet<String>();\r
217         try {\r
218             DatabaseMetaData md = connection.getMetaData();\r
219             ResultSet rs = md.getTables(null, null, "%", null);\r
220             if (rs != null) {\r
221                 while (rs.next()) {\r
222                     tables.add(rs.getString("TABLE_NAME"));\r
223                 }\r
224                 rs.close();\r
225             }\r
226         } catch (SQLException e) {\r
227             intlogger.fatal("PROV9010: Failed to get TABLE data from DB: " + e.getMessage());\r
228         }\r
229         return tables;\r
230     }\r
231 \r
232     /**\r
233      * Initialize the tables by running the initialization scripts located in the directory specified by the property\r
234      * <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>.  Scripts have names of the form\r
235      * sql_init_NN.sql\r
236      *\r
237      * @param connection a DB connection\r
238      * @param scriptId the number of the sql_init_NN.sql script to run\r
239      */\r
240     private void runInitScript(Connection connection, int scriptId) {\r
241         String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");\r
242         StringBuilder strBuilder = new StringBuilder();\r
243         try {\r
244             String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);\r
245             if (!(new File(scriptFile)).exists()) {\r
246                 intlogger.fatal("PROV9005 Failed to load sql script from : " + scriptFile);\r
247                 System.exit(1);\r
248             }\r
249             LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));\r
250             String line;\r
251             while ((line = lineReader.readLine()) != null) {\r
252                 if (!line.startsWith("--")) {\r
253                     line = line.trim();\r
254                     strBuilder.append(line);\r
255                     if (line.endsWith(";")) {\r
256                         // Execute one DDL statement\r
257                         String sql = strBuilder.toString();\r
258                         strBuilder.setLength(0);\r
259                         Statement statement = connection.createStatement();\r
260                         statement.execute(sql);\r
261                         statement.close();\r
262                     }\r
263                 }\r
264             }\r
265             lineReader.close();\r
266             strBuilder.setLength(0);\r
267         } catch (Exception e) {\r
268             intlogger.fatal("PROV9002 Error when initializing table: " + e.getMessage());\r
269             System.exit(1);\r
270         }\r
271     }\r
272 }\r