ad4242ae0b0055093c624436e9ba0315839c5541
[appc.git] /
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.artifact.handler.dbservices;
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 query(query, ctx, arguments);
74     }
75     
76     public QueryStatus query(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
77         
78         CachedRowSet result = null;
79         try {
80             result = dbLibService.getData(query, arguments, null);
81             if (!result.next()) {
82                 log.debug("No data found");
83                 return QueryStatus.NOT_FOUND;
84             } else {
85                 CtxParameterizedResolver.saveCachedRowSetToCtx(result, ctx, null, dbLibService);
86             }
87         } catch (SQLException e) {
88             log.error("Exception in query()",e);
89             return QueryStatus.FAILURE;
90         }
91         return QueryStatus.SUCCESS;
92         
93     }
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);
98     }
99     
100     public QueryStatus save(String query, SvcLogicContext ctx, ArrayList<String> arguments) {
101         boolean success = false;
102         try {
103             success = dbLibService.writeData(query, arguments, null);
104         } catch (SQLException e) {
105             log.error("Exception in save()",e);
106             success = false;
107         }
108         if(success) {
109             return QueryStatus.SUCCESS;
110         }
111         return QueryStatus.FAILURE;
112     }
113     
114     private static DbLibService getDbLibService() {
115         
116         DbLibService dbLibService = null;
117         BundleContext bundleContext = null;
118         ServiceReference serviceRef = null;
119
120         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
121
122         if (bundle != null) {
123             bundleContext = bundle.getBundleContext();
124         }
125
126         if (bundleContext != null) {
127             log.debug("Getting bundle Context");
128             serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
129         }
130
131         if (serviceRef == null) {
132             log.warn("Could not find service reference for DBLib service");
133                     
134         } else {
135             dbLibService = (DbLibService)bundleContext.getService(serviceRef);
136             if (dbLibService == null) {
137                 log.warn("DBLIB_SERVICE is null");
138             }
139         }
140         if (dbLibService == null) {
141             try {
142                 dbLibService = new DBResourceManager(System.getProperties());
143             } catch (Exception e) {
144                 log.error("Caught exception trying to create db service", e);
145             }
146
147             if (dbLibService == null) {
148                 log.warn("Could not create new DBResourceManager");
149             }
150         }
151         return dbLibService;
152     }
153
154 }