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