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.artifact.handler.dbservices;
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 query(query, ctx, arguments);
76 public QueryStatus query(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
78 CachedRowSet result = null;
80 result = dbLibService.getData(query, arguments, null);
82 log.debug("No data found");
83 return QueryStatus.NOT_FOUND;
85 CtxParameterizedResolver.saveCachedRowSetToCtx(result, ctx, null, dbLibService);
87 } catch (SQLException e) {
88 log.error("Exception in query()",e);
89 return QueryStatus.FAILURE;
91 return QueryStatus.SUCCESS;
94 public QueryStatus save(String query, SvcLogicContext ctx) {
95 ArrayList<String> arguments = new ArrayList<>();
96 query = CtxParameterizedResolver.resolveCtxVars(query, ctx, arguments);
97 return save(query,ctx,arguments);
100 public QueryStatus save(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
101 boolean success = false;
103 success = dbLibService.writeData(query, arguments, null);
104 } catch (SQLException e) {
105 log.error("Exception in save()",e);
109 return QueryStatus.SUCCESS;
111 return QueryStatus.FAILURE;
114 private static DbLibService getDbLibService() {
116 DbLibService dbLibService = null;
117 BundleContext bundleContext = null;
118 ServiceReference serviceRef = null;
120 Bundle bundle = FrameworkUtil.getBundle(SvcLogicService.class);
122 if (bundle != null) {
123 bundleContext = bundle.getBundleContext();
126 if (bundleContext != null) {
127 log.debug("Getting bundle Context");
128 serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
131 if (serviceRef == null) {
132 log.warn("Could not find service reference for DBLib service");
135 dbLibService = (DbLibService)bundleContext.getService(serviceRef);
136 if (dbLibService == null) {
137 log.warn("DBLIB_SERVICE is null");
140 if (dbLibService == null) {
142 dbLibService = new DBResourceManager(System.getProperties());
143 } catch (Exception e) {
144 log.error("Caught exception trying to create db service", e);
147 if (dbLibService == null) {
148 log.warn("Could not create new DBResourceManager");