2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Modifications Copyright (C) 2019 IBM
8 * ===================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=============================================
22 * ====================================================================
25 package org.onap.music.datastore.jsonobjects;
29 import javax.ws.rs.core.Response.Status;
31 import org.onap.music.datastore.PreparedQueryObject;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
36 import org.onap.music.exceptions.MusicQueryException;
37 import org.onap.music.main.MusicUtil;
39 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
41 import io.swagger.annotations.ApiModel;
42 import io.swagger.annotations.ApiModelProperty;
44 @ApiModel(value = "JsonTable", description = "Json model creating new keyspace")
45 @JsonIgnoreProperties(ignoreUnknown = true)
46 public class JsonKeySpace {
47 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(JsonKeySpace.class);
48 private String keyspaceName;
49 private Map<String, Object> replicationInfo;
50 private String durabilityOfWrites;
51 private Map<String, String> consistencyInfo;
53 @ApiModelProperty(value = "Consistency level", allowableValues = "eventual,critical,atomic")
54 public Map<String, String> getConsistencyInfo() {
55 return consistencyInfo;
58 public void setConsistencyInfo(Map<String, String> consistencyInfo) {
59 this.consistencyInfo = consistencyInfo;
62 @ApiModelProperty(value = "Replication information")
63 public Map<String, Object> getReplicationInfo() {
64 return replicationInfo;
67 public void setReplicationInfo(Map<String, Object> replicationInfo) {
68 this.replicationInfo = replicationInfo;
71 @ApiModelProperty(value = "Durability", allowableValues = "true,false")
72 public String getDurabilityOfWrites() {
73 return durabilityOfWrites;
76 public void setDurabilityOfWrites(String durabilityOfWrites) {
77 this.durabilityOfWrites = durabilityOfWrites;
80 @ApiModelProperty(value = "Keyspace name")
81 public String getKeyspaceName() {
85 public void setKeyspaceName(String keyspaceName) {
86 this.keyspaceName = keyspaceName;
90 * Will generate query to create Keyspacce.
92 * @throws MusicQueryException
94 @SuppressWarnings("deprecation")
95 public PreparedQueryObject genCreateKeyspaceQuery() throws MusicQueryException {
97 if (logger.isDebugEnabled()) {
98 logger.debug("Came inside createKeyspace method");
101 String keyspaceName = this.getKeyspaceName();
102 String durabilityOfWrites = this.getDurabilityOfWrites();
103 String consistency = MusicUtil.EVENTUAL;
105 logger.info("genCreateKeyspaceQuery keyspaceName ::" + keyspaceName);
106 logger.info("genCreateKeyspaceQuery class :: " + this.getReplicationInfo().get("class"));
107 logger.info("genCreateKeyspaceQuery replication_factor :: " + this.getReplicationInfo().get("replication_factor"));
108 logger.info("genCreateKeyspaceQuery durabilityOfWrites :: " + durabilityOfWrites);
110 PreparedQueryObject queryObject = new PreparedQueryObject();
112 if(consistency.equalsIgnoreCase(MusicUtil.EVENTUAL) && this.getConsistencyInfo().get("consistency") != null) {
113 if(MusicUtil.isValidConsistency(this.getConsistencyInfo().get("consistency"))) {
114 queryObject.setConsistency(this.getConsistencyInfo().get("consistency"));
116 throw new MusicQueryException("Invalid Consistency type",Status.BAD_REQUEST.getStatusCode());
120 long start = System.currentTimeMillis();
121 Map<String, Object> replicationInfo = this.getReplicationInfo();
122 String repString = null;
124 repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}";
125 } catch (Exception e) {
126 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.MISSINGDATA,
127 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
129 queryObject.appendQueryString("CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString);
130 if (this.getDurabilityOfWrites() != null) {
131 queryObject.appendQueryString(" AND durable_writes = " + this.getDurabilityOfWrites());
133 queryObject.appendQueryString(";");
134 long end = System.currentTimeMillis();
135 logger.info(EELFLoggerDelegate.applicationLogger,
136 "Time taken for setting up query in create keyspace:" + (end - start));
142 * Will generate Query to drop a keyspace.
146 public PreparedQueryObject genDropKeyspaceQuery() {
147 if (logger.isDebugEnabled()) {
148 logger.debug("Coming inside genDropKeyspaceQuery method "+this.getKeyspaceName());
151 PreparedQueryObject queryObject = new PreparedQueryObject();
152 queryObject.appendQueryString("DROP KEYSPACE " + this.getKeyspaceName() + ";");
158 public String toString() {
159 return "CassaKeyspaceObject [keyspaceName=" + keyspaceName + ", replicationInfo=" + replicationInfo
160 + "durabilityOfWrites=" + durabilityOfWrites + "]";