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.IOException;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
32 import java.sql.Connection;
33 import java.sql.PreparedStatement;
34 import java.sql.ResultSet;
35 import java.sql.SQLException;
36 import java.util.ArrayList;
37 import java.util.Properties;
39 import javax.sql.rowset.CachedRowSet;
41 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
42 import org.onap.ccsdk.sli.core.dblib.DbLibService;
43 import org.osgi.framework.Bundle;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.FrameworkUtil;
46 import org.osgi.framework.ServiceReference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class SvcLogicDblibStore implements SvcLogicStore {
52 private static final String SDNC_CONFIG_DIR = "SDNC_CONFIG_DIR";
54 private static final Logger LOG = LoggerFactory
55 .getLogger(SvcLogicDblibStore.class);
57 private static final String DBLIB_SERVICE =
58 "org.onap.ccsdk.sli.core.dblib.DbLibService";
60 Properties props = null;
62 public void init(Properties props) throws ConfigurationException {
64 DbLibService dbSvc = getDbLibService();
66 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
70 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
71 LOG.debug("SQL test was successful");
72 } catch (SQLException e) {
73 LOG.error("Failed SQL test", e);
77 public boolean hasGraph(String module, String rpc, String version,
78 String mode) throws SvcLogicException {
80 DbLibService dbSvc = getDbLibService();
82 boolean retval = false;
83 CachedRowSet results = null;
84 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
85 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
87 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
88 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
90 PreparedStatement hasGraphStmt = null;
92 ArrayList<String> args = new ArrayList<String>();
99 if (version == null) {
100 results = dbSvc.getData(hasActiveGraphSql, args, null);
103 results = dbSvc.getData(hasVersionGraphSql, args, null);
106 if (results.next()) {
107 int cnt = results.getInt(1);
114 } catch (Exception e) {
115 throw new ConfigurationException("SQL query failed", e);
117 if (results != null) {
121 } catch (SQLException x) {
131 public SvcLogicGraph fetch(String module, String rpc, String version,
132 String mode) throws SvcLogicException {
134 DbLibService dbSvc = getDbLibService();
136 Connection dbConn = null;
137 SvcLogicGraph retval = null;
138 ResultSet results = null;
140 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
141 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
143 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
144 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
148 dbConn = ((DBResourceManager) dbSvc).getConnection();
149 PreparedStatement fetchGraphStmt;
151 ArrayList<String> args = new ArrayList<String>();
156 if (version == null) {
157 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
159 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
162 fetchGraphStmt.setString(1, module);
163 fetchGraphStmt.setString(2, rpc);
164 fetchGraphStmt.setString(3, mode);
165 if (version != null) {
166 fetchGraphStmt.setString(4,version);
169 results = fetchGraphStmt.executeQuery();
171 if (results.next()) {
172 Blob graphBlob = results.getBlob("graph");
174 ObjectInputStream gStream = new ObjectInputStream(
175 graphBlob.getBinaryStream());
177 Object graphObj = gStream.readObject();
180 if (graphObj instanceof SvcLogicGraph) {
181 retval = (SvcLogicGraph) graphObj;
183 throw new ConfigurationException("invalid type for graph ("
184 + graphObj.getClass().getName());
191 } catch (SQLException e) {
192 throw new ConfigurationException("SQL query failed", e);
193 } catch (Exception e) {
194 throw new ConfigurationException("Graph processing failed", e);
196 if (results != null) {
199 } catch (SQLException x) {
203 if (dbConn != null && !dbConn.isClosed()) {
206 } catch (Throwable exc) {
207 // the exception not monitored
218 public void store(SvcLogicGraph graph) throws SvcLogicException {
220 DbLibService dbSvc = getDbLibService();
222 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph)"
223 + " VALUES(?, ?, ?, ?, ?, ?)";
226 throw new SvcLogicException("graph cannot be null");
229 byte[] graphBytes = null;
231 ByteArrayOutputStream byteStr = null;
232 ObjectOutputStream goutStr = null;
235 byteStr = new ByteArrayOutputStream();
236 goutStr = new ObjectOutputStream(byteStr);
237 goutStr.writeObject(graph);
239 graphBytes = byteStr.toByteArray();
241 } catch (Exception e) {
242 throw new SvcLogicException("could not serialize graph", e);
245 if (goutStr != null) {
248 } catch (IOException e) {
253 if (byteStr != null) {
256 } catch (IOException e) {
262 // If object already stored in database, delete it
263 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
265 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
269 Connection dbConn = null;
272 dbConn = ((DBResourceManager) dbSvc).getConnection();
273 boolean oldAutoCommit = dbConn.getAutoCommit();
274 dbConn.setAutoCommit(false);
275 PreparedStatement storeGraphStmt = dbConn
276 .prepareStatement(storeGraphSql);
277 storeGraphStmt.setString(1, graph.getModule());
278 storeGraphStmt.setString(2, graph.getRpc());
279 storeGraphStmt.setString(3, graph.getVersion());
280 storeGraphStmt.setString(4, graph.getMode());
281 storeGraphStmt.setString(5, "N");
282 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
284 storeGraphStmt.executeUpdate();
287 dbConn.setAutoCommit(oldAutoCommit);
288 } catch (Exception e) {
289 throw new SvcLogicException("Could not write object to database", e);
292 if (dbConn != null && !dbConn.isClosed()) {
295 } catch (Throwable exc) {
296 // the exception not monitored
304 public void delete(String module, String rpc, String version, String mode)
305 throws SvcLogicException {
307 DbLibService dbSvc = getDbLibService();
309 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
311 ArrayList<String> args = new ArrayList<String>();
319 dbSvc.writeData(deleteGraphSql, args, null);
320 } catch (Exception e) {
321 throw new SvcLogicException(
322 "Could not delete object from database", e);
326 public void activate(SvcLogicGraph graph) throws SvcLogicException {
327 DbLibService dbSvc = getDbLibService();
329 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
331 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
333 ArrayList<String> args = new ArrayList<String>();
335 args.add(graph.getModule());
336 args.add(graph.getRpc());
337 args.add(graph.getMode());
341 dbSvc.writeData(deactivateSql, args, null);
343 args.add(graph.getVersion());
344 dbSvc.writeData(activateSql, args, null);
346 } catch (Exception e) {
347 throw new SvcLogicException("Could not activate graph", e);
352 public void registerNodeType(String nodeType) throws SvcLogicException {
354 String registerNodeSql = "INSERT INTO NODE_TYPES (nodetype) VALUES(?)";
356 if (isValidNodeType(nodeType)) {
360 DbLibService dbSvc = getDbLibService();
361 ArrayList<String> args = new ArrayList<String>();
366 dbSvc.writeData(registerNodeSql, args, null);
367 } catch (Exception e) {
368 throw new SvcLogicException("Could not add node type to database",
375 public void unregisterNodeType(String nodeType) throws SvcLogicException {
377 if (!isValidNodeType(nodeType)) {
381 String unregisterNodeSql = "DELETE FROM NODE_TYPES WHERE nodetype = ?";
383 DbLibService dbSvc = getDbLibService();
384 ArrayList<String> args = new ArrayList<String>();
389 dbSvc.writeData(unregisterNodeSql, args, null);
390 } catch (Exception e) {
391 throw new SvcLogicException(
392 "Could not delete node type from database", e);
398 public boolean isValidNodeType(String nodeType) throws SvcLogicException {
400 String validateNodeSql = "SELECT count(*) FROM NODE_TYPES WHERE nodetype = ?";
402 DbLibService dbSvc = getDbLibService();
404 ArrayList<String> args = new ArrayList<String>();
408 boolean isValid = false;
410 CachedRowSet results = null;
412 results = dbSvc.getData(validateNodeSql, args, null);
413 if (results != null) {
414 if (results.next()) {
415 int cnt = results.getInt(1);
422 } catch (Exception e) {
423 throw new SvcLogicException(
424 "Cannot select node type from database", e);
426 if (results != null) {
429 } catch (SQLException x) {
438 private DbLibService getDbLibService() {
440 // Get DbLibService interface object.
441 DbLibService dblibSvc = null;
442 ServiceReference sref = null;
443 BundleContext bctx = null;
445 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
447 if (bundle != null) {
448 bctx = bundle.getBundleContext();
451 sref = bctx.getServiceReference(DBLIB_SERVICE);
455 LOG.warn("Could not find service reference for DBLIB service ("
456 + DBLIB_SERVICE + ")");
458 dblibSvc = (DbLibService) bctx.getService(sref);
459 if (dblibSvc == null) {
461 LOG.warn("Could not find service reference for DBLIB service ("
462 + DBLIB_SERVICE + ")");
467 // initialize a stand-alone instance of dblib resource
469 // Try to create a DbLibService object from dblib properties
470 if(JavaSingleton.getInstance() == null){
471 Properties dblibProps = new Properties();
473 String propDir = System.getenv(SDNC_CONFIG_DIR);
474 if (propDir == null) {
476 propDir = "/opt/sdnc/data/properties";
478 String propPath = propDir + "/dblib.properties";
480 File propFile = new File(propPath);
482 if (!propFile.exists()) {
485 "Missing configuration properties file : "
492 dblibProps.load(new FileInputStream(propFile));
493 } catch (Exception e) {
495 "Could not load properties file " + propPath, e);
501 dblibSvc = new DBResourceManager(dblibProps);
502 JavaSingleton.setInstance(dblibSvc);
503 } catch (Exception e) {
504 LOG.warn("Caught exception trying to create DBResourceManager", e);
507 dblibSvc = JavaSingleton.getInstance();
514 static class JavaSingleton {
515 /* Private constructor */
516 private JavaSingleton() {
517 /* the body of the constructor here */
520 /* instance of the singleton declaration */
521 private static volatile DbLibService INSTANCE ;
523 /* Access point to the unique instance of the singleton */
524 public static DbLibService getInstance() {
528 public static void setInstance(DbLibService dbresource) {
529 INSTANCE = dbresource;