2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 ONAP
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.sli.core.sli;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
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
45 .getLogger(SvcLogicJdbcStore.class);
47 private String dbUrl = null;
48 private String dbName = null;
49 private String dbUser = null;
50 private String dbPasswd = null;
51 private String dbDriver = null;
53 private Connection dbConn;
54 private PreparedStatement hasActiveGraphStmt = null;
55 private PreparedStatement hasVersionGraphStmt = null;
56 private PreparedStatement fetchActiveGraphStmt = null;
57 private PreparedStatement fetchVersionGraphStmt = null;
58 private PreparedStatement storeGraphStmt = null;
59 private PreparedStatement deleteGraphStmt = null;
61 private PreparedStatement deactivateStmt = null;
62 private PreparedStatement activateStmt = null;
64 private PreparedStatement registerNodeStmt = null;
65 private PreparedStatement unregisterNodeStmt = null;
66 private PreparedStatement validateNodeStmt = null;
68 private void getConnection() throws ConfigurationException
71 Properties jdbcProps = new Properties();
73 jdbcProps.setProperty("user", dbUser);
74 jdbcProps.setProperty("password", dbPasswd);
77 Driver dvr = new com.mysql.jdbc.Driver();
78 if (dvr.acceptsURL(dbUrl))
80 LOG.debug("Driver com.mysql.jdbc.Driver accepts "+dbUrl);
84 LOG.warn("Driver com.mysql.jdbc.Driver does not accept "+dbUrl);
86 } catch (SQLException e1) {
87 LOG.error("Caught exception trying to load com.mysql.jdbc.Driver", e1);
95 this.dbConn = DriverManager.getConnection(dbUrl, jdbcProps);
99 throw new ConfigurationException("failed to get database connection ["+dbUrl+"]", e);
104 private void createTable() throws ConfigurationException
108 DatabaseMetaData dbm = null;
112 dbm = dbConn.getMetaData();
113 } catch (SQLException e) {
115 throw new ConfigurationException("could not get databse metadata", e);
118 // See if table SVC_LOGIC exists. If not, create it.
123 ResultSet tables = dbm.getTables(null, null, "SVC_LOGIC", null);
129 String crTableCmd = "CREATE TABLE "+dbName+".SVC_LOGIC ("
130 + "module varchar(80) NOT NULL,"
131 + "rpc varchar(80) NOT NULL,"
132 + "version varchar(40) NOT NULL,"
133 + "mode varchar(5) NOT NULL,"
134 + "active varchar(1) NOT NULL,"
136 + "CONSTRAINT P_SVC_LOGIC PRIMARY KEY(module, rpc, version, mode))";
138 Statement stmt = null;
139 ConfigurationException myExc = null;
142 stmt = dbConn.createStatement();
143 stmt.executeUpdate(crTableCmd);
145 catch (SQLException e1)
147 myExc = new ConfigurationException("cannot create SVC_LOGIC table", e1);
165 throw new ConfigurationException("could not create SVC_LOGIC table", e);
168 // See if NODE_TYPES table exists and, if not, create it
174 ResultSet tables = dbm.getTables(null, null, "NODE_TYPES", null);
180 String crTableCmd = "CREATE TABLE "+dbName+".NODE_TYPES ("
181 + "nodetype varchar(80) NOT NULL,"
182 + "CONSTRAINT P_NODE_TYPES PRIMARY KEY(nodetype))";
184 Statement stmt = null;
185 ConfigurationException myExc = null;
188 stmt = dbConn.createStatement();
189 stmt.executeUpdate(crTableCmd);
191 catch (SQLException e1)
193 myExc = new ConfigurationException("cannot create SVC_LOGIC table", e1);
211 throw new ConfigurationException("could not create SVC_LOGIC table", e);
215 private void prepStatements() throws ConfigurationException
218 // Prepare statements
219 String hasVersionGraphSql = "SELECT count(*) FROM "+dbName+".SVC_LOGIC"
220 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
224 hasVersionGraphStmt = dbConn.prepareStatement(hasVersionGraphSql);
228 throw new ConfigurationException("could not prepare statement "+hasVersionGraphSql, e);
232 String hasActiveGraphSql = "SELECT count(*) FROM "+dbName+".SVC_LOGIC"
233 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
237 hasActiveGraphStmt = dbConn.prepareStatement(hasActiveGraphSql);
241 throw new ConfigurationException("could not prepare statement "+hasVersionGraphSql, e);
245 String fetchVersionGraphSql = "SELECT graph FROM "+dbName+".SVC_LOGIC"
246 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
250 fetchVersionGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
254 throw new ConfigurationException("could not prepare statement "+fetchVersionGraphSql, e);
258 String fetchActiveGraphSql = "SELECT graph FROM "+dbName+".SVC_LOGIC"
259 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
263 fetchActiveGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
267 throw new ConfigurationException("could not prepare statement "+fetchVersionGraphSql, e);
271 String storeGraphSql = "INSERT INTO "+dbName+".SVC_LOGIC (module, rpc, version, mode, active, graph)"
272 + " VALUES(?, ?, ?, ?, ?, ?)";
276 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
280 throw new ConfigurationException("could not prepare statement "+storeGraphSql, e);
283 String deleteGraphSql = "DELETE FROM "+dbName+".SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
287 deleteGraphStmt = dbConn.prepareStatement(deleteGraphSql);
291 throw new ConfigurationException("could not prepare statement "+deleteGraphSql, e);
294 String deactivateSql = "UPDATE "+dbName+".SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
298 deactivateStmt = dbConn.prepareStatement(deactivateSql);
302 throw new ConfigurationException("could not prepare statement "+deactivateSql, e);
305 String activateSql = "UPDATE "+dbName+".SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
309 activateStmt = dbConn.prepareStatement(activateSql);
313 throw new ConfigurationException("could not prepare statement "+activateSql, e);
316 String registerNodeSql = "INSERT INTO "+dbName+".NODE_TYPES (nodetype) VALUES(?)";
319 registerNodeStmt = dbConn.prepareStatement(registerNodeSql);
323 throw new ConfigurationException("could not prepare statement "+registerNodeSql, e);
326 String unregisterNodeSql = "DELETE FROM "+dbName+".NODE_TYPES WHERE nodetype = ?";
329 unregisterNodeStmt = dbConn.prepareStatement(unregisterNodeSql);
333 throw new ConfigurationException("could not prepare statement "+unregisterNodeSql, e);
336 String validateNodeSql = "SELECT count(*) FROM "+dbName+".NODE_TYPES WHERE nodetype = ?";
339 validateNodeStmt = dbConn.prepareStatement(validateNodeSql);
343 throw new ConfigurationException("could not prepare statement "+validateNodeSql, e);
347 private void initDbResources() throws ConfigurationException
349 if ((dbDriver != null) && (dbDriver.length() > 0))
354 Class.forName(dbDriver);
358 throw new ConfigurationException("could not load driver class "+dbDriver, e);
367 public void init(Properties props) throws ConfigurationException {
370 dbUrl = props.getProperty("org.openecomp.sdnc.sli.jdbc.url");
371 if ((dbUrl == null) || (dbUrl.length() == 0))
373 throw new ConfigurationException("property org.openecomp.sdnc.sli.jdbc.url unset");
376 dbName = props.getProperty("org.openecomp.sdnc.sli.jdbc.database");
377 if ((dbName == null) || (dbName.length() == 0))
379 throw new ConfigurationException("property org.openecomp.sdnc.sli.jdbc.database unset");
382 dbUser = props.getProperty("org.openecomp.sdnc.sli.jdbc.user");
383 if ((dbUser == null) || (dbUser.length() == 0))
385 throw new ConfigurationException("property org.openecomp.sdnc.sli.jdbc.user unset");
389 dbPasswd = props.getProperty("org.openecomp.sdnc.sli.jdbc.password");
390 if ((dbPasswd == null) || (dbPasswd.length() == 0))
392 throw new ConfigurationException("property org.openecomp.sdnc.sli.jdbc.password unset");
395 dbDriver = props.getProperty("org.openecomp.sdnc.sli.jdbc.driver");
402 private boolean isDbConnValid()
405 boolean isValid = false;
411 isValid = dbConn.isValid(1);
414 catch (SQLException e)
419 public boolean hasGraph(String module, String rpc, String version, String mode) throws SvcLogicException {
424 if (!isDbConnValid())
427 // Try reinitializing
430 if (!isDbConnValid())
432 throw new ConfigurationException("no jdbc connection");
438 boolean retval = false;
439 ResultSet results = null;
441 PreparedStatement hasGraphStmt = null;
444 hasGraphStmt = hasActiveGraphStmt;
448 hasGraphStmt = hasVersionGraphStmt;
455 hasGraphStmt.setString(1, module);
456 hasGraphStmt.setString(2, rpc);
457 hasGraphStmt.setString(3, mode);
462 hasGraphStmt.setString(4, version);
464 boolean oldAutoCommit = dbConn.getAutoCommit();
465 dbConn.setAutoCommit(false);
466 results = hasGraphStmt.executeQuery();
468 dbConn.setAutoCommit(oldAutoCommit);
472 int cnt = results.getInt(1);
483 throw new ConfigurationException("SQL query failed", e);
494 catch (SQLException x)
506 public SvcLogicGraph fetch(String module, String rpc, String version, String mode) throws SvcLogicException {
511 if (!isDbConnValid())
514 // Try reinitializing
517 if (!isDbConnValid())
519 throw new ConfigurationException("no jdbc connection");
525 SvcLogicGraph retval = null;
526 ResultSet results = null;
528 PreparedStatement fetchGraphStmt = null;
531 fetchGraphStmt = fetchActiveGraphStmt;
535 fetchGraphStmt = fetchVersionGraphStmt;
539 fetchGraphStmt.setString(1, module);
540 fetchGraphStmt.setString(2, rpc);
541 fetchGraphStmt.setString(3, mode);
546 fetchGraphStmt.setString(4, version);
548 boolean oldAutoCommit = dbConn.getAutoCommit();
549 dbConn.setAutoCommit(false);
550 results = fetchGraphStmt.executeQuery();
552 dbConn.setAutoCommit(oldAutoCommit);
556 Blob graphBlob = results.getBlob("graph");
558 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
560 Object graphObj = gStream.readObject();
563 if (graphObj instanceof SvcLogicGraph)
565 retval = (SvcLogicGraph) graphObj;
569 throw new ConfigurationException("invalid type for graph ("+graphObj.getClass().getName());
581 throw new ConfigurationException("SQL query failed", e);
591 catch (SQLException x)
603 public void store(SvcLogicGraph graph) throws SvcLogicException {
606 if (!isDbConnValid())
609 // Try reinitializing
612 if (!isDbConnValid())
614 throw new ConfigurationException("no jdbc connection");
620 throw new SvcLogicException("graph cannot be null");
623 byte[] graphBytes = null;
625 ByteArrayOutputStream byteStr = null;
626 ObjectOutputStream goutStr = null;
630 byteStr = new ByteArrayOutputStream();
631 goutStr = new ObjectOutputStream(byteStr);
632 goutStr.writeObject(graph);
634 graphBytes = byteStr.toByteArray();
639 throw new SvcLogicException("could not serialize graph", e);
648 } catch (IOException e) {
657 } catch (IOException e) {
664 // If object already stored in database, delete it
665 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode()))
667 delete(graph.getModule(), graph.getRpc(), graph.getVersion(), graph.getMode());
672 boolean oldAutoCommit = dbConn.getAutoCommit();
673 dbConn.setAutoCommit(false);
674 storeGraphStmt.setString(1, graph.getModule());
675 storeGraphStmt.setString(2, graph.getRpc());
676 storeGraphStmt.setString(3, graph.getVersion());
677 storeGraphStmt.setString(4, graph.getMode());
678 storeGraphStmt.setString(5, "N");
679 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
681 storeGraphStmt.executeUpdate();
684 dbConn.setAutoCommit(oldAutoCommit);
688 throw new SvcLogicException("Could not write object to database", e);
692 public void delete(String module, String rpc, String version, String mode) throws SvcLogicException
694 if (!isDbConnValid())
697 // Try reinitializing
700 if (!isDbConnValid())
702 throw new ConfigurationException("no jdbc connection");
708 boolean oldAutoCommit = dbConn.getAutoCommit();
709 dbConn.setAutoCommit(false);
710 deleteGraphStmt.setString(1, module);
711 deleteGraphStmt.setString(2, rpc);
712 deleteGraphStmt.setString(3, version);
713 deleteGraphStmt.setString(4, mode);
716 deleteGraphStmt.executeUpdate();
718 dbConn.setAutoCommit(oldAutoCommit);
722 throw new SvcLogicException("Could not delete object from database", e);
726 public void activate(SvcLogicGraph graph) throws SvcLogicException
730 boolean oldAutoCommit = dbConn.getAutoCommit();
732 dbConn.setAutoCommit(false);
734 // Deactivate any current active version
735 deactivateStmt.setString(1, graph.getModule());
736 deactivateStmt.setString(2, graph.getRpc());
737 deactivateStmt.setString(3, graph.getMode());
738 deactivateStmt.executeUpdate();
740 // Activate this version
741 activateStmt.setString(1, graph.getModule());
742 activateStmt.setString(2, graph.getRpc());
743 activateStmt.setString(3, graph.getVersion());
744 activateStmt.setString(4, graph.getMode());
745 activateStmt.executeUpdate();
749 dbConn.setAutoCommit(oldAutoCommit);
754 throw new SvcLogicException("Could not activate graph", e);
759 public void registerNodeType(String nodeType) throws SvcLogicException {
761 if (isValidNodeType(nodeType))
766 if (!isDbConnValid())
769 // Try reinitializing
772 if (!isDbConnValid())
774 throw new ConfigurationException("no jdbc connection");
780 boolean oldAutoCommit = dbConn.getAutoCommit();
781 dbConn.setAutoCommit(false);
782 registerNodeStmt.setString(1, nodeType);
783 registerNodeStmt.executeUpdate();
785 dbConn.setAutoCommit(oldAutoCommit);
789 throw new SvcLogicException("Could not add node type to database", e);
795 public void unregisterNodeType(String nodeType) throws SvcLogicException {
797 if (!isValidNodeType(nodeType))
802 if (!isDbConnValid())
805 // Try reinitializing
808 if (!isDbConnValid())
810 throw new ConfigurationException("no jdbc connection");
816 boolean oldAutoCommit = dbConn.getAutoCommit();
817 dbConn.setAutoCommit(false);
818 unregisterNodeStmt.setString(1, nodeType);
819 unregisterNodeStmt.executeUpdate();
821 dbConn.setAutoCommit(oldAutoCommit);
825 throw new SvcLogicException("Could not delete node type from database", e);
831 public boolean isValidNodeType(String nodeType) throws SvcLogicException {
833 boolean isValid = false;
835 if (!isDbConnValid())
838 // Try reinitializing
841 if (!isDbConnValid())
843 throw new ConfigurationException("no jdbc connection");
847 ResultSet results = null;
850 validateNodeStmt.setString(1, nodeType);
852 boolean oldAutoCommit = dbConn.getAutoCommit();
853 dbConn.setAutoCommit(false);
854 results = validateNodeStmt.executeQuery();
856 dbConn.setAutoCommit(oldAutoCommit);
862 int cnt = results.getInt(1);
874 throw new SvcLogicException("Cannot select node type from database", e);
884 catch (SQLException x)