Change Design Service to use DbLibService
[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-2018 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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.design.dbervices;
25
26 import static com.google.common.collect.Lists.newArrayList;
27
28 import java.sql.ResultSet;
29 import java.util.List;
30 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
31 import org.onap.ccsdk.sli.core.dblib.DbLibService;
32 import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
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.DbLibService";
44     private DbLibService dbLibSvc = null;
45
46     public DbService() throws DBException {
47         Log.info("Initializing DbService service");
48         try
49         {
50             dbLibSvc = getDbLibService();
51             if (dbLibSvc == null) {
52                 Log.error("Got Exception While getting DB Connection");
53                 throw new DBException("Got Exception While getting DB Connection");
54             }
55         }
56         catch (Exception e) {
57             Log.error(e.getMessage());
58             throw new DBException("An error occurred when instantiating DB service", e);
59         }
60     }
61
62     private static DbLibService getDbLibService() {
63         
64         DbLibService dbLibService = null;
65         BundleContext bundleContext = null;
66         ServiceReference serviceRef = null;
67
68         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
69
70         if (bundle != null) {
71             bundleContext = bundle.getBundleContext();
72         }
73
74         if (bundleContext != null) {
75             Log.debug("Getting bundle Context");
76             serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
77         }
78
79         if (serviceRef == null) {
80             Log.warn("Could not find service reference for DBLib service");
81                     
82         } else {
83             dbLibService = (DbLibService)bundleContext.getService(serviceRef);
84             if (dbLibService == null) {
85                 Log.warn("DBLIB_SERVICE is null");
86             }
87         }
88         if (dbLibService == null) {
89             try {
90                 dbLibService = new DBResourceManager(System.getProperties());
91             } catch (Exception e) {
92                 Log.error("Caught exception trying to create db service", e);
93             }
94
95             if (dbLibService == null) {
96                 Log.warn("Could not create new DBResourceManager");
97             }
98         }
99         return dbLibService;
100     }
101
102     public ResultSet getDBData(String query) throws DBException{
103         ResultSet resultSet;
104         StringBuilder sqlBuilder = new StringBuilder(query);
105         Log.info("Query: " + sqlBuilder.toString());
106         try {
107             resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
108         } catch (Exception e) {
109             Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
110             throw new DBException("An error occurred when reading DB data", e);
111         }
112         return resultSet;
113     }
114
115     public ResultSet getDBData(String query, List<String> paramList) throws DBException {
116         ResultSet resultSet;
117         StringBuilder sqlBuilder = new StringBuilder(query);
118         Log.info("Query :" + sqlBuilder.toString());
119         try {
120             resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
121         } catch (Exception e) {
122             Log.error("query "+sqlBuilder+" :: " + e.getMessage());
123             throw new DBException("An error occurred when reading DB data", e);
124         }
125         return resultSet;
126     }
127     
128     public boolean updateDBData(String query, List<String> paramList) throws DBException{
129         boolean update;
130         StringBuilder sqlBuilder = new StringBuilder(query);
131         Log.info("Query :" + sqlBuilder.toString());
132         try {
133             update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
134         } catch (Exception e) {
135             Log.error("query "+sqlBuilder+" :: " + e.getMessage());
136             throw new DBException("An error occurred when updating DB data", e);
137         }
138         return update;
139     }
140 }