aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java
diff options
context:
space:
mode:
authorDan Timoney <dtimoney@att.com>2017-12-05 11:50:42 -0500
committerDan Timoney <dtimoney@att.com>2017-12-05 11:50:42 -0500
commit13cbc4e6633e4cef9cc33d10c11b9b177213acd9 (patch)
tree1ef5785a6355f9491d7f2f9cf8fcbfaea3e66d40 /sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java
parent7cb74a4e851ae85ffa8c7cd1ebe94007418b816c (diff)
SLI parser improvements
Update SLI parser to remove validation based on NODE_TYPE table, which is no longer needed (was introduced prior to use of XSD schema validation). Also, use checksums to avoid needless recompilation if version being loaded already exists in database. Change-Id: Idfcba94de8fb71b17d5e0c5e69e04dee266988b1 Issue-ID: CCSDK-152 Signed-off-by: Dan Timoney <dtimoney@att.com>
Diffstat (limited to 'sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java')
-rw-r--r--sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java1280
1 files changed, 523 insertions, 757 deletions
diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java
index 1e4b71cd..e2fbdf72 100644
--- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java
+++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicJdbcStore.java
@@ -35,109 +35,90 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SvcLogicJdbcStore implements SvcLogicStore {
- private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
-
- private String dbUrl = null;
- private String dbName = null;
- private String dbUser = null;
- private String dbPasswd = null;
- private String dbDriver = null;
-
- private Connection dbConn;
- private PreparedStatement hasActiveGraphStmt = null;
- private PreparedStatement hasVersionGraphStmt = null;
- private PreparedStatement fetchActiveGraphStmt = null;
- private PreparedStatement fetchVersionGraphStmt = null;
- private PreparedStatement storeGraphStmt = null;
- private PreparedStatement deleteGraphStmt = null;
-
- private PreparedStatement deactivateStmt = null;
- private PreparedStatement activateStmt = null;
-
- private PreparedStatement registerNodeStmt = null;
- private PreparedStatement unregisterNodeStmt = null;
- private PreparedStatement validateNodeStmt = null;
-
- private void getConnection() throws ConfigurationException
- {
-
- Properties jdbcProps = new Properties();
-
- jdbcProps.setProperty("user", dbUser);
- jdbcProps.setProperty("password", dbPasswd);
-
- try {
- Driver dvr = new org.mariadb.jdbc.Driver();
- if (dvr.acceptsURL(dbUrl))
- {
- LOG.debug("Driver com.mysql.jdbc.Driver accepts {}", dbUrl);
- }
- else
- {
- LOG.warn("Driver com.mysql.jdbc.Driver does not accept {}", dbUrl);
- }
- } catch (SQLException e1) {
- LOG.error("Caught exception trying to load com.mysql.jdbc.Driver", e1);
- }
-
- try
- {
- this.dbConn = DriverManager.getConnection(dbUrl, jdbcProps);
- }
- catch (Exception e)
- {
- throw new ConfigurationException("failed to get database connection ["+dbUrl+"]", e);
- }
-
- }
-
- private void createTable() throws ConfigurationException
- {
-
- DatabaseMetaData dbm;
-
- try {
- dbm = dbConn.getMetaData();
- } catch (SQLException e) {
-
- throw new ConfigurationException("could not get databse metadata", e);
- }
-
- // See if table SVC_LOGIC exists. If not, create it.
+ private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
+
+ private String dbUrl = null;
+ private String dbName = null;
+ private String dbUser = null;
+ private String dbPasswd = null;
+ private String dbDriver = null;
+
+ private Connection dbConn;
+ private PreparedStatement hasActiveGraphStmt = null;
+ private PreparedStatement hasVersionGraphStmt = null;
+ private PreparedStatement fetchActiveGraphStmt = null;
+ private PreparedStatement fetchVersionGraphStmt = null;
+ private PreparedStatement storeGraphStmt = null;
+ private PreparedStatement deleteGraphStmt = null;
+
+ private PreparedStatement deactivateStmt = null;
+ private PreparedStatement activateStmt = null;
+
+ private PreparedStatement registerNodeStmt = null;
+ private PreparedStatement unregisterNodeStmt = null;
+ private PreparedStatement validateNodeStmt = null;
+
+ private void getConnection() throws ConfigurationException {
+
+ Properties jdbcProps = new Properties();
+
+ jdbcProps.setProperty("user", dbUser);
+ jdbcProps.setProperty("password", dbPasswd);
+
+ try {
+ Driver dvr = new org.mariadb.jdbc.Driver();
+ if (dvr.acceptsURL(dbUrl)) {
+ LOG.debug("Driver com.mysql.jdbc.Driver accepts {}", dbUrl);
+ } else {
+ LOG.warn("Driver com.mysql.jdbc.Driver does not accept {}", dbUrl);
+ }
+ } catch (SQLException e1) {
+ LOG.error("Caught exception trying to load com.mysql.jdbc.Driver", e1);
+ }
+
+ try {
+ this.dbConn = DriverManager.getConnection(dbUrl, jdbcProps);
+ } catch (Exception e) {
+ throw new ConfigurationException("failed to get database connection [" + dbUrl + "]", e);
+ }
+
+ }
+
+ private void createTable() throws ConfigurationException {
+
+ DatabaseMetaData dbm;
+
+ try {
+ dbm = dbConn.getMetaData();
+ } catch (SQLException e) {
+
+ throw new ConfigurationException("could not get databse metadata", e);
+ }
+
+ // See if table SVC_LOGIC exists. If not, create it.
Statement stmt = null;
- try
- {
+ try {
- ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
- if (tables.next()) {
+ ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
+ if (tables.next()) {
LOG.debug("SVC_LOGIC table already exists");
- }
- else {
- String crTableCmd = "CREATE TABLE "+dbName+".SVC_LOGIC ("
- + "module varchar(80) NOT NULL,"
- + "rpc varchar(80) NOT NULL,"
- + "version varchar(40) NOT NULL,"
- + "mode varchar(5) NOT NULL,"
- + "active varchar(1) NOT NULL,"
- + "graph BLOB,"
- + "CONSTRAINT P_SVC_LOGIC PRIMARY KEY(module, rpc, version, mode))";
+ } else {
+ String crTableCmd = "CREATE TABLE " + dbName + ".SVC_LOGIC (" + "module varchar(80) NOT NULL,"
+ + "rpc varchar(80) NOT NULL," + "version varchar(40) NOT NULL," + "mode varchar(5) NOT NULL,"
+ + "active varchar(1) NOT NULL," + "graph BLOB,"
+ + "CONSTRAINT P_SVC_LOGIC PRIMARY KEY(module, rpc, version, mode))";
stmt = dbConn.createStatement();
stmt.executeUpdate(crTableCmd);
- }
- }
- catch (Exception e)
- {
- throw new ConfigurationException("could not create SVC_LOGIC table", e);
- }
- finally
- {
+ }
+ } catch (Exception e) {
+ throw new ConfigurationException("could not create SVC_LOGIC table", e);
+ } finally {
if (stmt != null) {
try {
stmt.close();
@@ -147,31 +128,24 @@ public class SvcLogicJdbcStore implements SvcLogicStore {
}
}
- // See if NODE_TYPES table exists and, if not, create it
+ // See if NODE_TYPES table exists and, if not, create it
stmt = null;
- try
- {
+ try {
- ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
- if (tables.next()) {
+ ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
+ if (tables.next()) {
LOG.debug("NODE_TYPES table already exists");
- }
- else {
- String crTableCmd = "CREATE TABLE "+dbName+".NODE_TYPES ("
- + "nodetype varchar(80) NOT NULL,"
- + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
+ } else {
+ String crTableCmd = "CREATE TABLE " + dbName + ".NODE_TYPES (" + "nodetype varchar(80) NOT NULL,"
+ + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
stmt = dbConn.createStatement();
stmt.executeUpdate(crTableCmd);
- }
- }
- catch (Exception e)
- {
- throw new ConfigurationException("could not create SVC_LOGIC table", e);
- }
- finally
- {
+ }
+ } catch (Exception e) {
+ throw new ConfigurationException("could not create SVC_LOGIC table", e);
+ } finally {
if (stmt != null) {
try {
stmt.close();
@@ -180,652 +154,444 @@ public class SvcLogicJdbcStore implements SvcLogicStore {
}
}
}
- }
-
- private void prepStatements() throws ConfigurationException
- {
-
- // Prepare statements
- String hasVersionGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
- + CommonConstants.JDBC_GRAPH_QUERY;
-
- try
- {
- hasVersionGraphStmt = dbConn.prepareStatement(hasVersionGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
-
- }
-
- String hasActiveGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
- + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
-
- try
- {
- hasActiveGraphStmt = dbConn.prepareStatement(hasActiveGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
-
- }
-
- String fetchVersionGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName+CommonConstants.SVCLOGIC_TABLE
- + CommonConstants.JDBC_GRAPH_QUERY;
-
- try
- {
- fetchVersionGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
-
- }
-
- String fetchActiveGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName + CommonConstants.SVCLOGIC_TABLE
- + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
-
- try
- {
- fetchActiveGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
-
- }
-
- String storeGraphSql = CommonConstants.JDBC_INSERT+dbName
- + ".SVC_LOGIC (module, rpc, version, mode, active, graph) VALUES(?, ?, ?, ?, ?, ?)";
-
- try
- {
- storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + storeGraphSql, e);
- }
-
- String deleteGraphSql = CommonConstants.JDBC_DELETE+dbName
- + ".SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
-
- try
- {
- deleteGraphStmt = dbConn.prepareStatement(deleteGraphSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deleteGraphSql, e);
- }
-
- String deactivateSql = CommonConstants.JDBC_UPDATE + dbName
- +".SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
-
- try
- {
- deactivateStmt = dbConn.prepareStatement(deactivateSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deactivateSql, e);
- }
-
- String activateSql = CommonConstants.JDBC_UPDATE + dbName
- +".SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
-
- try
- {
- activateStmt = dbConn.prepareStatement(activateSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + activateSql, e);
- }
-
- String registerNodeSql = CommonConstants.JDBC_INSERT + dbName + ".NODE_TYPES (nodetype) VALUES(?)";
- try
- {
- registerNodeStmt = dbConn.prepareStatement(registerNodeSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + registerNodeSql, e);
- }
-
- String unregisterNodeSql = CommonConstants.JDBC_DELETE + dbName + ".NODE_TYPES WHERE nodetype = ?";
- try
- {
- unregisterNodeStmt = dbConn.prepareStatement(unregisterNodeSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + unregisterNodeSql, e);
- }
-
- String validateNodeSql = CommonConstants.JDBC_SELECT_COUNT + dbName + ".NODE_TYPES WHERE nodetype = ?";
- try
- {
- validateNodeStmt = dbConn.prepareStatement(validateNodeSql);
- }
- catch (Exception e)
- {
- throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + validateNodeSql, e);
- }
- }
-
- private void initDbResources() throws ConfigurationException
- {
- if ((dbDriver != null) && (dbDriver.length() > 0))
- {
-
- try
- {
- Class.forName(dbDriver);
- }
- catch (Exception e)
- {
- throw new ConfigurationException("could not load driver class "+dbDriver, e);
- }
- }
- getConnection();
- createTable();
- prepStatements();
- }
-
-
- @Override
- public void init(Properties props) throws ConfigurationException {
-
-
- dbUrl = props.getProperty("org.onap.ccsdk.sli.jdbc.url");
- if ((dbUrl == null) || (dbUrl.length() == 0))
- {
- throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.url unset");
- }
-
- dbName = props.getProperty("org.onap.ccsdk.sli.jdbc.database");
- if ((dbName == null) || (dbName.length() == 0))
- {
- throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.database unset");
- }
-
- dbUser = props.getProperty("org.onap.ccsdk.sli.jdbc.user");
- if ((dbUser == null) || (dbUser.length() == 0))
- {
- throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.user unset");
- }
-
-
- dbPasswd = props.getProperty("org.onap.ccsdk.sli.jdbc.password");
- if ((dbPasswd == null) || (dbPasswd.length() == 0))
- {
- throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.password unset");
- }
-
- dbDriver = props.getProperty("org.onap.ccsdk.sli.jdbc.driver");
-
-
- initDbResources();
-
- }
-
- private boolean isDbConnValid()
- {
-
- boolean isValid = false;
-
- try
- {
- if (dbConn != null)
- {
- isValid = dbConn.isValid(1);
- }
- }
- catch (SQLException e)
- {
- LOG.error("Not a valid db connection: ", e);
- }
-
- return isValid;
- }
-
- @Override
- public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- boolean retval = false;
- ResultSet results = null;
-
- PreparedStatement hasGraphStmt;
- if (version == null)
- {
- hasGraphStmt = hasActiveGraphStmt;
- }
- else
- {
- hasGraphStmt = hasVersionGraphStmt;
- }
-
- try
- {
- hasGraphStmt.setString(1, module);
- hasGraphStmt.setString(2, rpc);
- hasGraphStmt.setString(3, mode);
-
-
- if (version != null)
- {
- hasGraphStmt.setString(4, version);
- }
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- results = hasGraphStmt.executeQuery();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
-
- if (results.next())
- {
- int cnt = results.getInt(1);
-
- if (cnt > 0)
- {
- retval = true;
- }
-
- }
- }
- catch (Exception e)
- {
- throw new ConfigurationException("SQL query failed", e);
- }
- finally
- {
- if (results != null)
- {
- try
- {
- results.close();
- }
- catch (SQLException x)
- {
- LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
- }
- }
-
- }
-
- return retval;
-
- }
-
- @Override
- public SvcLogicGraph fetch(String module, String rpc, String version, String mode) throws SvcLogicException {
-
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- SvcLogicGraph retval = null;
- ResultSet results = null;
-
- PreparedStatement fetchGraphStmt;
- if (version == null)
- {
- fetchGraphStmt = fetchActiveGraphStmt;
- }
- else
- {
- fetchGraphStmt = fetchVersionGraphStmt;
- }
- try
- {
- fetchGraphStmt.setString(1, module);
- fetchGraphStmt.setString(2, rpc);
- fetchGraphStmt.setString(3, mode);
-
-
- if (version != null)
- {
- fetchGraphStmt.setString(4, version);
- }
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- results = fetchGraphStmt.executeQuery();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
-
- if (results.next())
- {
- Blob graphBlob = results.getBlob("graph");
-
- ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
-
- Object graphObj = gStream.readObject();
- gStream.close();
-
- if (graphObj instanceof SvcLogicGraph)
- {
- retval = (SvcLogicGraph) graphObj;
- }
- else
- {
- throw new ConfigurationException("invalid type for graph ("+graphObj.getClass().getName());
-
- }
- }
-
- }
- catch (Exception e)
- {
- throw new ConfigurationException("SQL query failed", e);
- }
- finally
- {
- if (results != null)
- {
- try
- {
- results.close();
- }
- catch (SQLException x)
- {
- LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
- }
- }
-
- }
-
- return retval;
- }
-
- public void store(SvcLogicGraph graph) throws SvcLogicException {
-
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- if (graph == null)
- {
- throw new SvcLogicException("graph cannot be null");
- }
-
- byte[] graphBytes;
-
- try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
- ObjectOutputStream goutStr = new ObjectOutputStream(byteStr))
- {
-
- goutStr.writeObject(graph);
-
- graphBytes = byteStr.toByteArray();
-
- }
- catch (Exception e)
- {
- throw new SvcLogicException("could not serialize graph", e);
- }
-
- // If object already stored in database, delete it
- if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode()))
- {
- delete(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode());
- }
-
- try
- {
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- storeGraphStmt.setString(1, graph.getModule());
- storeGraphStmt.setString(2, graph.getRpc());
- storeGraphStmt.setString(3, graph.getVersion());
- storeGraphStmt.setString(4, graph.getMode());
- storeGraphStmt.setString(5, "N");
- storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
-
- storeGraphStmt.executeUpdate();
- dbConn.commit();
-
- dbConn.setAutoCommit(oldAutoCommit);
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Could not write object to database", e);
- }
- }
-
- @Override
- public void delete(String module, String rpc, String version, String mode) throws SvcLogicException
- {
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- try
- {
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- deleteGraphStmt.setString(1, module);
- deleteGraphStmt.setString(2, rpc);
- deleteGraphStmt.setString(3, version);
- deleteGraphStmt.setString(4, mode);
-
-
- deleteGraphStmt.executeUpdate();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Could not delete object from database", e);
- }
- }
-
- @Override
- public void activate(SvcLogicGraph graph) throws SvcLogicException
- {
- try
- {
- boolean oldAutoCommit = dbConn.getAutoCommit();
-
- dbConn.setAutoCommit(false);
-
- // Deactivate any current active version
- deactivateStmt.setString(1, graph.getModule());
- deactivateStmt.setString(2, graph.getRpc());
- deactivateStmt.setString(3, graph.getMode());
- deactivateStmt.executeUpdate();
-
- // Activate this version
- activateStmt.setString(1, graph.getModule());
- activateStmt.setString(2, graph.getRpc());
- activateStmt.setString(3, graph.getVersion());
- activateStmt.setString(4, graph.getMode());
- activateStmt.executeUpdate();
-
- dbConn.commit();
-
- dbConn.setAutoCommit(oldAutoCommit);
-
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Could not activate graph", e);
- }
- }
-
- @Override
- public void registerNodeType(String nodeType) throws SvcLogicException {
-
- if (isValidNodeType(nodeType))
- {
- return;
- }
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- try
- {
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- registerNodeStmt.setString(1, nodeType);
- registerNodeStmt.executeUpdate();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Could not add node type to database", e);
- }
-
- }
-
- @Override
- public void unregisterNodeType(String nodeType) throws SvcLogicException {
-
- if (!isValidNodeType(nodeType))
- {
- return;
- }
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- try
- {
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- unregisterNodeStmt.setString(1, nodeType);
- unregisterNodeStmt.executeUpdate();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Could not delete node type from database", e);
- }
-
- }
-
- @Override
- public boolean isValidNodeType(String nodeType) throws SvcLogicException {
-
- boolean isValid = false;
-
- if (!isDbConnValid())
- {
-
- // Try reinitializing
- initDbResources();
-
- if (!isDbConnValid())
- {
- throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
- }
- }
-
- ResultSet results = null;
- try
- {
- validateNodeStmt.setString(1, nodeType);
-
- boolean oldAutoCommit = dbConn.getAutoCommit();
- dbConn.setAutoCommit(false);
- results = validateNodeStmt.executeQuery();
- dbConn.commit();
- dbConn.setAutoCommit(oldAutoCommit);
-
- if (results.next())
- {
- int cnt = results.getInt(1);
-
- if (cnt > 0)
- {
- isValid = true;
- }
- }
-
- }
- catch (Exception e)
- {
- throw new SvcLogicException("Cannot select node type from database", e);
- }
- finally
- {
- if (results != null)
- {
- try
- {
- results.close();
- }
- catch (SQLException x)
- {
- LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
- }
- }
-
- }
-
- return(isValid);
- }
+ }
+
+ private void prepStatements() throws ConfigurationException {
+
+ // Prepare statements
+ String hasVersionGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
+ + CommonConstants.JDBC_GRAPH_QUERY;
+
+ try {
+ hasVersionGraphStmt = dbConn.prepareStatement(hasVersionGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
+
+ }
+
+ String hasActiveGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
+ + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
+
+ try {
+ hasActiveGraphStmt = dbConn.prepareStatement(hasActiveGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
+
+ }
+
+ String fetchVersionGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName + CommonConstants.SVCLOGIC_TABLE
+ + CommonConstants.JDBC_GRAPH_QUERY;
+
+ try {
+ fetchVersionGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
+
+ }
+
+ String fetchActiveGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName + CommonConstants.SVCLOGIC_TABLE
+ + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
+
+ try {
+ fetchActiveGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
+
+ }
+
+ String storeGraphSql = CommonConstants.JDBC_INSERT + dbName
+ + ".SVC_LOGIC (module, rpc, version, mode, active, graph) VALUES(?, ?, ?, ?, ?, ?)";
+
+ try {
+ storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + storeGraphSql, e);
+ }
+
+ String deleteGraphSql = CommonConstants.JDBC_DELETE + dbName
+ + ".SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
+
+ try {
+ deleteGraphStmt = dbConn.prepareStatement(deleteGraphSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deleteGraphSql, e);
+ }
+
+ String deactivateSql = CommonConstants.JDBC_UPDATE + dbName
+ + ".SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
+
+ try {
+ deactivateStmt = dbConn.prepareStatement(deactivateSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deactivateSql, e);
+ }
+
+ String activateSql = CommonConstants.JDBC_UPDATE + dbName
+ + ".SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
+
+ try {
+ activateStmt = dbConn.prepareStatement(activateSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + activateSql, e);
+ }
+
+ String registerNodeSql = CommonConstants.JDBC_INSERT + dbName + ".NODE_TYPES (nodetype) VALUES(?)";
+ try {
+ registerNodeStmt = dbConn.prepareStatement(registerNodeSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + registerNodeSql, e);
+ }
+
+ String unregisterNodeSql = CommonConstants.JDBC_DELETE + dbName + ".NODE_TYPES WHERE nodetype = ?";
+ try {
+ unregisterNodeStmt = dbConn.prepareStatement(unregisterNodeSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + unregisterNodeSql, e);
+ }
+
+ String validateNodeSql = CommonConstants.JDBC_SELECT_COUNT + dbName + ".NODE_TYPES WHERE nodetype = ?";
+ try {
+ validateNodeStmt = dbConn.prepareStatement(validateNodeSql);
+ } catch (Exception e) {
+ throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + validateNodeSql, e);
+ }
+ }
+
+ private void initDbResources() throws ConfigurationException {
+ if ((dbDriver != null) && (dbDriver.length() > 0)) {
+
+ try {
+ Class.forName(dbDriver);
+ } catch (Exception e) {
+ throw new ConfigurationException("could not load driver class " + dbDriver, e);
+ }
+ }
+ getConnection();
+ createTable();
+ prepStatements();
+ }
+
+
+ @Override
+ public void init(Properties props) throws ConfigurationException {
+
+
+ dbUrl = props.getProperty("org.onap.ccsdk.sli.jdbc.url");
+ if ((dbUrl == null) || (dbUrl.length() == 0)) {
+ throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.url unset");
+ }
+
+ dbName = props.getProperty("org.onap.ccsdk.sli.jdbc.database");
+ if ((dbName == null) || (dbName.length() == 0)) {
+ throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.database unset");
+ }
+
+ dbUser = props.getProperty("org.onap.ccsdk.sli.jdbc.user");
+ if ((dbUser == null) || (dbUser.length() == 0)) {
+ throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.user unset");
+ }
+
+
+ dbPasswd = props.getProperty("org.onap.ccsdk.sli.jdbc.password");
+ if ((dbPasswd == null) || (dbPasswd.length() == 0)) {
+ throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.password unset");
+ }
+
+ dbDriver = props.getProperty("org.onap.ccsdk.sli.jdbc.driver");
+
+
+ initDbResources();
+
+ }
+
+ private boolean isDbConnValid() {
+
+ boolean isValid = false;
+
+ try {
+ if (dbConn != null) {
+ isValid = dbConn.isValid(1);
+ }
+ } catch (SQLException e) {
+ LOG.error("Not a valid db connection: ", e);
+ }
+
+ return isValid;
+ }
+
+ @Override
+ public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
+
+ if (!isDbConnValid()) {
+
+ // Try reinitializing
+ initDbResources();
+
+ if (!isDbConnValid()) {
+ throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
+ }
+ }
+
+ boolean retval = false;
+ ResultSet results = null;
+
+ PreparedStatement hasGraphStmt;
+ if (version == null) {
+ hasGraphStmt = hasActiveGraphStmt;
+ } else {
+ hasGraphStmt = hasVersionGraphStmt;
+ }
+
+ try {
+ hasGraphStmt.setString(1, module);
+ hasGraphStmt.setString(2, rpc);
+ hasGraphStmt.setString(3, mode);
+
+
+ if (version != null) {
+ hasGraphStmt.setString(4, version);
+ }
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+ dbConn.setAutoCommit(false);
+ results = hasGraphStmt.executeQuery();
+ dbConn.commit();
+ dbConn.setAutoCommit(oldAutoCommit);
+
+ if (results.next()) {
+ int cnt = results.getInt(1);
+
+ if (cnt > 0) {
+ retval = true;
+ }
+
+ }
+ } catch (Exception e) {
+ throw new ConfigurationException("SQL query failed", e);
+ } finally {
+ if (results != null) {
+ try {
+ results.close();
+ } catch (SQLException x) {
+ LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
+ }
+ }
+
+ }
+
+ return retval;
+
+ }
+
+ @Override
+ public SvcLogicGraph fetch(String module, String rpc, String version, String mode) throws SvcLogicException {
+
+
+ if (!isDbConnValid()) {
+
+ // Try reinitializing
+ initDbResources();
+
+ if (!isDbConnValid()) {
+ throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
+ }
+ }
+
+ SvcLogicGraph retval = null;
+ ResultSet results = null;
+
+ PreparedStatement fetchGraphStmt;
+ if (version == null) {
+ fetchGraphStmt = fetchActiveGraphStmt;
+ } else {
+ fetchGraphStmt = fetchVersionGraphStmt;
+ }
+ try {
+ fetchGraphStmt.setString(1, module);
+ fetchGraphStmt.setString(2, rpc);
+ fetchGraphStmt.setString(3, mode);
+
+
+ if (version != null) {
+ fetchGraphStmt.setString(4, version);
+ }
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+ dbConn.setAutoCommit(false);
+ results = fetchGraphStmt.executeQuery();
+ dbConn.commit();
+ dbConn.setAutoCommit(oldAutoCommit);
+
+ if (results.next()) {
+ Blob graphBlob = results.getBlob("graph");
+
+ ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
+
+ Object graphObj = gStream.readObject();
+ gStream.close();
+
+ if (graphObj instanceof SvcLogicGraph) {
+ retval = (SvcLogicGraph) graphObj;
+ } else {
+ throw new ConfigurationException("invalid type for graph (" + graphObj.getClass().getName());
+
+ }
+ }
+
+ } catch (Exception e) {
+ throw new ConfigurationException("SQL query failed", e);
+ } finally {
+ if (results != null) {
+ try {
+ results.close();
+ } catch (SQLException x) {
+ LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
+ }
+ }
+
+ }
+
+ return retval;
+ }
+
+ public void store(SvcLogicGraph graph) throws SvcLogicException {
+
+
+ if (!isDbConnValid()) {
+
+ // Try reinitializing
+ initDbResources();
+
+ if (!isDbConnValid()) {
+ throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
+ }
+ }
+
+ if (graph == null) {
+ throw new SvcLogicException("graph cannot be null");
+ }
+
+ byte[] graphBytes;
+
+ try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
+ ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
+
+ goutStr.writeObject(graph);
+
+ graphBytes = byteStr.toByteArray();
+
+ } catch (Exception e) {
+ throw new SvcLogicException("could not serialize graph", e);
+ }
+
+ // If object already stored in database, delete it
+ if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode())) {
+ delete(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode());
+ }
+
+ try {
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+ dbConn.setAutoCommit(false);
+ storeGraphStmt.setString(1, graph.getModule());
+ storeGraphStmt.setString(2, graph.getRpc());
+ storeGraphStmt.setString(3, graph.getVersion());
+ storeGraphStmt.setString(4, graph.getMode());
+ storeGraphStmt.setString(5, "N");
+ storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
+
+ storeGraphStmt.executeUpdate();
+ dbConn.commit();
+
+ dbConn.setAutoCommit(oldAutoCommit);
+ } catch (Exception e) {
+ throw new SvcLogicException("Could not write object to database", e);
+ }
+ }
+
+ @Override
+ public void delete(String module, String rpc, String version, String mode) throws SvcLogicException {
+ if (!isDbConnValid()) {
+
+ // Try reinitializing
+ initDbResources();
+
+ if (!isDbConnValid()) {
+ throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
+ }
+ }
+
+ try {
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+ dbConn.setAutoCommit(false);
+ deleteGraphStmt.setString(1, module);
+ deleteGraphStmt.setString(2, rpc);
+ deleteGraphStmt.setString(3, version);
+ deleteGraphStmt.setString(4, mode);
+
+
+ deleteGraphStmt.executeUpdate();
+ dbConn.commit();
+ dbConn.setAutoCommit(oldAutoCommit);
+ } catch (Exception e) {
+ throw new SvcLogicException("Could not delete object from database", e);
+ }
+ }
+
+ @Override
+ public void activate(SvcLogicGraph graph) throws SvcLogicException {
+ try {
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+
+ dbConn.setAutoCommit(false);
+
+ // Deactivate any current active version
+ deactivateStmt.setString(1, graph.getModule());
+ deactivateStmt.setString(2, graph.getRpc());
+ deactivateStmt.setString(3, graph.getMode());
+ deactivateStmt.executeUpdate();
+
+ // Activate this version
+ activateStmt.setString(1, graph.getModule());
+ activateStmt.setString(2, graph.getRpc());
+ activateStmt.setString(3, graph.getVersion());
+ activateStmt.setString(4, graph.getMode());
+ activateStmt.executeUpdate();
+
+ dbConn.commit();
+
+ dbConn.setAutoCommit(oldAutoCommit);
+
+ } catch (Exception e) {
+ throw new SvcLogicException("Could not activate graph", e);
+ }
+ }
+
+ @Override
+ public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
+ try {
+ boolean oldAutoCommit = dbConn.getAutoCommit();
+
+ dbConn.setAutoCommit(false);
+
+ // Deactivate any current active version
+ deactivateStmt.setString(1, module);
+ deactivateStmt.setString(2, rpc);
+ deactivateStmt.setString(3, mode);
+ deactivateStmt.executeUpdate();
+
+ // Activate this version
+ activateStmt.setString(1, module);
+ activateStmt.setString(2, rpc);
+ activateStmt.setString(3, version);
+ activateStmt.setString(4, mode);
+ activateStmt.executeUpdate();
+
+ dbConn.commit();
+
+ dbConn.setAutoCommit(oldAutoCommit);
+
+ } catch (Exception e) {
+ throw new SvcLogicException("Could not activate graph", e);
+ }
+ }
+
}