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 java.sql.ResultSet;
28 import java.util.ArrayList;
30 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
31 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
32 import org.onap.ccsdk.sli.core.dblib.DbLibService;
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.DBResourceManager";
44 DbLibService dblibSvc = null;
45 String errorMsg = null;
47 public DbService() throws Exception {
48 DbLibService dblibSvc = null;
49 Log.info("Initializing DbService service");
52 dblibSvc = getDbLibService();
53 if (dblibSvc == null) {
54 Log.error("Got Exception While getting DB Connection");
55 throw new Exception("Got Exception While getting DB Connection");
57 this.dblibSvc = dblibSvc;
60 Log.error(e.getMessage());
65 private static DbLibService getDbLibService() {
67 DbLibService dblibSvc = null;
68 BundleContext bctx = null;
69 ServiceReference sref = null;
71 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
74 bctx = bundle.getBundleContext();
78 Log.debug("Getting bundle Context");
79 sref = bctx.getServiceReference(DBLIB_SERVICE);
83 Log.warn("Could not find service reference for DBLib service");
86 dblibSvc = (DbLibService) bctx.getService(sref);
87 if (dblibSvc == null) {
88 Log.warn("DBLIB_SERVICE is null");
91 if (dblibSvc == null) {
93 dblibSvc = new DBResourceManager(System.getProperties());
94 } catch (Exception e) {
95 Log.error("Caught exception trying to create db service", e);
98 if (dblibSvc == null) {
99 Log.warn("Could not create new DBResourceManager");
105 public ResultSet getDBData(String query) throws Exception {
107 StringBuilder sqlBuilder = new StringBuilder(query);
108 Log.info("Query: " + sqlBuilder.toString());
110 resultSet = dblibSvc.getData(sqlBuilder.toString(), null, null);
111 } catch (Exception e) {
112 Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
118 public ResultSet getDBData(String query, ArrayList<String> paramList) throws Exception {
120 StringBuilder sqlBuilder = new StringBuilder(query);
121 Log.info("Query :" + sqlBuilder.toString());
123 resultSet = dblibSvc.getData(sqlBuilder.toString(), paramList, null);
124 } catch (Exception expObj) {
125 Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
131 public boolean updateDBData(String query, ArrayList<String> paramList) throws Exception {
133 StringBuilder sqlBuilder = new StringBuilder(query);
134 Log.info("Query :" + sqlBuilder.toString());
136 update = dblibSvc.writeData(sqlBuilder.toString(), paramList, null);
137 } catch (Exception expObj) {
138 Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());