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.PreparedStatement;
31 import java.sql.ResultSet;
32 import java.sql.SQLException;
33 import java.util.ArrayList;
34 import java.util.Properties;
35 import javax.sql.rowset.CachedRowSet;
36 import org.onap.ccsdk.sli.core.dblib.DbLibService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SvcLogicDblibStore implements SvcLogicStore {
41 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicDblibStore.class);
42 private DbLibService dbSvc;
44 public SvcLogicDblibStore(DbLibService dbsvc) {
48 public Connection getConnection() throws SQLException {
49 return(dbSvc.getConnection());
53 public void init(Properties props) throws ConfigurationException {
55 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
59 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
60 LOG.debug("SQL test was successful");
61 } catch (SQLException e) {
62 LOG.error("Failed SQL test", e);
67 public boolean hasGraph(String module, String rpc, String version,
68 String mode) throws SvcLogicException {
69 boolean retval = false;
70 CachedRowSet results = null;
71 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
72 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
74 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
75 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
77 ArrayList<String> args = new ArrayList<>();
84 if (version == null) {
85 results = dbSvc.getData(hasActiveGraphSql, args, null);
88 results = dbSvc.getData(hasVersionGraphSql, args, null);
92 int cnt = results.getInt(1);
98 } catch (Exception e) {
99 throw new ConfigurationException("SQL query failed", e);
101 if (results != null) {
104 } catch (SQLException x) {
105 LOG.error("Failed to close CachedRowSet", x);
114 public SvcLogicGraph fetch(String module, String rpc, String version,
115 String mode) throws SvcLogicException {
117 PreparedStatement fetchGraphStmt = null;
118 Connection dbConn = null;
119 SvcLogicGraph retval = null;
120 ResultSet results = null;
122 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
123 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
125 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
126 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
130 dbConn = dbSvc.getConnection();
132 if (version == null) {
133 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
135 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
138 fetchGraphStmt.setString(1, module);
139 fetchGraphStmt.setString(2, rpc);
140 fetchGraphStmt.setString(3, mode);
141 if (version != null) {
142 fetchGraphStmt.setString(4,version);
145 results = fetchGraphStmt.executeQuery();
147 if (results.next()) {
148 Blob graphBlob = results.getBlob("graph");
150 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
152 Object graphObj = gStream.readObject();
155 if (graphObj instanceof SvcLogicGraph) {
156 retval = (SvcLogicGraph) graphObj;
158 throw new ConfigurationException("invalid type for graph ("
159 + graphObj.getClass().getName());
166 } catch (Exception e) {
167 LOG.error("Graph processing failed", e);
168 throw new ConfigurationException("Graph processing failed: " + e.getMessage());
171 if (fetchGraphStmt != null) {
172 fetchGraphStmt.close();
174 } catch (SQLException e) {
175 LOG.error("PreparedStatement close error", e);
177 if (results != null) {
180 } catch (SQLException x) {
181 LOG.error("ResultSet close error", x);
185 if (dbConn != null && !dbConn.isClosed()) {
188 } catch (Exception exc) {
189 LOG.error("dbConn close error", exc);
199 public void store(SvcLogicGraph graph) throws SvcLogicException {
203 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph, md5sum)"
204 + " VALUES(?, ?, ?, ?, ?, ?, ?)";
207 throw new SvcLogicException("graph cannot be null");
210 byte[] graphBytes = null;
212 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
213 ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
215 goutStr.writeObject(graph);
217 graphBytes = byteStr.toByteArray();
219 } catch (Exception e) {
220 throw new SvcLogicException("could not serialize graph", e);
223 // If object already stored in database, delete it
224 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
226 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
230 Connection dbConn = null;
231 PreparedStatement storeGraphStmt = null;
233 dbConn = dbSvc.getConnection();
234 boolean oldAutoCommit = dbConn.getAutoCommit();
235 dbConn.setAutoCommit(false);
236 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
237 storeGraphStmt.setString(1, graph.getModule());
238 storeGraphStmt.setString(2, graph.getRpc());
239 storeGraphStmt.setString(3, graph.getVersion());
240 storeGraphStmt.setString(4, graph.getMode());
241 storeGraphStmt.setString(5, "N");
242 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
243 storeGraphStmt.setString(7, graph.getMd5sum());
244 storeGraphStmt.executeUpdate();
246 dbConn.setAutoCommit(oldAutoCommit);
247 } catch (Exception e) {
248 throw new SvcLogicException("Could not write object to database", e);
251 if (storeGraphStmt != null) {
252 storeGraphStmt.close();
254 } catch (SQLException e) {
255 LOG.error("PreparedStatement close error", e);
258 if (dbConn != null && !dbConn.isClosed()) {
261 } catch (Exception exc) {
262 LOG.error("dbConn close error", exc);
270 public void delete(String module, String rpc, String version, String mode)
271 throws SvcLogicException {
272 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
274 ArrayList<String> args = new ArrayList<>();
282 dbSvc.writeData(deleteGraphSql, args, null);
283 } catch (Exception e) {
284 throw new SvcLogicException("Could not delete object from database", e);
288 public void activate(SvcLogicGraph graph) throws SvcLogicException {
289 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
290 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
292 ArrayList<String> args = new ArrayList<>();
294 args.add(graph.getModule());
295 args.add(graph.getRpc());
296 args.add(graph.getMode());
299 dbSvc.writeData(deactivateSql, args, null);
300 args.add(graph.getVersion());
301 dbSvc.writeData(activateSql, args, null);
302 } catch (Exception e) {
303 throw new SvcLogicException("Could not activate graph", e);
308 public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
310 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
312 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
314 ArrayList<String> args = new ArrayList<>();
322 dbSvc.writeData(deactivateSql, args, null);
325 dbSvc.writeData(activateSql, args, null);
327 } catch (Exception e) {
328 throw new SvcLogicException("Could not activate graph", e);