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.openecomp.appc.design.dbervices;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.util.ArrayList;
30 import java.util.Properties;
31 import java.util.UUID;
33 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
34 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
35 import org.onap.ccsdk.sli.core.dblib.DbLibService;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class DbService {
45 private static final Logger Log = LoggerFactory.getLogger(DbService.class);
46 private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
47 DbLibService dblibSvc = null;
48 String errorMsg = null;
50 public DbService() throws Exception {
51 DbLibService dblibSvc = null;
52 Log.info("Initializing DbService service");
55 dblibSvc = getDbLibService();
56 if (dblibSvc == null) {
57 Log.error("Got Exception While getting DB Connection");
58 throw new Exception("Got Exception While getting DB Connection");
60 this.dblibSvc = dblibSvc;
63 Log.error(e.getMessage());
68 private static DbLibService getDbLibService() {
70 DbLibService dblibSvc = null;
71 BundleContext bctx = null;
72 ServiceReference sref = null;
74 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
77 bctx = bundle.getBundleContext();
81 Log.debug("Getting bundle Context");
82 sref = bctx.getServiceReference(DBLIB_SERVICE);
86 Log.warn("Could not find service reference for DBLib service");
89 dblibSvc = (DbLibService) bctx.getService(sref);
90 if (dblibSvc == null) {
91 Log.warn("DBLIB_SERVICE is null");
94 if (dblibSvc == null) {
96 dblibSvc = new DBResourceManager(System.getProperties());
97 } catch (Exception e) {
98 Log.error("Caught exception trying to create db service", e);
101 if (dblibSvc == null) {
102 Log.warn("Could not create new DBResourceManager");
108 public ResultSet getDBData(String query) throws Exception {
110 StringBuilder sqlBuilder = new StringBuilder(query);
111 Log.info("Query: " + sqlBuilder.toString());
113 resultSet = dblibSvc.getData(sqlBuilder.toString(), null, null);
114 } catch (Exception e) {
115 Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
121 public ResultSet getDBData(String query, ArrayList<String> paramList) throws Exception {
123 StringBuilder sqlBuilder = new StringBuilder(query);
124 Log.info("Query :" + sqlBuilder.toString());
126 resultSet = dblibSvc.getData(sqlBuilder.toString(), paramList, null);
127 } catch (Exception expObj) {
128 Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
134 public boolean updateDBData(String query, ArrayList<String> paramList) throws Exception {
136 StringBuilder sqlBuilder = new StringBuilder(query);
137 Log.info("Query :" + sqlBuilder.toString());
139 update = dblibSvc.writeData(sqlBuilder.toString(), paramList, null);
140 } catch (Exception expObj) {
141 Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());