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 javax.sql.rowset.CachedRowSet;
26 import org.openecomp.appc.licmgr.Constants;
27 import org.openecomp.appc.licmgr.LicenseDataAccessService;
28 import org.openecomp.appc.licmgr.exception.DataAccessException;
29 import com.att.eelf.configuration.EELFLogger;
30 import com.att.eelf.configuration.EELFManager;
31 import org.openecomp.sdnc.sli.resource.dblib.DbLibService;
33 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS;
35 import java.sql.SQLException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
41 @SuppressWarnings("JavaDoc")
42 public class LicenseDataAccessServiceImpl implements LicenseDataAccessService {
44 private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);
46 public void setSchema(String schema) {
50 private String schema;
52 public void setDbLibService(DbLibService dbLibService) {
53 this.dbLibService = dbLibService;
56 private DbLibService dbLibService;
62 public LicenseDataAccessServiceImpl(){}
65 public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
68 Map<String,String> result = new HashMap<>();
69 if (null == fields || 0 == fields.length) fields = new String[]{ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};
71 String queryString = buildQueryStatement();
73 ArrayList<String> argList = new ArrayList<>();
75 argList.add(vnfVersion);
76 argList.add(Constants.VF_LICENSE);
80 final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);
83 for (String field : fields) {
84 result.put(field, data.getString(field));
87 String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.ASDC_ARTIFACTS_TABLE_NAME;
90 } catch (SQLException e) {
91 logger.error("Error Accessing Database " + e);
92 throw new DataAccessException(e);
98 private String buildQueryStatement() {
99 return "select * " + "from " + Constants.ASDC_ARTIFACTS_TABLE_NAME + " " +
100 "where " + ASDC_ARTIFACTS_FIELDS.RESOURCE_NAME.name() + " = ?" +
101 " AND " + ASDC_ARTIFACTS_FIELDS.RESOURCE_VERSION.name() + " = ?" +
102 " AND " + ASDC_ARTIFACTS_FIELDS.ARTIFACT_TYPE.name() + " = ?";
106 * Implementation of storeArtifactPayload()
107 * @see LicenseDataAccessService
110 public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {
112 if(parameters == null || parameters.isEmpty()) {
113 throw new RuntimeException("No parameters for insert are provided");
116 String insertStr = "INSERT INTO " + Constants.ASDC_ARTIFACTS_TABLE_NAME + "(";
117 String valuesStr = "VALUES(";
118 String insertStatementStr;
120 ArrayList<String> params = new ArrayList<>();
121 boolean firstTime = true;
122 for(Map.Entry<String, String> entry : parameters.entrySet()) {
130 insertStr += entry.getKey();
133 params.add(entry.getValue());
138 insertStatementStr = insertStr + " " + valuesStr;
140 executeStoreArtifactPayload(insertStatementStr, params);
144 * Exexutes insert statement for artifact payload
145 * @param insertStatementStr
147 * @throws RuntimeException
149 private void executeStoreArtifactPayload(String insertStatementStr, ArrayList<String> params) throws RuntimeException {
152 logger.info("used schema=" + this.schema);
153 logger.info("insert statement=" + insertStatementStr);
155 dbLibService.writeData(insertStatementStr, params, this.schema);
157 logger.info("finished to execute insert");
159 } catch (SQLException e) {
160 logger.error("Storing Artifact payload failed - " + insertStatementStr);
161 throw new RuntimeException("Storing Artifact payload failed - " + insertStatementStr);