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;
27 import java.io.FileInputStream;
28 import java.io.ObjectInputStream;
29 import java.io.ObjectOutputStream;
31 import java.sql.Connection;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.util.ArrayList;
36 import java.util.Properties;
37 import javax.sql.rowset.CachedRowSet;
38 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
39 import org.onap.ccsdk.sli.core.dblib.DbLibService;
40 import org.osgi.framework.Bundle;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.FrameworkUtil;
43 import org.osgi.framework.ServiceReference;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class SvcLogicDblibStore implements SvcLogicStore {
49 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
51 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicDblibStore.class);
53 private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DbLibService";
55 private DbLibService dbSvc;
57 public SvcLogicDblibStore()
59 // Does nothing, but needed so that argumentless constructor
63 public SvcLogicDblibStore(DbLibService dbsvc) {
67 public SvcLogicDblibStore(Properties props) {
69 dbSvc = new DBResourceManager(props);
70 JavaSingleton.setInstance(dbSvc);
71 } catch (Exception e) {
72 LOG.warn("Caught exception trying to create DBResourceManager", e);
76 public Connection getConnection() throws SQLException {
77 return(dbSvc.getConnection());
81 public void init(Properties props) throws ConfigurationException {
83 dbSvc = getDbLibService();
85 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
89 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
90 LOG.debug("SQL test was successful");
91 } catch (SQLException e) {
92 LOG.error("Failed SQL test", e);
97 public boolean hasGraph(String module, String rpc, String version,
98 String mode) throws SvcLogicException {
99 boolean retval = false;
100 CachedRowSet results = null;
101 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
102 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
104 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
105 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
107 ArrayList<String> args = new ArrayList<>();
114 if (version == null) {
115 results = dbSvc.getData(hasActiveGraphSql, args, null);
118 results = dbSvc.getData(hasVersionGraphSql, args, null);
121 if (results.next()) {
122 int cnt = results.getInt(1);
128 } catch (Exception e) {
129 throw new ConfigurationException("SQL query failed", e);
131 if (results != null) {
134 } catch (SQLException x) {
135 LOG.error("Failed to close CachedRowSet", x);
144 public SvcLogicGraph fetch(String module, String rpc, String version,
145 String mode) throws SvcLogicException {
147 PreparedStatement fetchGraphStmt = null;
148 Connection dbConn = null;
149 SvcLogicGraph retval = null;
150 ResultSet results = null;
152 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
153 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
155 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
156 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
160 dbConn = dbSvc.getConnection();
162 if (version == null) {
163 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
165 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
168 fetchGraphStmt.setString(1, module);
169 fetchGraphStmt.setString(2, rpc);
170 fetchGraphStmt.setString(3, mode);
171 if (version != null) {
172 fetchGraphStmt.setString(4,version);
175 results = fetchGraphStmt.executeQuery();
177 if (results.next()) {
178 Blob graphBlob = results.getBlob("graph");
180 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
182 Object graphObj = gStream.readObject();
185 if (graphObj instanceof SvcLogicGraph) {
186 retval = (SvcLogicGraph) graphObj;
188 throw new ConfigurationException("invalid type for graph ("
189 + graphObj.getClass().getName());
196 } catch (Exception e) {
197 LOG.error("Graph processing failed", e);
198 throw new ConfigurationException("Graph processing failed: " + e.getMessage());
201 if (fetchGraphStmt != null) {
202 fetchGraphStmt.close();
204 } catch (SQLException e) {
205 LOG.error("PreparedStatement close error", e);
207 if (results != null) {
210 } catch (SQLException x) {
211 LOG.error("ResultSet close error", x);
215 if (dbConn != null && !dbConn.isClosed()) {
218 } catch (Exception exc) {
219 LOG.error("dbConn close error", exc);
229 public void store(SvcLogicGraph graph) throws SvcLogicException {
233 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph, md5sum)"
234 + " VALUES(?, ?, ?, ?, ?, ?, ?)";
237 throw new SvcLogicException("graph cannot be null");
240 byte[] graphBytes = null;
242 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
243 ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
245 goutStr.writeObject(graph);
247 graphBytes = byteStr.toByteArray();
249 } catch (Exception e) {
250 throw new SvcLogicException("could not serialize graph", e);
253 // If object already stored in database, delete it
254 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
256 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
260 Connection dbConn = null;
261 PreparedStatement storeGraphStmt = null;
263 dbConn = dbSvc.getConnection();
264 boolean oldAutoCommit = dbConn.getAutoCommit();
265 dbConn.setAutoCommit(false);
266 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
267 storeGraphStmt.setString(1, graph.getModule());
268 storeGraphStmt.setString(2, graph.getRpc());
269 storeGraphStmt.setString(3, graph.getVersion());
270 storeGraphStmt.setString(4, graph.getMode());
271 storeGraphStmt.setString(5, "N");
272 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
273 storeGraphStmt.setString(7, graph.getMd5sum());
274 storeGraphStmt.executeUpdate();
276 dbConn.setAutoCommit(oldAutoCommit);
277 } catch (Exception e) {
278 throw new SvcLogicException("Could not write object to database", e);
281 if (storeGraphStmt != null) {
282 storeGraphStmt.close();
284 } catch (SQLException e) {
285 LOG.error("PreparedStatement close error", e);
288 if (dbConn != null && !dbConn.isClosed()) {
291 } catch (Exception exc) {
292 LOG.error("dbConn close error", exc);
300 public void delete(String module, String rpc, String version, String mode)
301 throws SvcLogicException {
302 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
304 ArrayList<String> args = new ArrayList<>();
312 dbSvc.writeData(deleteGraphSql, args, null);
313 } catch (Exception e) {
314 throw new SvcLogicException("Could not delete object from database", e);
318 public void activate(SvcLogicGraph graph) throws SvcLogicException {
319 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
320 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
322 ArrayList<String> args = new ArrayList<>();
324 args.add(graph.getModule());
325 args.add(graph.getRpc());
326 args.add(graph.getMode());
329 dbSvc.writeData(deactivateSql, args, null);
330 args.add(graph.getVersion());
331 dbSvc.writeData(activateSql, args, null);
332 } catch (Exception e) {
333 throw new SvcLogicException("Could not activate graph", e);
337 private DbLibService getDbLibService() {
343 // Get DbLibService interface object.
344 ServiceReference sref = null;
345 BundleContext bctx = null;
347 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
349 if (bundle != null) {
350 bctx = bundle.getBundleContext();
353 sref = bctx.getServiceReference(DBLIB_SERVICE);
357 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
359 dbSvc = (DbLibService) bctx.getService(sref);
362 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
367 // initialize a stand-alone instance of dblib resource
369 // Try to create a DbLibService object from dblib properties
370 if(JavaSingleton.getInstance() == null){
371 Properties dblibProps = new Properties();
373 String propDir = System.getenv(SDNC_CONFIG_DIR);
374 if (propDir == null) {
376 propDir = "/opt/sdnc/data/properties";
378 String propPath = propDir + "/dblib.properties";
380 File propFile = new File(propPath);
382 if (!propFile.exists()) {
384 LOG.warn("Missing configuration properties file : {}", propFile);
390 dblibProps.load(new FileInputStream(propFile));
391 } catch (Exception e) {
393 "Could not load properties file " + propPath, e);
399 dbSvc = new DBResourceManager(dblibProps);
400 JavaSingleton.setInstance(dbSvc);
401 } catch (Exception e) {
402 LOG.warn("Caught exception trying to create DBResourceManager", e);
405 dbSvc = JavaSingleton.getInstance();
412 static class JavaSingleton {
413 /* Private constructor */
414 private JavaSingleton() {
415 /* the body of the constructor here */
418 /* instance of the singleton declaration */
419 private static volatile DbLibService INSTANCE ;
421 /* Access point to the unique instance of the singleton */
422 public static DbLibService getInstance() {
426 public static void setInstance(DbLibService dbresource) {
427 INSTANCE = dbresource;
433 public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
435 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
437 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
439 ArrayList<String> args = new ArrayList<>();
447 dbSvc.writeData(deactivateSql, args, null);
450 dbSvc.writeData(activateSql, args, null);
452 } catch (Exception e) {
453 throw new SvcLogicException("Could not activate graph", e);