aboutsummaryrefslogtreecommitdiffstats
path: root/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DbService.java
blob: b09ef0d8c3eabc90ae9ca45541da7b6f9a12da38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.design.dbervices;

import static com.google.common.collect.Lists.newArrayList;

import java.sql.ResultSet;
import java.util.List;
import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
import org.onap.ccsdk.sli.core.dblib.DbLibService;
import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DbService {

    private static final Logger Log = LoggerFactory.getLogger(DbService.class);
    private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
    private DbLibService dbLibSvc = null;

    public DbService() throws DBException {
        Log.info("Initializing DbService service");
        try
        {
            dbLibSvc = getDbLibService();
            if (dbLibSvc == null) {
                Log.error("Got Exception While getting DB Connection");
                throw new DBException("Got Exception While getting DB Connection");
            }
        }
        catch (Exception e) {
            Log.error(e.getMessage());
            throw new DBException("An error occurred when instantiating DB service", e);
        }
    }

    private static DbLibService getDbLibService() {
        
        DbLibService dbLibService = null;
        BundleContext bundleContext = null;
        ServiceReference serviceRef = null;

        Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);

        if (bundle != null) {
            bundleContext = bundle.getBundleContext();
        }

        if (bundleContext != null) {
            Log.debug("Getting bundle Context");
            serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
        }

        if (serviceRef == null) {
            Log.warn("Could not find service reference for DBLib service");
                    
        } else {
            dbLibService = (DbLibService)bundleContext.getService(serviceRef);
            if (dbLibService == null) {
                Log.warn("DBLIB_SERVICE is null");
            }
        }
        if (dbLibService == null) {
            try {
                dbLibService = new DBResourceManager(System.getProperties());
            } catch (Exception e) {
                Log.error("Caught exception trying to create db service", e);
            }

            if (dbLibService == null) {
                Log.warn("Could not create new DBResourceManager");
            }
        }
        return dbLibService;
    }

    public ResultSet getDBData(String query) throws DBException{
        ResultSet resultSet;
        StringBuilder sqlBuilder = new StringBuilder(query);
        Log.info("Query: " + sqlBuilder.toString());
        try {
            resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
        } catch (Exception e) {
            Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
            throw new DBException("An error occurred when reading DB data", e);
        }
        return resultSet;
    }

    public ResultSet getDBData(String query, List<String> paramList) throws DBException {
        ResultSet resultSet;
        StringBuilder sqlBuilder = new StringBuilder(query);
        Log.info("Query :" + sqlBuilder.toString());
        try {
            resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
        } catch (Exception e) {
            Log.error("query "+sqlBuilder+" :: " + e.getMessage());
            throw new DBException("An error occurred when reading DB data", e);
        }
        return resultSet;
    }
    
    public boolean updateDBData(String query, List<String> paramList) throws DBException{
        boolean update;
        StringBuilder sqlBuilder = new StringBuilder(query);
        Log.info("Query :" + sqlBuilder.toString());
        try {
            update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
        } catch (Exception e) {
            Log.error("query "+sqlBuilder+" :: " + e.getMessage());
            throw new DBException("An error occurred when updating DB data", e);
        }
        return update;
    }
}