2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.core.sli;
24 import java.io.ByteArrayInputStream;
25 import java.io.ByteArrayOutputStream;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
29 import java.sql.Connection;
30 import java.sql.DatabaseMetaData;
31 import java.sql.Driver;
32 import java.sql.DriverManager;
33 import java.sql.PreparedStatement;
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import java.util.Properties;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 public class SvcLogicJdbcStore implements SvcLogicStore {
44 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicJdbcStore.class);
46 private String dbUrl = null;
47 private String dbName = null;
48 private String dbUser = null;
49 private String dbPasswd = null;
50 private String dbDriver = null;
52 private Connection dbConn;
53 private PreparedStatement hasActiveGraphStmt = null;
54 private PreparedStatement hasVersionGraphStmt = null;
55 private PreparedStatement fetchActiveGraphStmt = null;
56 private PreparedStatement fetchVersionGraphStmt = null;
57 private PreparedStatement storeGraphStmt = null;
58 private PreparedStatement deleteGraphStmt = null;
60 private PreparedStatement deactivateStmt = null;
61 private PreparedStatement activateStmt = null;
63 private PreparedStatement registerNodeStmt = null;
64 private PreparedStatement unregisterNodeStmt = null;
65 private PreparedStatement validateNodeStmt = null;
67 private void getConnection() throws ConfigurationException
70 Properties jdbcProps = new Properties();
72 jdbcProps.setProperty("user", dbUser);
73 jdbcProps.setProperty("password", dbPasswd);
76 Driver dvr = new org.mariadb.jdbc.Driver();
77 if (dvr.acceptsURL(dbUrl))
79 LOG.debug("Driver com.mysql.jdbc.Driver accepts {}", dbUrl);
83 LOG.warn("Driver com.mysql.jdbc.Driver does not accept {}", dbUrl);
85 } catch (SQLException e1) {
86 LOG.error("Caught exception trying to load com.mysql.jdbc.Driver", e1);
91 this.dbConn = DriverManager.getConnection(dbUrl, jdbcProps);
95 throw new ConfigurationException("failed to get database connection ["+dbUrl+"]", e);
100 private void createTable() throws ConfigurationException
103 DatabaseMetaData dbm;
106 dbm = dbConn.getMetaData();
107 } catch (SQLException e) {
109 throw new ConfigurationException("could not get databse metadata", e);
112 // See if table SVC_LOGIC exists. If not, create it.
113 Statement stmt = null;
117 ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
119 LOG.debug("SVC_LOGIC table already exists");
122 String crTableCmd = "CREATE TABLE "+dbName+".SVC_LOGIC ("
123 + "module varchar(80) NOT NULL,"
124 + "rpc varchar(80) NOT NULL,"
125 + "version varchar(40) NOT NULL,"
126 + "mode varchar(5) NOT NULL,"
127 + "active varchar(1) NOT NULL,"
129 + "CONSTRAINT P_SVC_LOGIC PRIMARY KEY(module, rpc, version, mode))";
131 stmt = dbConn.createStatement();
132 stmt.executeUpdate(crTableCmd);
137 throw new ConfigurationException("could not create SVC_LOGIC table", e);
144 } catch (SQLException e) {
145 LOG.error("Statement close error ", e);
150 // See if NODE_TYPES table exists and, if not, create it
155 ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
157 LOG.debug("NODE_TYPES table already exists");
160 String crTableCmd = "CREATE TABLE "+dbName+".NODE_TYPES ("
161 + "nodetype varchar(80) NOT NULL,"
162 + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
164 stmt = dbConn.createStatement();
166 stmt.executeUpdate(crTableCmd);
171 throw new ConfigurationException("could not create SVC_LOGIC table", e);
178 } catch (SQLException e) {
179 LOG.error("Statement close error ", e);
185 private void prepStatements() throws ConfigurationException
188 // Prepare statements
189 String hasVersionGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
190 + CommonConstants.JDBC_GRAPH_QUERY;
194 hasVersionGraphStmt = dbConn.prepareStatement(hasVersionGraphSql);
198 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
202 String hasActiveGraphSql = CommonConstants.JDBC_SELECT_COUNT + dbName + CommonConstants.SVCLOGIC_TABLE
203 + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
207 hasActiveGraphStmt = dbConn.prepareStatement(hasActiveGraphSql);
211 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + hasVersionGraphSql, e);
215 String fetchVersionGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName+CommonConstants.SVCLOGIC_TABLE
216 + CommonConstants.JDBC_GRAPH_QUERY;
220 fetchVersionGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
224 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
228 String fetchActiveGraphSql = CommonConstants.JDBC_SELECT_GRAPGH + dbName + CommonConstants.SVCLOGIC_TABLE
229 + CommonConstants.JDBC_ACTIVE_GRAPH_QUERY;
233 fetchActiveGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
237 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + fetchVersionGraphSql, e);
241 String storeGraphSql = CommonConstants.JDBC_INSERT+dbName
242 + ".SVC_LOGIC (module, rpc, version, mode, active, graph) VALUES(?, ?, ?, ?, ?, ?)";
246 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
250 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + storeGraphSql, e);
253 String deleteGraphSql = CommonConstants.JDBC_DELETE+dbName
254 + ".SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
258 deleteGraphStmt = dbConn.prepareStatement(deleteGraphSql);
262 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deleteGraphSql, e);
265 String deactivateSql = CommonConstants.JDBC_UPDATE + dbName
266 +".SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
270 deactivateStmt = dbConn.prepareStatement(deactivateSql);
274 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + deactivateSql, e);
277 String activateSql = CommonConstants.JDBC_UPDATE + dbName
278 +".SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
282 activateStmt = dbConn.prepareStatement(activateSql);
286 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + activateSql, e);
289 String registerNodeSql = CommonConstants.JDBC_INSERT + dbName + ".NODE_TYPES (nodetype) VALUES(?)";
292 registerNodeStmt = dbConn.prepareStatement(registerNodeSql);
296 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + registerNodeSql, e);
299 String unregisterNodeSql = CommonConstants.JDBC_DELETE + dbName + ".NODE_TYPES WHERE nodetype = ?";
302 unregisterNodeStmt = dbConn.prepareStatement(unregisterNodeSql);
306 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + unregisterNodeSql, e);
309 String validateNodeSql = CommonConstants.JDBC_SELECT_COUNT + dbName + ".NODE_TYPES WHERE nodetype = ?";
312 validateNodeStmt = dbConn.prepareStatement(validateNodeSql);
316 throw new ConfigurationException(CommonConstants.JDBC_STATEMENT_ERR + validateNodeSql, e);
320 private void initDbResources() throws ConfigurationException
322 if ((dbDriver != null) && (dbDriver.length() > 0))
327 Class.forName(dbDriver);
331 throw new ConfigurationException("could not load driver class "+dbDriver, e);
341 public void init(Properties props) throws ConfigurationException {
344 dbUrl = props.getProperty("org.onap.ccsdk.sli.jdbc.url");
345 if ((dbUrl == null) || (dbUrl.length() == 0))
347 throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.url unset");
350 dbName = props.getProperty("org.onap.ccsdk.sli.jdbc.database");
351 if ((dbName == null) || (dbName.length() == 0))
353 throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.database unset");
356 dbUser = props.getProperty("org.onap.ccsdk.sli.jdbc.user");
357 if ((dbUser == null) || (dbUser.length() == 0))
359 throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.user unset");
363 dbPasswd = props.getProperty("org.onap.ccsdk.sli.jdbc.password");
364 if ((dbPasswd == null) || (dbPasswd.length() == 0))
366 throw new ConfigurationException("property org.onap.ccsdk.sli.jdbc.password unset");
369 dbDriver = props.getProperty("org.onap.ccsdk.sli.jdbc.driver");
376 private boolean isDbConnValid()
379 boolean isValid = false;
385 isValid = dbConn.isValid(1);
388 catch (SQLException e)
390 LOG.error("Not a valid db connection: ", e);
397 public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
399 if (!isDbConnValid())
402 // Try reinitializing
405 if (!isDbConnValid())
407 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
411 boolean retval = false;
412 ResultSet results = null;
414 PreparedStatement hasGraphStmt;
417 hasGraphStmt = hasActiveGraphStmt;
421 hasGraphStmt = hasVersionGraphStmt;
426 hasGraphStmt.setString(1, module);
427 hasGraphStmt.setString(2, rpc);
428 hasGraphStmt.setString(3, mode);
433 hasGraphStmt.setString(4, version);
435 boolean oldAutoCommit = dbConn.getAutoCommit();
436 dbConn.setAutoCommit(false);
437 results = hasGraphStmt.executeQuery();
439 dbConn.setAutoCommit(oldAutoCommit);
443 int cnt = results.getInt(1);
454 throw new ConfigurationException("SQL query failed", e);
464 catch (SQLException x)
466 LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
477 public SvcLogicGraph fetch(String module, String rpc, String version, String mode) throws SvcLogicException {
480 if (!isDbConnValid())
483 // Try reinitializing
486 if (!isDbConnValid())
488 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
492 SvcLogicGraph retval = null;
493 ResultSet results = null;
495 PreparedStatement fetchGraphStmt;
498 fetchGraphStmt = fetchActiveGraphStmt;
502 fetchGraphStmt = fetchVersionGraphStmt;
506 fetchGraphStmt.setString(1, module);
507 fetchGraphStmt.setString(2, rpc);
508 fetchGraphStmt.setString(3, mode);
513 fetchGraphStmt.setString(4, version);
515 boolean oldAutoCommit = dbConn.getAutoCommit();
516 dbConn.setAutoCommit(false);
517 results = fetchGraphStmt.executeQuery();
519 dbConn.setAutoCommit(oldAutoCommit);
523 Blob graphBlob = results.getBlob("graph");
525 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
527 Object graphObj = gStream.readObject();
530 if (graphObj instanceof SvcLogicGraph)
532 retval = (SvcLogicGraph) graphObj;
536 throw new ConfigurationException("invalid type for graph ("+graphObj.getClass().getName());
544 throw new ConfigurationException("SQL query failed", e);
554 catch (SQLException x)
556 LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);
565 public void store(SvcLogicGraph graph) throws SvcLogicException {
568 if (!isDbConnValid())
571 // Try reinitializing
574 if (!isDbConnValid())
576 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
582 throw new SvcLogicException("graph cannot be null");
587 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
588 ObjectOutputStream goutStr = new ObjectOutputStream(byteStr))
591 goutStr.writeObject(graph);
593 graphBytes = byteStr.toByteArray();
598 throw new SvcLogicException("could not serialize graph", e);
601 // If object already stored in database, delete it
602 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode()))
604 delete(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode());
609 boolean oldAutoCommit = dbConn.getAutoCommit();
610 dbConn.setAutoCommit(false);
611 storeGraphStmt.setString(1, graph.getModule());
612 storeGraphStmt.setString(2, graph.getRpc());
613 storeGraphStmt.setString(3, graph.getVersion());
614 storeGraphStmt.setString(4, graph.getMode());
615 storeGraphStmt.setString(5, "N");
616 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
618 storeGraphStmt.executeUpdate();
621 dbConn.setAutoCommit(oldAutoCommit);
625 throw new SvcLogicException("Could not write object to database", e);
630 public void delete(String module, String rpc, String version, String mode) throws SvcLogicException
632 if (!isDbConnValid())
635 // Try reinitializing
638 if (!isDbConnValid())
640 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
646 boolean oldAutoCommit = dbConn.getAutoCommit();
647 dbConn.setAutoCommit(false);
648 deleteGraphStmt.setString(1, module);
649 deleteGraphStmt.setString(2, rpc);
650 deleteGraphStmt.setString(3, version);
651 deleteGraphStmt.setString(4, mode);
654 deleteGraphStmt.executeUpdate();
656 dbConn.setAutoCommit(oldAutoCommit);
660 throw new SvcLogicException("Could not delete object from database", e);
665 public void activate(SvcLogicGraph graph) throws SvcLogicException
669 boolean oldAutoCommit = dbConn.getAutoCommit();
671 dbConn.setAutoCommit(false);
673 // Deactivate any current active version
674 deactivateStmt.setString(1, graph.getModule());
675 deactivateStmt.setString(2, graph.getRpc());
676 deactivateStmt.setString(3, graph.getMode());
677 deactivateStmt.executeUpdate();
679 // Activate this version
680 activateStmt.setString(1, graph.getModule());
681 activateStmt.setString(2, graph.getRpc());
682 activateStmt.setString(3, graph.getVersion());
683 activateStmt.setString(4, graph.getMode());
684 activateStmt.executeUpdate();
688 dbConn.setAutoCommit(oldAutoCommit);
693 throw new SvcLogicException("Could not activate graph", e);
698 public void registerNodeType(String nodeType) throws SvcLogicException {
700 if (isValidNodeType(nodeType))
705 if (!isDbConnValid())
708 // Try reinitializing
711 if (!isDbConnValid())
713 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
719 boolean oldAutoCommit = dbConn.getAutoCommit();
720 dbConn.setAutoCommit(false);
721 registerNodeStmt.setString(1, nodeType);
722 registerNodeStmt.executeUpdate();
724 dbConn.setAutoCommit(oldAutoCommit);
728 throw new SvcLogicException("Could not add node type to database", e);
734 public void unregisterNodeType(String nodeType) throws SvcLogicException {
736 if (!isValidNodeType(nodeType))
741 if (!isDbConnValid())
744 // Try reinitializing
747 if (!isDbConnValid())
749 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
755 boolean oldAutoCommit = dbConn.getAutoCommit();
756 dbConn.setAutoCommit(false);
757 unregisterNodeStmt.setString(1, nodeType);
758 unregisterNodeStmt.executeUpdate();
760 dbConn.setAutoCommit(oldAutoCommit);
764 throw new SvcLogicException("Could not delete node type from database", e);
770 public boolean isValidNodeType(String nodeType) throws SvcLogicException {
772 boolean isValid = false;
774 if (!isDbConnValid())
777 // Try reinitializing
780 if (!isDbConnValid())
782 throw new ConfigurationException(CommonConstants.JDBC_CONN_ERR);
786 ResultSet results = null;
789 validateNodeStmt.setString(1, nodeType);
791 boolean oldAutoCommit = dbConn.getAutoCommit();
792 dbConn.setAutoCommit(false);
793 results = validateNodeStmt.executeQuery();
795 dbConn.setAutoCommit(oldAutoCommit);
799 int cnt = results.getInt(1);
810 throw new SvcLogicException("Cannot select node type from database", e);
820 catch (SQLException x)
822 LOG.error(CommonConstants.RESULTSET_CLOSE_ERR, x);