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 (SQLException e) {
197 throw new ConfigurationException("SQL query failed", e);
198 } catch (Exception e) {
199 throw new ConfigurationException("Graph processing failed", e);
202 if (fetchGraphStmt != null) {
203 fetchGraphStmt.close();
205 } catch (SQLException e) {
206 LOG.error("PreparedStatement close error", e);
208 if (results != null) {
211 } catch (SQLException x) {
212 LOG.error("ResultSet close error", x);
216 if (dbConn != null && !dbConn.isClosed()) {
219 } catch (Exception exc) {
220 LOG.error("dbConn close error", exc);
230 public void store(SvcLogicGraph graph) throws SvcLogicException {
234 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph, md5sum)"
235 + " VALUES(?, ?, ?, ?, ?, ?, ?)";
238 throw new SvcLogicException("graph cannot be null");
241 byte[] graphBytes = null;
243 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
244 ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
246 goutStr.writeObject(graph);
248 graphBytes = byteStr.toByteArray();
250 } catch (Exception e) {
251 throw new SvcLogicException("could not serialize graph", e);
254 // If object already stored in database, delete it
255 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
257 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
261 Connection dbConn = null;
262 PreparedStatement storeGraphStmt = null;
264 dbConn = dbSvc.getConnection();
265 boolean oldAutoCommit = dbConn.getAutoCommit();
266 dbConn.setAutoCommit(false);
267 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
268 storeGraphStmt.setString(1, graph.getModule());
269 storeGraphStmt.setString(2, graph.getRpc());
270 storeGraphStmt.setString(3, graph.getVersion());
271 storeGraphStmt.setString(4, graph.getMode());
272 storeGraphStmt.setString(5, "N");
273 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
274 storeGraphStmt.setString(7, graph.getMd5sum());
275 storeGraphStmt.executeUpdate();
277 dbConn.setAutoCommit(oldAutoCommit);
278 } catch (Exception e) {
279 throw new SvcLogicException("Could not write object to database", e);
282 if (storeGraphStmt != null) {
283 storeGraphStmt.close();
285 } catch (SQLException e) {
286 LOG.error("PreparedStatement close error", e);
289 if (dbConn != null && !dbConn.isClosed()) {
292 } catch (Exception exc) {
293 LOG.error("dbConn close error", exc);
301 public void delete(String module, String rpc, String version, String mode)
302 throws SvcLogicException {
303 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
305 ArrayList<String> args = new ArrayList<>();
313 dbSvc.writeData(deleteGraphSql, args, null);
314 } catch (Exception e) {
315 throw new SvcLogicException("Could not delete object from database", e);
319 public void activate(SvcLogicGraph graph) throws SvcLogicException {
320 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
321 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
323 ArrayList<String> args = new ArrayList<>();
325 args.add(graph.getModule());
326 args.add(graph.getRpc());
327 args.add(graph.getMode());
330 dbSvc.writeData(deactivateSql, args, null);
331 args.add(graph.getVersion());
332 dbSvc.writeData(activateSql, args, null);
333 } catch (Exception e) {
334 throw new SvcLogicException("Could not activate graph", e);
338 private DbLibService getDbLibService() {
344 // Get DbLibService interface object.
345 ServiceReference sref = null;
346 BundleContext bctx = null;
348 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
350 if (bundle != null) {
351 bctx = bundle.getBundleContext();
354 sref = bctx.getServiceReference(DBLIB_SERVICE);
358 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
360 dbSvc = (DbLibService) bctx.getService(sref);
363 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
368 // initialize a stand-alone instance of dblib resource
370 // Try to create a DbLibService object from dblib properties
371 if(JavaSingleton.getInstance() == null){
372 Properties dblibProps = new Properties();
374 String propDir = System.getenv(SDNC_CONFIG_DIR);
375 if (propDir == null) {
377 propDir = "/opt/sdnc/data/properties";
379 String propPath = propDir + "/dblib.properties";
381 File propFile = new File(propPath);
383 if (!propFile.exists()) {
385 LOG.warn("Missing configuration properties file : {}", propFile);
391 dblibProps.load(new FileInputStream(propFile));
392 } catch (Exception e) {
394 "Could not load properties file " + propPath, e);
400 dbSvc = new DBResourceManager(dblibProps);
401 JavaSingleton.setInstance(dbSvc);
402 } catch (Exception e) {
403 LOG.warn("Caught exception trying to create DBResourceManager", e);
406 dbSvc = JavaSingleton.getInstance();
413 static class JavaSingleton {
414 /* Private constructor */
415 private JavaSingleton() {
416 /* the body of the constructor here */
419 /* instance of the singleton declaration */
420 private static volatile DbLibService INSTANCE ;
422 /* Access point to the unique instance of the singleton */
423 public static DbLibService getInstance() {
427 public static void setInstance(DbLibService dbresource) {
428 INSTANCE = dbresource;
434 public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
436 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
438 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
440 ArrayList<String> args = new ArrayList<>();
448 dbSvc.writeData(deactivateSql, args, null);
451 dbSvc.writeData(activateSql, args, null);
453 } catch (Exception e) {
454 throw new SvcLogicException("Could not activate graph", e);