2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=========================================================
22 package org.onap.appc.flow.controller.dbervices;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
27 import javax.sql.rowset.CachedRowSet;
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;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
42 public class DbLibServiceQueries {
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);
47 DbLibService dbLibService;
49 public DbLibServiceQueries() {
50 this.dbLibService = getDbLibService();
51 if(this.dbLibService == null) {
52 throw new NullPointerException("DbLibService reference not found");
56 public DbLibServiceQueries(DbLibService dbLibService) {
57 this.dbLibService = dbLibService;
58 if(this.dbLibService == null) {
59 throw new NullPointerException("Provided DbLibService is null");
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");
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);
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);
82 public QueryStatus query(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
83 return performQuery(query, ctx, null, valueOfArrayList(arguments));
85 public QueryStatus query(String query, SvcLogicContext ctx, String prefix, ArrayList<String> arguments) {
86 return performQuery(query, ctx, prefix, valueOfArrayList(arguments));
89 private QueryStatus performQuery(String query, SvcLogicContext ctx, String prefix, ArrayList<String> arguments) {
91 CachedRowSet result = null;
93 result = dbLibService.getData(query, arguments, null);
95 log.debug("No data found");
96 return QueryStatus.NOT_FOUND;
98 CtxParameterizedResolver.saveCachedRowSetToCtx(result, ctx, prefix, dbLibService);
100 } catch (SQLException e) {
101 log.error("Exception in query()",e);
102 return QueryStatus.FAILURE;
104 return QueryStatus.SUCCESS;
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);
113 public QueryStatus save(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
114 return performSave(query, valueOfArrayList(arguments));
117 private QueryStatus performSave(String query, ArrayList<String> arguments) {
118 boolean success = false;
120 success = dbLibService.writeData(query, arguments, null);
121 } catch (SQLException e) {
122 log.error("Exception in save()",e);
126 return QueryStatus.SUCCESS;
128 return QueryStatus.FAILURE;
131 private static DbLibService getDbLibService() {
133 DbLibService dbLibService = null;
134 BundleContext bundleContext = null;
135 ServiceReference serviceRef = null;
137 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
139 if (bundle != null) {
140 bundleContext = bundle.getBundleContext();
143 if (bundleContext != null) {
144 log.debug("Getting bundle Context");
145 serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
148 if (serviceRef == null) {
149 log.warn("Could not find service reference for DBLib service");
152 dbLibService = (DbLibService)bundleContext.getService(serviceRef);
153 if (dbLibService == null) {
154 log.warn("DBLIB_SERVICE is null");
157 if (dbLibService == null) {
159 dbLibService = new DBResourceManager(System.getProperties());
160 } catch (Exception e) {
161 log.error("Caught exception trying to create db service", e);
164 if (dbLibService == null) {
165 log.warn("Could not create new DBResourceManager");
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));