Prepared statements for DG services
[appc.git] / appc-config / appc-flow-controller / provider / src / main / java / org / onap / appc / flow / controller / dbervices / DbLibServiceQueries.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.flow.controller.dbervices;
23
24 import java.sql.SQLException;
25 import java.util.ArrayList;
26
27 import javax.sql.rowset.CachedRowSet;
28
29 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
30 import org.onap.ccsdk.sli.core.dblib.DbLibService;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
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
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41
42 public class DbLibServiceQueries {
43     
44     private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DbLibService";
45     private static final EELFLogger log = EELFManager.getInstance().getLogger(DbLibServiceQueries.class);
46     
47     DbLibService dbLibService;
48     
49     public DbLibServiceQueries() {
50         this.dbLibService = getDbLibService();
51         if(this.dbLibService == null) {
52             throw new NullPointerException("DbLibService reference not found");
53         }
54     }
55     
56     public DbLibServiceQueries(DbLibService dbLibService) {
57         this.dbLibService = dbLibService;
58         if(this.dbLibService == null) {
59             throw new NullPointerException("Provided DbLibService is null");
60         }
61     }
62     
63     public DbLibServiceQueries(DbLibService dbLibService, boolean allowNull) {
64         this.dbLibService = dbLibService;
65         if(this.dbLibService == null && !allowNull) {
66             throw new NullPointerException("Provided DbLibService is null");
67         }
68     }
69     
70     public QueryStatus query(String query, SvcLogicContext ctx) {
71         ArrayList<String> arguments = new ArrayList<>();
72         query = CtxParameterizedResolver.resolveCtxVars(query, ctx, arguments);
73         return performQuery(query, ctx, null, arguments);
74     }
75     
76     public QueryStatus query(String query, String prefix, SvcLogicContext ctx) {
77         ArrayList<String> arguments = new ArrayList<>();
78         query = CtxParameterizedResolver.resolveCtxVars(query, ctx, arguments);
79         return performQuery(query, ctx, prefix, arguments);
80     }
81     
82     public QueryStatus query(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
83         return performQuery(query, ctx, null, valueOfArrayList(arguments));
84     }
85     public QueryStatus query(String query, SvcLogicContext ctx, String prefix, ArrayList<String> arguments) {
86         return performQuery(query, ctx, prefix, valueOfArrayList(arguments));
87     }
88     
89     private QueryStatus performQuery(String query, SvcLogicContext ctx, String prefix, ArrayList<String> arguments) {
90         
91         CachedRowSet result = null;
92         try {
93             result = dbLibService.getData(query, arguments, null);
94             if (!result.next()) {
95                 log.debug("No data found");
96                 return QueryStatus.NOT_FOUND;
97             } else {
98                 CtxParameterizedResolver.saveCachedRowSetToCtx(result, ctx, prefix, dbLibService);
99             }
100         } catch (SQLException e) {
101             log.error("Exception in query()",e);
102             return QueryStatus.FAILURE;
103         }
104         return QueryStatus.SUCCESS;
105         
106     }
107     public QueryStatus save(String query, SvcLogicContext ctx) {
108         ArrayList<String> arguments = new ArrayList<>();
109         query = CtxParameterizedResolver.resolveCtxVars(query, ctx, arguments);
110         return performSave(query, arguments);
111     }
112     
113     public QueryStatus save(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
114         return performSave(query, valueOfArrayList(arguments));
115     }
116     
117     private QueryStatus performSave(String query, ArrayList<String> arguments) {
118         boolean success = false;
119         try {
120             success = dbLibService.writeData(query, arguments, null);
121         } catch (SQLException e) {
122             log.error("Exception in save()",e);
123             success = false;
124         }
125         if(success) {
126             return QueryStatus.SUCCESS;
127         }
128         return QueryStatus.FAILURE;
129     }
130     
131     private static DbLibService getDbLibService() {
132         
133         DbLibService dbLibService = null;
134         BundleContext bundleContext = null;
135         ServiceReference serviceRef = null;
136
137         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
138
139         if (bundle != null) {
140             bundleContext = bundle.getBundleContext();
141         }
142
143         if (bundleContext != null) {
144             log.debug("Getting bundle Context");
145             serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
146         }
147
148         if (serviceRef == null) {
149             log.warn("Could not find service reference for DBLib service");
150                     
151         } else {
152             dbLibService = (DbLibService)bundleContext.getService(serviceRef);
153             if (dbLibService == null) {
154                 log.warn("DBLIB_SERVICE is null");
155             }
156         }
157         if (dbLibService == null) {
158             try {
159                 dbLibService = new DBResourceManager(System.getProperties());
160             } catch (Exception e) {
161                 log.error("Caught exception trying to create db service", e);
162             }
163
164             if (dbLibService == null) {
165                 log.warn("Could not create new DBResourceManager");
166             }
167         }
168         return dbLibService;
169     }
170     
171     //By using String.valueOf on the array list items, we can store any null values as
172     //Strings with the value "null". This mirrors the way queries worked prior to the
173     //prepared statements.
174     private ArrayList<String> valueOfArrayList(ArrayList<String> original) {
175         ArrayList<String> valueOfList = new ArrayList<>();
176         for(String s : original) {
177             valueOfList.add(String.valueOf(s));
178         }
179         return valueOfList;
180     }
181
182 }