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 (Exception e) {
193 throw new ConfigurationException("SQL query 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;