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;
38 import javax.sql.rowset.CachedRowSet;
40 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
41 import org.onap.ccsdk.sli.core.dblib.DbLibService;
42 import org.osgi.framework.Bundle;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.FrameworkUtil;
45 import org.osgi.framework.ServiceReference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class SvcLogicDblibStore implements SvcLogicStore {
51 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
53 private static final Logger LOG = LoggerFactory.getLogger(SvcLogicDblibStore.class);
55 private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DbLibService";
57 private DbLibService dbSvc;
59 public SvcLogicDblibStore()
61 // Does nothing, but needed so that argumentless constructor
65 public SvcLogicDblibStore(DbLibService dbsvc) {
70 public void init(Properties props) throws ConfigurationException {
72 dbSvc = getDbLibService();
74 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
78 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
79 LOG.debug("SQL test was successful");
80 } catch (SQLException e) {
81 LOG.error("Failed SQL test", e);
86 public boolean hasGraph(String module, String rpc, String version,
87 String mode) throws SvcLogicException {
91 boolean retval = false;
92 CachedRowSet results = null;
93 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
94 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
96 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
97 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
99 ArrayList<String> args = new ArrayList<>();
106 if (version == null) {
107 results = dbSvc.getData(hasActiveGraphSql, args, null);
110 results = dbSvc.getData(hasVersionGraphSql, args, null);
113 if (results.next()) {
114 int cnt = results.getInt(1);
120 } catch (Exception e) {
121 throw new ConfigurationException("SQL query failed", e);
123 if (results != null) {
126 } catch (SQLException x) {
127 LOG.error("Failed to close CachedRowSet", x);
136 public SvcLogicGraph fetch(String module, String rpc, String version,
137 String mode) throws SvcLogicException {
139 PreparedStatement fetchGraphStmt = null;
140 Connection dbConn = null;
141 SvcLogicGraph retval = null;
142 ResultSet results = null;
144 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
145 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
147 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
148 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
152 dbConn = dbSvc.getConnection();
154 if (version == null) {
155 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
157 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
160 fetchGraphStmt.setString(1, module);
161 fetchGraphStmt.setString(2, rpc);
162 fetchGraphStmt.setString(3, mode);
163 if (version != null) {
164 fetchGraphStmt.setString(4,version);
167 results = fetchGraphStmt.executeQuery();
169 if (results.next()) {
170 Blob graphBlob = results.getBlob("graph");
172 ObjectInputStream gStream = new ObjectInputStream(graphBlob.getBinaryStream());
174 Object graphObj = gStream.readObject();
177 if (graphObj instanceof SvcLogicGraph) {
178 retval = (SvcLogicGraph) graphObj;
180 throw new ConfigurationException("invalid type for graph ("
181 + graphObj.getClass().getName());
188 } catch (SQLException e) {
189 throw new ConfigurationException("SQL query failed", e);
190 } catch (Exception e) {
191 throw new ConfigurationException("Graph processing failed", e);
194 if (fetchGraphStmt != null) {
195 fetchGraphStmt.close();
197 } catch (SQLException e) {
198 LOG.error("PreparedStatement close error", e);
200 if (results != null) {
203 } catch (SQLException x) {
204 LOG.error("ResultSet close error", x);
208 if (dbConn != null && !dbConn.isClosed()) {
211 } catch (Exception exc) {
212 LOG.error("dbConn close error", exc);
222 public void store(SvcLogicGraph graph) throws SvcLogicException {
226 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph)"
227 + " VALUES(?, ?, ?, ?, ?, ?)";
230 throw new SvcLogicException("graph cannot be null");
233 byte[] graphBytes = null;
235 try (ByteArrayOutputStream byteStr = new ByteArrayOutputStream();
236 ObjectOutputStream goutStr = new ObjectOutputStream(byteStr)) {
238 goutStr.writeObject(graph);
240 graphBytes = byteStr.toByteArray();
242 } catch (Exception e) {
243 throw new SvcLogicException("could not serialize graph", e);
246 // If object already stored in database, delete it
247 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
249 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
253 Connection dbConn = null;
254 PreparedStatement storeGraphStmt = null;
256 dbConn = dbSvc.getConnection();
257 boolean oldAutoCommit = dbConn.getAutoCommit();
258 dbConn.setAutoCommit(false);
259 storeGraphStmt = dbConn.prepareStatement(storeGraphSql);
260 storeGraphStmt.setString(1, graph.getModule());
261 storeGraphStmt.setString(2, graph.getRpc());
262 storeGraphStmt.setString(3, graph.getVersion());
263 storeGraphStmt.setString(4, graph.getMode());
264 storeGraphStmt.setString(5, "N");
265 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
267 storeGraphStmt.executeUpdate();
270 dbConn.setAutoCommit(oldAutoCommit);
271 } catch (Exception e) {
272 throw new SvcLogicException("Could not write object to database", e);
275 if (storeGraphStmt != null) {
276 storeGraphStmt.close();
278 } catch (SQLException e) {
279 LOG.error("PreparedStatement close error", e);
282 if (dbConn != null && !dbConn.isClosed()) {
285 } catch (Exception exc) {
286 LOG.error("dbConn close error", exc);
294 public void delete(String module, String rpc, String version, String mode)
295 throws SvcLogicException {
299 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
301 ArrayList<String> args = new ArrayList<>();
309 dbSvc.writeData(deleteGraphSql, args, null);
310 } catch (Exception e) {
311 throw new SvcLogicException("Could not delete object from database", e);
315 public void activate(SvcLogicGraph graph) throws SvcLogicException {
318 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());
330 dbSvc.writeData(deactivateSql, args, null);
332 args.add(graph.getVersion());
333 dbSvc.writeData(activateSql, args, null);
335 } catch (Exception e) {
336 throw new SvcLogicException("Could not activate graph", e);
340 private DbLibService getDbLibService() {
346 // Get DbLibService interface object.
347 ServiceReference sref = null;
348 BundleContext bctx = null;
350 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
352 if (bundle != null) {
353 bctx = bundle.getBundleContext();
356 sref = bctx.getServiceReference(DBLIB_SERVICE);
360 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
362 dbSvc = (DbLibService) bctx.getService(sref);
365 LOG.warn("Could not find service reference for DBLIB service ({})", DBLIB_SERVICE);
370 // initialize a stand-alone instance of dblib resource
372 // Try to create a DbLibService object from dblib properties
373 if(JavaSingleton.getInstance() == null){
374 Properties dblibProps = new Properties();
376 String propDir = System.getenv(SDNC_CONFIG_DIR);
377 if (propDir == null) {
379 propDir = "/opt/sdnc/data/properties";
381 String propPath = propDir + "/dblib.properties";
383 File propFile = new File(propPath);
385 if (!propFile.exists()) {
387 LOG.warn("Missing configuration properties file : {}", propFile);
393 dblibProps.load(new FileInputStream(propFile));
394 } catch (Exception e) {
396 "Could not load properties file " + propPath, e);
402 dbSvc = new DBResourceManager(dblibProps);
403 JavaSingleton.setInstance(dbSvc);
404 } catch (Exception e) {
405 LOG.warn("Caught exception trying to create DBResourceManager", e);
408 dbSvc = JavaSingleton.getInstance();
415 static class JavaSingleton {
416 /* Private constructor */
417 private JavaSingleton() {
418 /* the body of the constructor here */
421 /* instance of the singleton declaration */
422 private static volatile DbLibService INSTANCE ;
424 /* Access point to the unique instance of the singleton */
425 public static DbLibService getInstance() {
429 public static void setInstance(DbLibService dbresource) {
430 INSTANCE = dbresource;
435 public void activate(String module, String rpc, String version, String mode) throws SvcLogicException {
438 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
440 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
442 ArrayList<String> args = new ArrayList<String>();
450 dbSvc.writeData(deactivateSql, args, null);
453 dbSvc.writeData(activateSql, args, null);
455 } catch (Exception e) {
456 throw new SvcLogicException("Could not activate graph", e);