2eaf79062f96b2fdfcce21b8c0d13914cff492d5
[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 javax.sql.rowset.CachedRowSet;
25
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;
32
33 import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS;
34
35 import java.sql.SQLException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Map;
39
40
41 @SuppressWarnings("JavaDoc")
42 public class LicenseDataAccessServiceImpl implements LicenseDataAccessService {
43
44     private static EELFLogger logger = EELFManager.getInstance().getLogger(LicenseDataAccessServiceImpl.class);
45
46     public void setSchema(String schema) {
47         this.schema = schema;
48     }
49
50     private String schema;
51
52     public void setDbLibService(DbLibService dbLibService) {
53         this.dbLibService = dbLibService;
54     }
55
56     private DbLibService dbLibService;
57
58
59     /**
60      * empty constructor
61      */
62     public LicenseDataAccessServiceImpl(){}
63
64     @Override
65     public Map<String,String> retrieveLicenseModelData(String vnfType, String vnfVersion, String... fields) throws
66                     DataAccessException {
67
68         Map<String,String> result = new HashMap<>();
69         if (null == fields || 0 == fields.length) fields = new String[]{ASDC_ARTIFACTS_FIELDS.ARTIFACT_CONTENT.name()};
70
71         String queryString = buildQueryStatement();
72
73         ArrayList<String> argList = new ArrayList<>();
74         argList.add(vnfType);
75         argList.add(vnfVersion);
76         argList.add(Constants.VF_LICENSE);
77
78         try {
79
80             final CachedRowSet data = dbLibService.getData(queryString, argList, Constants.NETCONF_SCHEMA);
81
82             if (data.first()) {
83                 for (String field : fields) {
84                     result.put(field, data.getString(field));
85                 }
86             } else {
87                 String msg = "Missing license model for VNF_TYPE: " + vnfType + " and VNF_VERSION: " + vnfVersion + " in table " + Constants.ASDC_ARTIFACTS_TABLE_NAME;
88                 logger.info(msg);
89             }
90         } catch (SQLException e) {
91             logger.error("Error Accessing Database " + e);
92             throw new DataAccessException(e);
93         }
94
95         return result;
96     }
97
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() + " = ?";
103     }
104
105     /**
106      * Implementation of storeArtifactPayload()
107      * @see LicenseDataAccessService
108      */
109     @Override
110     public void storeArtifactPayload(Map<String, String> parameters) throws RuntimeException {
111
112         if(parameters == null || parameters.isEmpty()) {
113             throw new RuntimeException("No parameters for insert are provided");
114         }
115
116         String insertStr = "INSERT INTO " + Constants.ASDC_ARTIFACTS_TABLE_NAME + "(";
117         String valuesStr = "VALUES(";
118         String insertStatementStr;
119
120         ArrayList<String> params = new ArrayList<>();
121         boolean firstTime = true;
122         for(Map.Entry<String, String> entry : parameters.entrySet()) {
123             if(!firstTime) {
124                 insertStr += ",";
125                 valuesStr += ",";
126             }
127             else {
128                 firstTime = false;
129             }
130             insertStr += entry.getKey();
131             valuesStr += "?";
132
133             params.add(entry.getValue());
134         }
135
136         insertStr += ")";
137         valuesStr += ")";
138         insertStatementStr = insertStr + " " + valuesStr;
139
140         executeStoreArtifactPayload(insertStatementStr, params);
141     }
142
143     /**
144      * Exexutes insert statement for artifact payload
145      * @param insertStatementStr
146      * @param params
147      * @throws RuntimeException
148      */
149     private void executeStoreArtifactPayload(String insertStatementStr, ArrayList<String> params) throws RuntimeException {
150
151         try {
152             logger.info("used schema=" + this.schema);
153             logger.info("insert statement=" + insertStatementStr);
154
155             dbLibService.writeData(insertStatementStr, params, this.schema);
156
157             logger.info("finished to execute insert");
158
159         } catch (SQLException e) {
160             logger.error("Storing Artifact payload failed - " + insertStatementStr);
161             throw new RuntimeException("Storing Artifact payload failed - " + insertStatementStr);
162         }
163     }
164
165 }