DbService sonar fixes
[appc.git] / appc-inbound / appc-design-services / provider / src / main / java / org / onap / appc / design / dbervices / DbService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.design.dbervices;
26
27 import static com.google.common.collect.Lists.newArrayList;
28
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;
40
41 public class DbService {
42
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;
46
47     public DbService() throws DBException {
48         Log.info("Initializing DbService service");
49         try
50         {
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");
55             }
56         }
57         catch (Exception e) {
58             Log.error(e.getMessage());
59             throw new DBException("An error occurred when instantiating DB service", e);
60         }
61     }
62
63     private static DbLibService getDbLibService() {
64         
65         DbLibService dbLibService = null;
66         BundleContext bundleContext = null;
67         ServiceReference serviceRef = null;
68
69         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
70
71         if (bundle != null) {
72             bundleContext = bundle.getBundleContext();
73         }
74
75         if (bundleContext != null) {
76             Log.debug("Getting bundle Context");
77             serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
78         }
79
80         if (serviceRef == null) {
81             Log.warn("Could not find service reference for DBLib service");
82                     
83         } else {
84             dbLibService = (DbLibService)bundleContext.getService(serviceRef);
85             if (dbLibService == null) {
86                 Log.warn("DBLIB_SERVICE is null");
87             }
88         }
89         if (dbLibService == null) {
90             try {
91                 dbLibService = new DBResourceManager(System.getProperties());
92             } catch (Exception e) {
93                 Log.error("Caught exception trying to create db service", e);
94             }
95
96             if (dbLibService == null) {
97                 Log.warn("Could not create new DBResourceManager");
98             }
99         }
100         return dbLibService;
101     }
102
103     public ResultSet getDBData(String query) throws DBException{
104         ResultSet resultSet;
105         StringBuilder sqlBuilder = new StringBuilder(query);
106         Log.info("Query: " + sqlBuilder.toString());
107         try {
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);
112         }
113         return resultSet;
114     }
115
116     public ResultSet getDBData(String query, List<String> paramList) throws DBException {
117         ResultSet resultSet;
118         StringBuilder sqlBuilder = new StringBuilder(query);
119         Log.info("Query :" + sqlBuilder.toString());
120         try {
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);
125         }
126         return resultSet;
127     }
128     
129     public boolean updateDBData(String query, List<String> paramList) throws DBException{
130         boolean update;
131         StringBuilder sqlBuilder = new StringBuilder(query);
132         Log.info("Query :" + sqlBuilder.toString());
133         try {
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);
138         }
139         return update;
140     }
141 }