2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ===================================================================
9 * Modifications Copyright (C) 2019 IBM
10 * ===================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=============================================
24 * ====================================================================
27 package org.onap.music.datastore.jsonobjects;
29 import java.util.List;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.Response.Status;
35 import org.onap.music.datastore.Condition;
36 import org.onap.music.datastore.MusicDataStoreHandle;
37 import org.onap.music.datastore.PreparedQueryObject;
38 import org.onap.music.eelf.logging.EELFLoggerDelegate;
39 import org.onap.music.eelf.logging.format.AppMessages;
40 import org.onap.music.eelf.logging.format.ErrorSeverity;
41 import org.onap.music.eelf.logging.format.ErrorTypes;
42 import org.onap.music.exceptions.MusicQueryException;
43 import org.onap.music.exceptions.MusicServiceException;
44 import org.onap.music.main.MusicUtil;
46 import com.datastax.driver.core.DataType;
47 import com.datastax.driver.core.TableMetadata;
48 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
50 import io.swagger.annotations.ApiModel;
51 import io.swagger.annotations.ApiModelProperty;
53 @ApiModel(value = "JsonTable", description = "Json model for delete")
54 @JsonIgnoreProperties(ignoreUnknown = true)
55 public class JsonDelete {
57 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JsonDelete.class);
59 private List<String> columns = null;
60 private Map<String, String> consistencyInfo;
61 private Map<String, Object> conditions;
63 private String timestamp;
64 private String keyspaceName;
65 private String tableName;
66 private StringBuilder rowIdString;
67 private String primarKeyValue;
70 @ApiModelProperty(value = "Conditions")
71 public Map<String, Object> getConditions() {
75 public void setConditions(Map<String, Object> conditions) {
76 this.conditions = conditions;
79 @ApiModelProperty(value = "Consistency level", allowableValues = "eventual,critical,atomic")
80 public Map<String, String> getConsistencyInfo() {
81 return consistencyInfo;
84 public void setConsistencyInfo(Map<String, String> consistencyInfo) {
85 this.consistencyInfo = consistencyInfo;
88 @ApiModelProperty(value = "Column values")
89 public List<String> getColumns() {
93 public void setColumns(List<String> columns) {
94 this.columns = columns;
98 @ApiModelProperty(value = "Time to live information")
99 public String getTtl() {
103 public void setTtl(String ttl) {
107 @ApiModelProperty(value = "Time stamp")
108 public String getTimestamp() {
112 public void setTimestamp(String timestamp) {
113 this.timestamp = timestamp;
116 public String getKeyspaceName() {
120 public void setKeyspaceName(String keyspaceName) {
121 this.keyspaceName = keyspaceName;
124 public String getTableName() {
128 public void setTableName(String tableName) {
129 this.tableName = tableName;
132 public StringBuilder getRowIdString() {
136 public void setRowIdString(StringBuilder rowIdString) {
137 this.rowIdString = rowIdString;
140 public String getPrimarKeyValue() {
141 return primarKeyValue;
144 public void setPrimarKeyValue(String primarKeyValue) {
145 this.primarKeyValue = primarKeyValue;
149 public PreparedQueryObject genDeletePreparedQueryObj(MultivaluedMap<String, String> rowParams) throws MusicQueryException {
150 if (logger.isDebugEnabled()) {
151 logger.debug("Coming inside genUpdatePreparedQueryObj method " + this.getKeyspaceName());
152 logger.debug("Coming inside genUpdatePreparedQueryObj method " + this.getTableName());
155 PreparedQueryObject queryObject = new PreparedQueryObject();
157 if((this.getKeyspaceName() == null || this.getKeyspaceName().isEmpty())
158 || (this.getTableName() == null || this.getTableName().isEmpty())){
161 throw new MusicQueryException("one or more path parameters are not set, please check and try again",
162 Status.BAD_REQUEST.getStatusCode());
165 EELFLoggerDelegate.mdcPut("keyspace", "( "+this.getKeyspaceName()+" ) ");
168 logger.error(EELFLoggerDelegate.errorLogger,"Required HTTP Request body is missing.", AppMessages.MISSINGDATA ,ErrorSeverity.WARN, ErrorTypes.DATAERROR);
170 throw new MusicQueryException("Required HTTP Request body is missing.",
171 Status.BAD_REQUEST.getStatusCode());
173 StringBuilder columnString = new StringBuilder();
176 List<String> columnList = this.getColumns();
177 if (columnList != null) {
178 for (String column : columnList) {
179 columnString.append(column);
180 if (counter != columnList.size() - 1)
181 columnString.append(",");
182 counter = counter + 1;
186 // get the row specifier
187 RowIdentifier rowId = null;
189 rowId = getRowIdentifier(this.getKeyspaceName(), this.getTableName(), rowParams, queryObject);
190 this.setRowIdString(rowId.rowIdString);
191 this.setPrimarKeyValue(rowId.primarKeyValue);
192 if(rowId == null || rowId.primarKeyValue.isEmpty()) {
194 throw new MusicQueryException("Mandatory WHERE clause is missing. Please check the input request.",
195 Status.BAD_REQUEST.getStatusCode());
197 } catch (MusicServiceException ex) {
198 logger.error(EELFLoggerDelegate.errorLogger,ex, AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes
199 .GENERALSERVICEERROR, ex);
200 /*return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build();*/
201 throw new MusicQueryException(AppMessages.UNKNOWNERROR.toString(), Status.BAD_REQUEST.getStatusCode());
203 String rowSpec = rowId.rowIdString.toString();
205 if ((columnList != null) && (!rowSpec.isEmpty())) {
206 queryObject.appendQueryString("DELETE " + columnString + " FROM " + this.getKeyspaceName() + "."
207 + this.getTableName() + " WHERE " + rowSpec + ";");
210 if ((columnList == null) && (!rowSpec.isEmpty())) {
211 queryObject.appendQueryString("DELETE FROM " + this.getKeyspaceName() + "." + this.getTableName() + " WHERE "
215 if ((columnList != null) && (rowSpec.isEmpty())) {
216 queryObject.appendQueryString(
217 "DELETE " + columnString + " FROM " + this.getKeyspaceName() + "." + rowSpec + ";");
219 // get the conditional, if any
220 Condition conditionInfo;
221 if (this.getConditions() == null) {
222 conditionInfo = null;
224 // to avoid parsing repeatedly, just send the select query to
226 PreparedQueryObject selectQuery = new PreparedQueryObject();
227 selectQuery.appendQueryString("SELECT * FROM " + this.getKeyspaceName() + "." + this.getTableName() + " WHERE "
228 + rowId.rowIdString + ";");
229 selectQuery.addValue(rowId.primarKeyValue);
230 conditionInfo = new Condition(this.getConditions(), selectQuery);
233 String consistency = this.getConsistencyInfo().get("type");
236 if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && this.getConsistencyInfo().get("consistency")!=null) {
237 if(MusicUtil.isValidConsistency(this.getConsistencyInfo().get("consistency"))) {
238 queryObject.setConsistency(this.getConsistencyInfo().get("consistency"));
240 throw new MusicQueryException("Invalid Consistency type", Status.BAD_REQUEST.getStatusCode());
244 queryObject.setOperation("delete");
257 * @throws MusicServiceException
259 private RowIdentifier getRowIdentifier(String keyspace, String tablename,
260 MultivaluedMap<String, String> rowParams, PreparedQueryObject queryObject)
261 throws MusicServiceException {
262 StringBuilder rowSpec = new StringBuilder();
264 TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
265 if (tableInfo == null) {
266 logger.error(EELFLoggerDelegate.errorLogger,
267 "Table information not found. Please check input for table name= "
268 + keyspace + "." + tablename);
269 throw new MusicServiceException(
270 "Table information not found. Please check input for table name= "
271 + keyspace + "." + tablename);
273 StringBuilder primaryKey = new StringBuilder();
274 for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
275 String keyName = entry.getKey();
276 List<String> valueList = entry.getValue();
277 String indValue = valueList.get(0);
278 DataType colType = null;
279 Object formattedValue = null;
281 colType = tableInfo.getColumn(entry.getKey()).getType();
282 formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
283 } catch (Exception e) {
284 logger.error(EELFLoggerDelegate.errorLogger,e);
286 if(tableInfo.getPrimaryKey().get(0).getName().equals(entry.getKey())) {
287 primaryKey.append(indValue);
289 rowSpec.append(keyName + "= ?");
290 queryObject.addValue(formattedValue);
291 if (counter != rowParams.size() - 1) {
292 rowSpec.append(" AND ");
294 counter = counter + 1;
296 return new RowIdentifier(primaryKey.toString(), rowSpec, queryObject);
299 private class RowIdentifier {
300 private String primarKeyValue;
301 private StringBuilder rowIdString;
302 @SuppressWarnings("unused")
303 public PreparedQueryObject queryObject; // the string with all the row
304 // identifiers separated by AND
306 public RowIdentifier(String primaryKeyValue, StringBuilder rowIdString,
307 PreparedQueryObject queryObject) {
308 this.primarKeyValue = primaryKeyValue;
309 this.rowIdString = rowIdString;
310 this.queryObject = queryObject;