aboutsummaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-license-manager/appc-license-manager-core/src/main/java/org/openecomp/appc/licmgr/impl/LicenseDataAccessServiceImpl.java
blob: 2aff1ffb960caf297b796b28dd95449f29fb7c24 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.appc.licmgr.impl;

import org.openecomp.appc.licmgr.Constants;
import org.openecomp.appc.licmgr.LicenseDataAccessService;
import org.openecomp.appc.licmgr.exception.DataAccessException;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;

import javax.sql.rowset.CachedRowSet;

import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


@SuppressWarnings("JavaDoc")
class LicenseDataAccessServiceImpl implements LicenseDataAccessService {

    private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);

    public void setSchema(String schema) {
        this.schema = schema;
    }

    private String schema;

    private DbLibService dbLibService;

    private void checkDbLibService() throws DataAccessException {
        if (null != dbLibService) {return;}

        //get dblib service and send it to DAService
        BundleContext bctx = FrameworkUtil.getBundle(LicenseManagerImpl.class).getBundleContext();
        ServiceReference sref = bctx.getServiceReference(DbLibService.class.getName());
        dbLibService  = (DbLibService)bctx.getService(sref);

    }

    /**
     * empty constructor
     */
    public LicenseDataAccessServiceImpl(){}

    @Override
    public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
                    DataAccessException {

        checkDbLibService();

        Map<String,String> result = new HashMap<>();
        if (null == fields || 0 == fields.length) fields = new String[]{ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};

        String queryString = buildQueryStatement();

        ArrayList<String> argList = new ArrayList<>();
        argList.add(vnfType);
        argList.add(vnfVersion);
        argList.add(Constants.VF_LICENSE);

        try {

            final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);

            if (data.first()) {
                for (String field : fields) {
                    result.put(field, data.getString(field));
                }
            } else {
                String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.ASDC_ARTIFACTS_TABLE_NAME;
                logger.info(msg);
            }
        } catch (SQLException e) {
            logger.error("Error Accessing Database " + e);
            throw new DataAccessException(e);
        }

        return result;
    }

    private String buildQueryStatement() {
        return "select * " + "from " + Constants.ASDC_ARTIFACTS_TABLE_NAME + " " +
            "where " + ASDC_ARTIFACTS_FIELDS.RESOURCE_NAME.name() + " = ?" +
             " AND " + ASDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.name() + " = ?" +
             " AND " + ASDC_ARTIFACTS_FIELDS.ARTIFACT_TYPE.name() + " = ?";
    }

    /**
     * Implementation of storeArtifactPayload()
     * @see LicenseDataAccessService
     */
    @Override
    public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {

        checkDbLibService();

        if(parameters == null || parameters.isEmpty()) {
            throw new RuntimeException("No parameters for insert are provided");
        }

        String insertStr = "INSERT INTO " + Constants.ASDC_ARTIFACTS_TABLE_NAME + "(";
        String valuesStr = "VALUES(";
        String insertStatementStr;

        ArrayList<String> params = new ArrayList<>();
        boolean firstTime = true;
        for(Map.Entry<String, String> entry : parameters.entrySet()) {
            if(!firstTime) {
                insertStr += ",";
                valuesStr += ",";
            }
            else {
                firstTime = false;
            }
            insertStr += entry.getKey();
            valuesStr += "?";

            params.add(entry.getValue());
        }

        insertStr += ")";
        valuesStr += ")";
        insertStatementStr = insertStr + " " + valuesStr;

        executeStoreArtifactPayload(insertStatementStr, params);
    }

    /**
     * Exexutes insert statement for artifact payload
     * @param insertStatementStr
     * @param params
     * @throws RuntimeException
     */
    private void executeStoreArtifactPayload(String insertStatementStr, ArrayList<String> params) throws RuntimeException {

        try {
            logger.info("used schema=" + this.schema);
            logger.info("insert statement=" + insertStatementStr);

            dbLibService.writeData(insertStatementStr, params, this.schema);

            logger.info("finished to execute insert");

        } catch (SQLException e) {
            logger.error("Storing Artifact payload failed - " + insertStatementStr);
            throw new RuntimeException("Storing Artifact payload failed - " + insertStatementStr);
        }
    }

}