2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.design.dbervices;
27 import static com.google.common.collect.Lists.newArrayList;
29 import java.sql.ResultSet;
30 import java.util.List;
31 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
32 import org.onap.ccsdk.sli.core.dblib.DbLibService;
33 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
34 import org.osgi.framework.Bundle;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceReference;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class DbService {
43 private static final Logger Log = LoggerFactory.getLogger(DbService.class);
44 private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
45 private DbLibService dbLibSvc = null;
47 public DbService() throws DBException {
48 Log.info("Initializing DbService service");
51 dbLibSvc = getDbLibService();
52 if (dbLibSvc == null) {
53 Log.error("Got Exception While getting DB Connection");
54 throw new DBException("Got Exception While getting DB Connection");
58 Log.error(e.getMessage());
59 throw new DBException("An error occurred when instantiating DB service", e);
63 private static DbLibService getDbLibService() {
65 DbLibService dbLibService = null;
66 BundleContext bundleContext = null;
67 ServiceReference serviceRef = null;
69 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
72 bundleContext = bundle.getBundleContext();
75 if (bundleContext != null) {
76 Log.debug("Getting bundle Context");
77 serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
80 if (serviceRef == null) {
81 Log.warn("Could not find service reference for DBLib service");
84 dbLibService = (DbLibService)bundleContext.getService(serviceRef);
85 if (dbLibService == null) {
86 Log.warn("DBLIB_SERVICE is null");
89 if (dbLibService == null) {
91 dbLibService = new DBResourceManager(System.getProperties());
92 } catch (Exception e) {
93 Log.error("Caught exception trying to create db service", e);
96 if (dbLibService == null) {
97 Log.warn("Could not create new DBResourceManager");
103 public ResultSet getDBData(String query) throws DBException{
105 StringBuilder sqlBuilder = new StringBuilder(query);
106 Log.info("Query: " + sqlBuilder.toString());
108 resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
109 } catch (Exception e) {
110 Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
111 throw new DBException("An error occurred when reading DB data", e);
116 public ResultSet getDBData(String query, List<String> paramList) throws DBException {
118 StringBuilder sqlBuilder = new StringBuilder(query);
119 Log.info("Query :" + sqlBuilder.toString());
121 resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
122 } catch (Exception e) {
123 Log.error("query "+sqlBuilder+" :: " + e.getMessage());
124 throw new DBException("An error occurred when reading DB data", e);
129 public boolean updateDBData(String query, List<String> paramList) throws DBException{
131 StringBuilder sqlBuilder = new StringBuilder(query);
132 Log.info("Query :" + sqlBuilder.toString());
134 update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
135 } catch (Exception e) {
136 Log.error("query "+sqlBuilder+" :: " + e.getMessage());
137 throw new DBException("An error occurred when updating DB data", e);