1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
25 package org.onap.dmaap.datarouter.provisioning.utils;
\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
45 import com.att.eelf.configuration.EELFLogger;
\r
46 import com.att.eelf.configuration.EELFManager;
\r
49 * Load the DB JDBC driver, and manage a simple pool of connections to the DB.
\r
51 * @author Robert Eby
\r
56 private static EELFLogger intlogger = EELFManager.getInstance().getLogger("InternalLog");
\r
58 private static String DB_URL;
\r
59 private static String DB_LOGIN;
\r
60 private static String DB_PASSWORD;
\r
61 private static Properties props;
\r
62 private static final Queue<Connection> queue = new LinkedList<>();
\r
64 private static String HTTPS_PORT;
\r
65 private static String HTTP_PORT;
\r
68 * Construct a DB object. If this is the very first creation of this object, it will load a copy of the properties
\r
69 * for the server, and attempt to load the JDBC driver for the database. If a fatal error occurs (e.g. either the
\r
70 * properties file or the DB driver is missing), the JVM will exit.
\r
73 if (props == null) {
\r
74 props = new Properties();
\r
76 props.load(new FileInputStream(System.getProperty(
\r
77 "org.onap.dmaap.datarouter.provserver.properties",
\r
78 "/opt/app/datartr/etc/provserver.properties")));
\r
79 String DB_DRIVER = (String) props.get("org.onap.dmaap.datarouter.db.driver");
\r
80 DB_URL = (String) props.get("org.onap.dmaap.datarouter.db.url");
\r
81 DB_LOGIN = (String) props.get("org.onap.dmaap.datarouter.db.login");
\r
82 DB_PASSWORD = (String) props.get("org.onap.dmaap.datarouter.db.password");
\r
83 HTTPS_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.https.port");
\r
84 HTTP_PORT = (String) props.get("org.onap.dmaap.datarouter.provserver.http.port");
\r
85 Class.forName(DB_DRIVER);
\r
86 } catch (IOException e) {
\r
87 intlogger.error("PROV9003 Opening properties: " + e.getMessage(), e);
\r
89 } catch (ClassNotFoundException e) {
\r
90 intlogger.error("PROV9004 cannot find the DB driver: " + e);
\r
97 * Get the provisioning server properties (loaded from provserver.properties).
\r
99 * @return the Properties object
\r
101 public Properties getProperties() {
\r
106 * Get a JDBC connection to the DB from the pool. Creates a new one if none are available.
\r
108 * @return the Connection
\r
110 @SuppressWarnings("resource")
\r
111 public Connection getConnection() throws SQLException {
\r
112 Connection connection = null;
\r
113 while (connection == null) {
\r
114 synchronized (queue) {
\r
116 connection = queue.remove();
\r
117 } catch (NoSuchElementException nseEx) {
\r
118 intlogger.error("PROV9006 No connection on queue: " + nseEx.getMessage(), nseEx);
\r
121 // Try up to 3 times to get a connection
\r
123 connection = DriverManager.getConnection(DB_URL, DB_LOGIN, DB_PASSWORD);
\r
124 } catch (SQLException sqlEx) {
\r
129 } while (connection == null);
\r
132 if (connection != null && !connection.isValid(1)) {
\r
133 connection.close();
\r
141 * Returns a JDBC connection to the pool.
\r
143 * @param connection the Connection to return
\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
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
159 * @return true if all retrofits worked, false otherwise
\r
161 public boolean runRetroFits() {
\r
162 return retroFit1();
\r
166 public static String getHttpsPort() {
\r
170 public static String getHttpPort() {
\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
177 * @return true if the retrofit worked, false otherwise
\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
185 Connection connection = null;
\r
187 connection = getConnection();
\r
188 Set<String> actualTables = getTableSet(connection);
\r
189 boolean initialize = false;
\r
190 for (String tableName : expectedTables) {
\r
191 initialize |= !actualTables.contains(tableName);
\r
194 intlogger.info("PROV9001: First time startup; The database is being initialized.");
\r
195 runInitScript(connection, 1);
\r
197 } catch (SQLException e) {
\r
198 intlogger.error("PROV9000: The database credentials are not working: " + e.getMessage(), e);
\r
201 if (connection != null) {
\r
202 release(connection);
\r
209 * Get a set of all table names in the DB.
\r
211 * @param connection a DB connection
\r
212 * @return the set of table names
\r
214 private Set<String> getTableSet(Connection connection) {
\r
215 Set<String> tables = new HashSet<>();
\r
217 DatabaseMetaData md = connection.getMetaData();
\r
218 ResultSet rs = md.getTables(null, null, "%", null);
\r
220 while (rs.next()) {
\r
221 tables.add(rs.getString("TABLE_NAME").toUpperCase());
\r
225 } catch (SQLException e) {
\r
226 intlogger.error("PROV9010: Failed to get TABLE data from DB: " + e.getMessage(), e);
\r
232 * Initialize the tables by running the initialization scripts located in the directory specified by the property
\r
233 * <i>org.onap.dmaap.datarouter.provserver.dbscripts</i>. Scripts have names of the form
\r
236 * @param connection a DB connection
\r
237 * @param scriptId the number of the sql_init_NN.sql script to run
\r
239 private void runInitScript(Connection connection, int scriptId) {
\r
240 String scriptDir = (String) props.get("org.onap.dmaap.datarouter.provserver.dbscripts");
\r
241 StringBuilder strBuilder = new StringBuilder();
\r
243 String scriptFile = String.format("%s/sql_init_%02d.sql", scriptDir, scriptId);
\r
244 if (!(new File(scriptFile)).exists()) {
\r
245 intlogger.error("PROV9005 Failed to load sql script from : " + scriptFile);
\r
248 LineNumberReader lineReader = new LineNumberReader(new FileReader(scriptFile));
\r
250 while ((line = lineReader.readLine()) != null) {
\r
251 if (!line.startsWith("--")) {
\r
252 line = line.trim();
\r
253 strBuilder.append(line);
\r
254 if (line.endsWith(";")) {
\r
255 // Execute one DDL statement
\r
256 String sql = strBuilder.toString();
\r
257 strBuilder.setLength(0);
\r
258 Statement statement = connection.createStatement();
\r
259 statement.execute(sql);
\r
264 lineReader.close();
\r
265 strBuilder.setLength(0);
\r
266 } catch (Exception e) {
\r
267 intlogger.error("PROV9002 Error when initializing table: " + e.getMessage(), e);
\r