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;
26 import java.io.FileInputStream;
27 import java.io.IOException;
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
54 .getLogger(SvcLogicDblibStore.class);
56 private static final String DBLIB_SERVICE =
57 "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
59 Properties props = null;
61 public void init(Properties props) throws ConfigurationException {
63 DbLibService dbSvc = getDbLibService();
65 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
69 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
70 LOG.debug("SQL test was successful");
71 } catch (SQLException e) {
72 LOG.error("Failed SQL test", e);
76 public boolean hasGraph(String module, String rpc, String version,
77 String mode) throws SvcLogicException {
79 DbLibService dbSvc = getDbLibService();
81 boolean retval = false;
82 CachedRowSet results = null;
83 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
84 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
86 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
87 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
89 PreparedStatement hasGraphStmt = null;
91 ArrayList<String> args = new ArrayList<String>();
98 if (version == null) {
99 results = dbSvc.getData(hasActiveGraphSql, args, null);
102 results = dbSvc.getData(hasVersionGraphSql, args, null);
105 if (results.next()) {
106 int cnt = results.getInt(1);
113 } catch (Exception e) {
114 throw new ConfigurationException("SQL query failed", e);
116 if (results != null) {
120 } catch (SQLException x) {
130 public SvcLogicGraph fetch(String module, String rpc, String version,
131 String mode) throws SvcLogicException {
133 DbLibService dbSvc = getDbLibService();
135 Connection dbConn = null;
136 SvcLogicGraph retval = null;
137 ResultSet results = null;
139 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
140 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
142 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
143 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
147 dbConn = ((DBResourceManager) dbSvc).getConnection();
148 PreparedStatement fetchGraphStmt;
150 ArrayList<String> args = new ArrayList<String>();
155 if (version == null) {
156 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
158 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
161 fetchGraphStmt.setString(1, module);
162 fetchGraphStmt.setString(2, rpc);
163 fetchGraphStmt.setString(3, mode);
164 if (version != null) {
165 fetchGraphStmt.setString(4,version);
168 results = fetchGraphStmt.executeQuery();
170 if (results.next()) {
171 Blob graphBlob = results.getBlob("graph");
173 ObjectInputStream gStream = new ObjectInputStream(
174 graphBlob.getBinaryStream());
176 Object graphObj = gStream.readObject();
179 if (graphObj instanceof SvcLogicGraph) {
180 retval = (SvcLogicGraph) graphObj;
182 throw new ConfigurationException("invalid type for graph ("
183 + graphObj.getClass().getName());
190 } catch (SQLException e) {
191 throw new ConfigurationException("SQL query failed", e);
192 } catch (Exception e) {
193 throw new ConfigurationException("Graph processing failed", e);
195 if (results != null) {
198 } catch (SQLException x) {
202 if (dbConn != null && !dbConn.isClosed()) {
205 } catch (Throwable exc) {
206 // the exception not monitored
217 public void store(SvcLogicGraph graph) throws SvcLogicException {
219 DbLibService dbSvc = getDbLibService();
221 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph)"
222 + " VALUES(?, ?, ?, ?, ?, ?)";
225 throw new SvcLogicException("graph cannot be null");
228 byte[] graphBytes = null;
230 ByteArrayOutputStream byteStr = null;
231 ObjectOutputStream goutStr = null;
234 byteStr = new ByteArrayOutputStream();
235 goutStr = new ObjectOutputStream(byteStr);
236 goutStr.writeObject(graph);
238 graphBytes = byteStr.toByteArray();
240 } catch (Exception e) {
241 throw new SvcLogicException("could not serialize graph", e);
244 if (goutStr != null) {
247 } catch (IOException e) {
252 if (byteStr != null) {
255 } catch (IOException e) {
261 // If object already stored in database, delete it
262 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
264 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
268 Connection dbConn = null;
271 dbConn = ((DBResourceManager) dbSvc).getConnection();
272 boolean oldAutoCommit = dbConn.getAutoCommit();
273 dbConn.setAutoCommit(false);
274 PreparedStatement storeGraphStmt = dbConn
275 .prepareStatement(storeGraphSql);
276 storeGraphStmt.setString(1, graph.getModule());
277 storeGraphStmt.setString(2, graph.getRpc());
278 storeGraphStmt.setString(3, graph.getVersion());
279 storeGraphStmt.setString(4, graph.getMode());
280 storeGraphStmt.setString(5, "N");
281 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
283 storeGraphStmt.executeUpdate();
286 dbConn.setAutoCommit(oldAutoCommit);
287 } catch (Exception e) {
288 throw new SvcLogicException("Could not write object to database", e);
291 if (dbConn != null && !dbConn.isClosed()) {
294 } catch (Throwable exc) {
295 // the exception not monitored
303 public void delete(String module, String rpc, String version, String mode)
304 throws SvcLogicException {
306 DbLibService dbSvc = getDbLibService();
308 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
310 ArrayList<String> args = new ArrayList<String>();
318 dbSvc.writeData(deleteGraphSql, args, null);
319 } catch (Exception e) {
320 throw new SvcLogicException(
321 "Could not delete object from database", e);
325 public void activate(SvcLogicGraph graph) throws SvcLogicException {
326 DbLibService dbSvc = getDbLibService();
328 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
330 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
332 ArrayList<String> args = new ArrayList<String>();
334 args.add(graph.getModule());
335 args.add(graph.getRpc());
336 args.add(graph.getMode());
340 dbSvc.writeData(deactivateSql, args, null);
342 args.add(graph.getVersion());
343 dbSvc.writeData(activateSql, args, null);
345 } catch (Exception e) {
346 throw new SvcLogicException("Could not activate graph", e);
351 public void registerNodeType(String nodeType) throws SvcLogicException {
353 String registerNodeSql = "INSERT INTO NODE_TYPES (nodetype) VALUES(?)";
355 if (isValidNodeType(nodeType)) {
359 DbLibService dbSvc = getDbLibService();
360 ArrayList<String> args = new ArrayList<String>();
365 dbSvc.writeData(registerNodeSql, args, null);
366 } catch (Exception e) {
367 throw new SvcLogicException("Could not add node type to database",
374 public void unregisterNodeType(String nodeType) throws SvcLogicException {
376 if (!isValidNodeType(nodeType)) {
380 String unregisterNodeSql = "DELETE FROM NODE_TYPES WHERE nodetype = ?";
382 DbLibService dbSvc = getDbLibService();
383 ArrayList<String> args = new ArrayList<String>();
388 dbSvc.writeData(unregisterNodeSql, args, null);
389 } catch (Exception e) {
390 throw new SvcLogicException(
391 "Could not delete node type from database", e);
397 public boolean isValidNodeType(String nodeType) throws SvcLogicException {
399 String validateNodeSql = "SELECT count(*) FROM NODE_TYPES WHERE nodetype = ?";
401 DbLibService dbSvc = getDbLibService();
403 ArrayList<String> args = new ArrayList<String>();
407 boolean isValid = false;
409 CachedRowSet results = null;
411 results = dbSvc.getData(validateNodeSql, args, null);
412 if (results != null) {
413 if (results.next()) {
414 int cnt = results.getInt(1);
421 } catch (Exception e) {
422 throw new SvcLogicException(
423 "Cannot select node type from database", e);
425 if (results != null) {
428 } catch (SQLException x) {
437 private DbLibService getDbLibService() {
439 // Get DbLibService interface object.
440 DbLibService dblibSvc = null;
441 ServiceReference sref = null;
442 BundleContext bctx = null;
444 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
446 if (bundle != null) {
447 bctx = bundle.getBundleContext();
450 sref = bctx.getServiceReference(DBLIB_SERVICE);
454 LOG.warn("Could not find service reference for DBLIB service ("
455 + DBLIB_SERVICE + ")");
457 dblibSvc = (DbLibService) bctx.getService(sref);
458 if (dblibSvc == null) {
460 LOG.warn("Could not find service reference for DBLIB service ("
461 + DBLIB_SERVICE + ")");
466 // initialize a stand-alone instance of dblib resource
468 // Try to create a DbLibService object from dblib properties
469 if(JavaSingleton.getInstance() == null){
470 Properties dblibProps = new Properties();
472 String propDir = System.getenv(SDNC_CONFIG_DIR);
473 if (propDir == null) {
475 propDir = "/opt/sdnc/data/properties";
477 String propPath = propDir + "/dblib.properties";
479 File propFile = new File(propPath);
481 if (!propFile.exists()) {
484 "Missing configuration properties file : "
491 dblibProps.load(new FileInputStream(propFile));
492 } catch (Exception e) {
494 "Could not load properties file " + propPath, e);
500 dblibSvc = DBResourceManager.create(dblibProps);
501 JavaSingleton.setInstance(dblibSvc);
502 } catch (Exception e) {
503 LOG.warn("Caught exception trying to create DBResourceManager", e);
506 dblibSvc = JavaSingleton.getInstance();
513 static class JavaSingleton {
514 /* Private constructor */
515 private JavaSingleton() {
516 /* the body of the constructor here */
519 /* instance of the singleton declaration */
520 private static volatile DbLibService INSTANCE ;
522 /* Access point to the unique instance of the singleton */
523 public static DbLibService getInstance() {
527 public static void setInstance(DbLibService dbresource) {
528 INSTANCE = dbresource;