Fix sonar issues for APPC
[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 java.sql.ResultSet;
28 import java.util.ArrayList;
29
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;
39
40 public class DbService {
41
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;
46
47     public DbService() throws Exception {
48         DbLibService dblibSvc = null;
49         Log.info("Initializing DbService service");
50         try
51         {
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");
56             }
57             this.dblibSvc = dblibSvc;
58         }
59         catch (Exception e) {
60             Log.error(e.getMessage());
61             throw e;
62         }
63     }
64
65     private static DbLibService getDbLibService() {
66         
67         DbLibService dblibSvc = null;
68         BundleContext bctx = null;
69         ServiceReference sref = null;
70
71         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
72
73         if (bundle != null) {
74             bctx = bundle.getBundleContext();
75         }
76
77         if (bctx != null) {
78             Log.debug("Getting bundle Context");
79             sref = bctx.getServiceReference(DBLIB_SERVICE);
80         }
81
82         if (sref == null) {
83             Log.warn("Could not find service reference for DBLib service");
84                     
85         } else {
86             dblibSvc = (DbLibService) bctx.getService(sref);
87             if (dblibSvc == null) {
88                 Log.warn("DBLIB_SERVICE is null");
89             }
90         }
91         if (dblibSvc == null) {
92             try {
93                 dblibSvc = new DBResourceManager(System.getProperties());
94             } catch (Exception e) {
95                 Log.error("Caught exception trying to create db service", e);
96             }
97
98             if (dblibSvc == null) {
99                 Log.warn("Could not create new DBResourceManager");
100             }
101         }
102         return (dblibSvc);
103     }
104
105     public ResultSet getDBData(String query) throws Exception {
106         ResultSet resultSet;
107         StringBuilder sqlBuilder = new StringBuilder(query);
108         Log.info("Query: " + sqlBuilder.toString());
109         try {
110             resultSet = dblibSvc.getData(sqlBuilder.toString(), null, null);
111         } catch (Exception e) {
112             Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
113             throw e;
114         }
115         return resultSet;
116     }
117
118     public ResultSet getDBData(String query, ArrayList<String> paramList) throws Exception {
119         ResultSet resultSet;
120         StringBuilder sqlBuilder = new StringBuilder(query);
121         Log.info("Query :" + sqlBuilder.toString());
122         try {
123             resultSet = dblibSvc.getData(sqlBuilder.toString(), paramList, null);
124         } catch (Exception expObj) {
125             Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
126             throw expObj;
127         }
128         return resultSet;
129     }
130     
131     public boolean updateDBData(String query, ArrayList<String> paramList) throws Exception {
132         boolean update;
133         StringBuilder sqlBuilder = new StringBuilder(query);
134         Log.info("Query :" + sqlBuilder.toString());
135         try {
136             update = dblibSvc.writeData(sqlBuilder.toString(), paramList, null);
137         } catch (Exception expObj) {
138             Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
139             throw expObj;
140         }
141         return update;
142     }
143 }