2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.design.dbervices;
26 import static com.google.common.collect.Lists.newArrayList;
28 import java.sql.ResultSet;
29 import java.util.List;
30 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
31 import org.onap.ccsdk.sli.core.dblib.DbLibService;
32 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class DbService {
42 private static final Logger Log = LoggerFactory.getLogger(DbService.class);
43 private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DbLibService";
44 private DbLibService dbLibSvc = null;
46 public DbService() throws DBException {
47 Log.info("Initializing DbService service");
50 dbLibSvc = getDbLibService();
51 if (dbLibSvc == null) {
52 Log.error("Got Exception While getting DB Connection");
53 throw new DBException("Got Exception While getting DB Connection");
57 Log.error(e.getMessage());
58 throw new DBException("An error occurred when instantiating DB service", e);
62 private static DbLibService getDbLibService() {
64 DbLibService dbLibService = null;
65 BundleContext bundleContext = null;
66 ServiceReference serviceRef = null;
68 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
71 bundleContext = bundle.getBundleContext();
74 if (bundleContext != null) {
75 Log.debug("Getting bundle Context");
76 serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
79 if (serviceRef == null) {
80 Log.warn("Could not find service reference for DBLib service");
83 dbLibService = (DbLibService)bundleContext.getService(serviceRef);
84 if (dbLibService == null) {
85 Log.warn("DBLIB_SERVICE is null");
88 if (dbLibService == null) {
90 dbLibService = new DBResourceManager(System.getProperties());
91 } catch (Exception e) {
92 Log.error("Caught exception trying to create db service", e);
95 if (dbLibService == null) {
96 Log.warn("Could not create new DBResourceManager");
102 public ResultSet getDBData(String query) throws DBException{
104 StringBuilder sqlBuilder = new StringBuilder(query);
105 Log.info("Query: " + sqlBuilder.toString());
107 resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
108 } catch (Exception e) {
109 Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
110 throw new DBException("An error occurred when reading DB data", e);
115 public ResultSet getDBData(String query, List<String> paramList) throws DBException {
117 StringBuilder sqlBuilder = new StringBuilder(query);
118 Log.info("Query :" + sqlBuilder.toString());
120 resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
121 } catch (Exception e) {
122 Log.error("query "+sqlBuilder+" :: " + e.getMessage());
123 throw new DBException("An error occurred when reading DB data", e);
128 public boolean updateDBData(String query, List<String> paramList) throws DBException{
130 StringBuilder sqlBuilder = new StringBuilder(query);
131 Log.info("Query :" + sqlBuilder.toString());
133 update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
134 } catch (Exception e) {
135 Log.error("query "+sqlBuilder+" :: " + e.getMessage());
136 throw new DBException("An error occurred when updating DB data", e);