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.openecomp.sdnc.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.openecomp.sdnc.sli.resource.dblib.DBResourceManager;
42 import org.openecomp.sdnc.sli.resource.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.openecomp.sdnc.sli.resource.dblib.DBLibService";
59 "org.openecomp.sdnc.sli.resource.dblib.DBResourceManager";
61 Properties props = null;
63 public void init(Properties props) throws ConfigurationException {
65 DbLibService dbSvc = getDbLibService();
67 LOG.error("SvcLogic cannot acquire DBLIB_SERVICE");
71 dbSvc.getData("select 1 from DUAL", new ArrayList<String>(), null);
72 LOG.debug("SQL test was successful");
73 } catch (SQLException e) {
74 LOG.error("Failed SQL test", e);
78 public boolean hasGraph(String module, String rpc, String version,
79 String mode) throws SvcLogicException {
81 DbLibService dbSvc = getDbLibService();
83 boolean retval = false;
84 CachedRowSet results = null;
85 String hasVersionGraphSql = "SELECT count(*) FROM SVC_LOGIC"
86 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
88 String hasActiveGraphSql = "SELECT count(*) FROM SVC_LOGIC"
89 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
91 PreparedStatement hasGraphStmt = null;
93 ArrayList<String> args = new ArrayList<String>();
100 if (version == null) {
101 results = dbSvc.getData(hasActiveGraphSql, args, null);
104 results = dbSvc.getData(hasVersionGraphSql, args, null);
107 if (results.next()) {
108 int cnt = results.getInt(1);
115 } catch (Exception e) {
116 throw new ConfigurationException("SQL query failed", e);
118 if (results != null) {
122 } catch (SQLException x) {
132 public SvcLogicGraph fetch(String module, String rpc, String version,
133 String mode) throws SvcLogicException {
135 DbLibService dbSvc = getDbLibService();
137 Connection dbConn = null;
138 SvcLogicGraph retval = null;
139 ResultSet results = null;
141 String fetchVersionGraphSql = "SELECT graph FROM SVC_LOGIC"
142 + " WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
144 String fetchActiveGraphSql = "SELECT graph FROM SVC_LOGIC"
145 + " WHERE module = ? AND rpc = ? AND mode = ? AND active = 'Y'";
149 dbConn = ((DBResourceManager) dbSvc).getConnection();
150 PreparedStatement fetchGraphStmt;
152 ArrayList<String> args = new ArrayList<String>();
157 if (version == null) {
158 fetchGraphStmt = dbConn.prepareStatement(fetchActiveGraphSql);
160 fetchGraphStmt = dbConn.prepareStatement(fetchVersionGraphSql);
163 fetchGraphStmt.setString(1, module);
164 fetchGraphStmt.setString(2, rpc);
165 fetchGraphStmt.setString(3, mode);
166 if (version != null) {
167 fetchGraphStmt.setString(4,version);
170 results = fetchGraphStmt.executeQuery();
172 if (results.next()) {
173 Blob graphBlob = results.getBlob("graph");
175 ObjectInputStream gStream = new ObjectInputStream(
176 graphBlob.getBinaryStream());
178 Object graphObj = gStream.readObject();
181 if (graphObj instanceof SvcLogicGraph) {
182 retval = (SvcLogicGraph) graphObj;
184 throw new ConfigurationException("invalid type for graph ("
185 + graphObj.getClass().getName());
192 } catch (SQLException e) {
193 throw new ConfigurationException("SQL query failed", e);
194 } catch (Exception e) {
195 throw new ConfigurationException("Graph processing failed", e);
197 if (results != null) {
200 } catch (SQLException x) {
204 if (dbConn != null && !dbConn.isClosed()) {
207 } catch (Throwable exc) {
208 // the exception not monitored
219 public void store(SvcLogicGraph graph) throws SvcLogicException {
221 DbLibService dbSvc = getDbLibService();
223 String storeGraphSql = "INSERT INTO SVC_LOGIC (module, rpc, version, mode, active, graph)"
224 + " VALUES(?, ?, ?, ?, ?, ?)";
227 throw new SvcLogicException("graph cannot be null");
230 byte[] graphBytes = null;
232 ByteArrayOutputStream byteStr = null;
233 ObjectOutputStream goutStr = null;
236 byteStr = new ByteArrayOutputStream();
237 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 (goutStr != null) {
249 } catch (IOException e) {
254 if (byteStr != null) {
257 } catch (IOException e) {
263 // If object already stored in database, delete it
264 if (hasGraph(graph.getModule(), graph.getRpc(), graph.getVersion(),
266 delete(graph.getModule(), graph.getRpc(), graph.getVersion(),
270 Connection dbConn = null;
273 dbConn = ((DBResourceManager) dbSvc).getConnection();
274 boolean oldAutoCommit = dbConn.getAutoCommit();
275 dbConn.setAutoCommit(false);
276 PreparedStatement storeGraphStmt = dbConn
277 .prepareStatement(storeGraphSql);
278 storeGraphStmt.setString(1, graph.getModule());
279 storeGraphStmt.setString(2, graph.getRpc());
280 storeGraphStmt.setString(3, graph.getVersion());
281 storeGraphStmt.setString(4, graph.getMode());
282 storeGraphStmt.setString(5, "N");
283 storeGraphStmt.setBlob(6, new ByteArrayInputStream(graphBytes));
285 storeGraphStmt.executeUpdate();
288 dbConn.setAutoCommit(oldAutoCommit);
289 } catch (Exception e) {
290 throw new SvcLogicException("Could not write object to database", e);
293 if (dbConn != null && !dbConn.isClosed()) {
296 } catch (Throwable exc) {
297 // the exception not monitored
305 public void delete(String module, String rpc, String version, String mode)
306 throws SvcLogicException {
308 DbLibService dbSvc = getDbLibService();
310 String deleteGraphSql = "DELETE FROM SVC_LOGIC WHERE module = ? AND rpc = ? AND version = ? AND mode = ?";
312 ArrayList<String> args = new ArrayList<String>();
320 dbSvc.writeData(deleteGraphSql, args, null);
321 } catch (Exception e) {
322 throw new SvcLogicException(
323 "Could not delete object from database", e);
327 public void activate(SvcLogicGraph graph) throws SvcLogicException {
328 DbLibService dbSvc = getDbLibService();
330 String deactivateSql = "UPDATE SVC_LOGIC SET active = 'N' WHERE module = ? AND rpc = ? AND mode = ?";
332 String activateSql = "UPDATE SVC_LOGIC SET active = 'Y' WHERE module = ? AND rpc = ? AND mode = ? AND version = ?";
334 ArrayList<String> args = new ArrayList<String>();
336 args.add(graph.getModule());
337 args.add(graph.getRpc());
338 args.add(graph.getMode());
342 dbSvc.writeData(deactivateSql, args, null);
344 args.add(graph.getVersion());
345 dbSvc.writeData(activateSql, args, null);
347 } catch (Exception e) {
348 throw new SvcLogicException("Could not activate graph", e);
353 public void registerNodeType(String nodeType) throws SvcLogicException {
355 String registerNodeSql = "INSERT INTO NODE_TYPES (nodetype) VALUES(?)";
357 if (isValidNodeType(nodeType)) {
361 DbLibService dbSvc = getDbLibService();
362 ArrayList<String> args = new ArrayList<String>();
367 dbSvc.writeData(registerNodeSql, args, null);
368 } catch (Exception e) {
369 throw new SvcLogicException("Could not add node type to database",
376 public void unregisterNodeType(String nodeType) throws SvcLogicException {
378 if (!isValidNodeType(nodeType)) {
382 String unregisterNodeSql = "DELETE FROM NODE_TYPES WHERE nodetype = ?";
384 DbLibService dbSvc = getDbLibService();
385 ArrayList<String> args = new ArrayList<String>();
390 dbSvc.writeData(unregisterNodeSql, args, null);
391 } catch (Exception e) {
392 throw new SvcLogicException(
393 "Could not delete node type from database", e);
399 public boolean isValidNodeType(String nodeType) throws SvcLogicException {
401 String validateNodeSql = "SELECT count(*) FROM NODE_TYPES WHERE nodetype = ?";
403 DbLibService dbSvc = getDbLibService();
405 ArrayList<String> args = new ArrayList<String>();
409 boolean isValid = false;
411 CachedRowSet results = null;
413 results = dbSvc.getData(validateNodeSql, args, null);
414 if (results != null) {
415 if (results.next()) {
416 int cnt = results.getInt(1);
423 } catch (Exception e) {
424 throw new SvcLogicException(
425 "Cannot select node type from database", e);
427 if (results != null) {
430 } catch (SQLException x) {
439 private DbLibService getDbLibService() {
441 // Get DbLibService interface object.
442 DbLibService dblibSvc = null;
443 ServiceReference sref = null;
444 BundleContext bctx = null;
446 Bundle bundle = FrameworkUtil.getBundle(SvcLogicDblibStore.class);
448 if (bundle != null) {
449 bctx = bundle.getBundleContext();
452 sref = bctx.getServiceReference(DBLIB_SERVICE);
456 LOG.warn("Could not find service reference for DBLIB service ("
457 + DBLIB_SERVICE + ")");
459 dblibSvc = (DbLibService) bctx.getService(sref);
460 if (dblibSvc == null) {
462 LOG.warn("Could not find service reference for DBLIB service ("
463 + DBLIB_SERVICE + ")");
468 // initialize a stand-alone instance of dblib resource
470 // Try to create a DbLibService object from dblib properties
471 if(JavaSingleton.getInstance() == null){
472 Properties dblibProps = new Properties();
474 String propDir = System.getenv(SDNC_CONFIG_DIR);
475 if (propDir == null) {
477 propDir = "/opt/sdnc/data/properties";
479 String propPath = propDir + "/dblib.properties";
481 File propFile = new File(propPath);
483 if (!propFile.exists()) {
486 "Missing configuration properties file : "
493 dblibProps.load(new FileInputStream(propFile));
494 } catch (Exception e) {
496 "Could not load properties file " + propPath, e);
502 dblibSvc = DBResourceManager.create(dblibProps);
503 JavaSingleton.setInstance(dblibSvc);
504 } catch (Exception e) {
505 LOG.warn("Caught exception trying to create DBResourceManager", e);
508 dblibSvc = JavaSingleton.getInstance();
515 static class JavaSingleton {
516 /* Private constructor */
517 private JavaSingleton() {
518 /* the body of the constructor here */
521 /* instance of the singleton declaration */
522 private static volatile DbLibService INSTANCE ;
524 /* Access point to the unique instance of the singleton */
525 public static DbLibService getInstance() {
529 public static void setInstance(DbLibService dbresource) {
530 INSTANCE = dbresource;