Change nexus values to properties
[appc.git] / appc-dispatcher / appc-license-manager / appc-license-manager-core / src / main / java / org / openecomp / appc / licmgr / impl / LicenseDataAccessServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.licmgr.impl;
23
24 import org.openecomp.appc.licmgr.Constants;
25 import org.openecomp.appc.licmgr.LicenseDataAccessService;
26 import org.openecomp.appc.licmgr.exception.DataAccessException;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.FrameworkUtil;
32 import org.osgi.framework.ServiceReference;
33
34 import javax.sql.rowset.CachedRowSet;
35
36 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS;
37
38 import java.sql.SQLException;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Map;
42
43
44 @SuppressWarnings("JavaDoc")
45 class LicenseDataAccessServiceImpl implements LicenseDataAccessService {
46
47     private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);
48
49     public void setSchema(String schema) {
50         this.schema = schema;
51     }
52
53     private String schema;
54
55     private DbLibService dbLibService;
56
57     private void checkDbLibService() throws DataAccessException {
58         if (null != dbLibService) {return;}
59
60         //get dblib service and send it to DAService
61         BundleContext bctx = FrameworkUtil.getBundle(LicenseManagerImpl.class).getBundleContext();
62         ServiceReference sref = bctx.getServiceReference(DbLibService.class.getName());
63         dbLibService  = (DbLibService)bctx.getService(sref);
64
65     }
66
67     /**
68      * empty constructor
69      */
70     public LicenseDataAccessServiceImpl(){}
71
72     @Override
73     public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
74                     DataAccessException {
75
76         checkDbLibService();
77
78         Map<String,String> result = new HashMap<>();
79         if (null == fields || 0 == fields.length) fields = new String[]{ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};
80
81         String queryString = buildQueryStatement();
82
83         ArrayList<String> argList = new ArrayList<>();
84         argList.add(vnfType);
85         argList.add(vnfVersion);
86         argList.add(Constants.VF_LICENSE);
87
88         try {
89
90             final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);
91
92             if (data.first()) {
93                 for (String field : fields) {
94                     result.put(field, data.getString(field));
95                 }
96             } else {
97                 String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.ASDC_ARTIFACTS_TABLE_NAME;
98                 logger.info(msg);
99             }
100         } catch (SQLException e) {
101             logger.error("Error Accessing Database " + e);
102             throw new DataAccessException(e);
103         }
104
105         return result;
106     }
107
108     private String buildQueryStatement() {
109         return "select * " + "from " + Constants.ASDC_ARTIFACTS_TABLE_NAME + " " +
110             "where " + ASDC_ARTIFACTS_FIELDS.RESOURCE_NAME.name() + " = ?" +
111              " AND " + ASDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.name() + " = ?" +
112              " AND " + ASDC_ARTIFACTS_FIELDS.ARTIFACT_TYPE.name() + " = ?";
113     }
114
115     /**
116      * Implementation of storeArtifactPayload()
117      * @see LicenseDataAccessService
118      */
119     @Override
120     public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {
121
122         checkDbLibService();
123
124         if(parameters == null || parameters.isEmpty()) {
125             throw new RuntimeException("No parameters for insert are provided");
126         }
127
128         String insertStr = "INSERT INTO " + Constants.ASDC_ARTIFACTS_TABLE_NAME + "(";
129         String valuesStr = "VALUES(";
130         String insertStatementStr;
131
132         ArrayList<String> params = new ArrayList<>();
133         boolean firstTime = true;
134         for(Map.Entry<String, String> entry : parameters.entrySet()) {
135             if(!firstTime) {
136                 insertStr += ",";
137                 valuesStr += ",";
138             }
139             else {
140                 firstTime = false;
141             }
142             insertStr += entry.getKey();
143             valuesStr += "?";
144
145             params.add(entry.getValue());
146         }
147
148         insertStr += ")";
149         valuesStr += ")";
150         insertStatementStr = insertStr + " " + valuesStr;
151
152         executeStoreArtifactPayload(insertStatementStr, params);
153     }
154
155     /**
156      * Exexutes insert statement for artifact payload
157      * @param insertStatementStr
158      * @param params
159      * @throws RuntimeException
160      */
161     private void executeStoreArtifactPayload(String insertStatementStr, ArrayList<String> params) throws RuntimeException {
162
163         try {
164             logger.info("used schema=" + this.schema);
165             logger.info("insert statement=" + insertStatementStr);
166
167             dbLibService.writeData(insertStatementStr, params, this.schema);
168
169             logger.info("finished to execute insert");
170
171         } catch (SQLException e) {
172             logger.error("Storing Artifact payload failed - " + insertStatementStr);
173             throw new RuntimeException("Storing Artifact payload failed - " + insertStatementStr);
174         }
175     }
176
177 }