2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.licmgr.impl;
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;
34 import javax.sql.rowset.CachedRowSet;
36 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS;
38 import java.sql.SQLException;
39 import java.util.ArrayList;
40 import java.util.HashMap;
44 @SuppressWarnings("JavaDoc")
45 class LicenseDataAccessServiceImpl implements LicenseDataAccessService {
47 private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);
49 public void setSchema(String schema) {
53 private String schema;
55 private DbLibService dbLibService;
57 private void checkDbLibService() throws DataAccessException {
58 if (null != dbLibService) {return;}
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);
70 public LicenseDataAccessServiceImpl(){}
73 public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
78 Map<String,String> result = new HashMap<>();
79 if (null == fields || 0 == fields.length) fields = new String[]{ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};
81 String queryString = buildQueryStatement();
83 ArrayList<String> argList = new ArrayList<>();
85 argList.add(vnfVersion);
86 argList.add(Constants.VF_LICENSE);
90 final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);
93 for (String field : fields) {
94 result.put(field, data.getString(field));
97 String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.ASDC_ARTIFACTS_TABLE_NAME;
100 } catch (SQLException e) {
101 logger.error("Error Accessing Database " + e);
102 throw new DataAccessException(e);
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() + " = ?";
116 * Implementation of storeArtifactPayload()
117 * @see LicenseDataAccessService
120 public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {
124 if(parameters == null || parameters.isEmpty()) {
125 throw new RuntimeException("No parameters for insert are provided");
128 String insertStr = "INSERT INTO " + Constants.ASDC_ARTIFACTS_TABLE_NAME + "(";
129 String valuesStr = "VALUES(";
130 String insertStatementStr;
132 ArrayList<String> params = new ArrayList<>();
133 boolean firstTime = true;
134 for(Map.Entry<String, String> entry : parameters.entrySet()) {
142 insertStr += entry.getKey();
145 params.add(entry.getValue());
150 insertStatementStr = insertStr + " " + valuesStr;
152 executeStoreArtifactPayload(insertStatementStr, params);
156 * Exexutes insert statement for artifact payload
157 * @param insertStatementStr
159 * @throws RuntimeException
161 private void executeStoreArtifactPayload(String insertStatementStr, ArrayList<String> params) throws RuntimeException {
164 logger.info("used schema=" + this.schema);
165 logger.info("insert statement=" + insertStatementStr);
167 dbLibService.writeData(insertStatementStr, params, this.schema);
169 logger.info("finished to execute insert");
171 } catch (SQLException e) {
172 logger.error("Storing Artifact payload failed - " + insertStatementStr);
173 throw new RuntimeException("Storing Artifact payload failed - " + insertStatementStr);