MUSIC-ORM-Implemetation
[music.git] / src / main / java / org / onap / music / datastore / jsonobjects / JsonKeySpace.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.datastore.jsonobjects;
26
27 import java.util.Map;
28
29 import javax.ws.rs.core.Response.Status;
30
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;
38
39 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
40
41 import io.swagger.annotations.ApiModel;
42 import io.swagger.annotations.ApiModelProperty;
43
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;
52
53     @ApiModelProperty(value = "Consistency level", allowableValues = "eventual,critical,atomic")
54     public Map<String, String> getConsistencyInfo() {
55         return consistencyInfo;
56     }
57
58     public void setConsistencyInfo(Map<String, String> consistencyInfo) {
59         this.consistencyInfo = consistencyInfo;
60     }
61
62     @ApiModelProperty(value = "Replication information")
63     public Map<String, Object> getReplicationInfo() {
64         return replicationInfo;
65     }
66
67     public void setReplicationInfo(Map<String, Object> replicationInfo) {
68         this.replicationInfo = replicationInfo;
69     }
70
71     @ApiModelProperty(value = "Durability", allowableValues = "true,false")
72     public String getDurabilityOfWrites() {
73         return durabilityOfWrites;
74     }
75
76     public void setDurabilityOfWrites(String durabilityOfWrites) {
77         this.durabilityOfWrites = durabilityOfWrites;
78     }
79
80     @ApiModelProperty(value = "Keyspace name")
81     public String getKeyspaceName() {
82         return keyspaceName;
83     }
84
85     public void setKeyspaceName(String keyspaceName) {
86         this.keyspaceName = keyspaceName;
87     }
88
89     /**
90      * Will generate query to create Keyspacce.
91      * 
92      * @throws MusicQueryException
93      */
94     @SuppressWarnings("deprecation")
95     public PreparedQueryObject genCreateKeyspaceQuery() throws MusicQueryException {
96
97         if (logger.isDebugEnabled()) {
98             logger.debug("Came inside createKeyspace method");
99         }
100
101         String keyspaceName = this.getKeyspaceName();
102         String durabilityOfWrites = this.getDurabilityOfWrites();
103         String consistency = MusicUtil.EVENTUAL;
104
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);
109
110         PreparedQueryObject queryObject = new PreparedQueryObject();
111         
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"));
115             }else {
116                 throw new MusicQueryException("Invalid Consistency type",Status.BAD_REQUEST.getStatusCode());
117             }  
118         }
119         
120         long start = System.currentTimeMillis();
121         Map<String, Object> replicationInfo = this.getReplicationInfo();
122         String repString = null;
123         try {
124             repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}";
125         } catch (Exception e) {
126             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), AppMessages.MISSINGDATA,
127                     ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
128         }
129         queryObject.appendQueryString("CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString);
130         if (this.getDurabilityOfWrites() != null) {
131             queryObject.appendQueryString(" AND durable_writes = " + this.getDurabilityOfWrites());
132         }
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));
137
138         return queryObject;
139     }
140
141     /**
142      * Will generate Query to drop a keyspace.
143      * 
144      * @return
145      */
146     public PreparedQueryObject genDropKeyspaceQuery() {
147         if (logger.isDebugEnabled()) {
148             logger.debug("Coming inside genDropKeyspaceQuery method "+this.getKeyspaceName());
149         }
150
151         PreparedQueryObject queryObject = new PreparedQueryObject();
152         queryObject.appendQueryString("DROP KEYSPACE " + this.getKeyspaceName() + ";");
153
154         return queryObject;
155     }
156
157     @Override
158     public String toString() {
159         return "CassaKeyspaceObject [keyspaceName=" + keyspaceName + ", replicationInfo=" + replicationInfo
160                 + "durabilityOfWrites=" + durabilityOfWrites + "]";
161     }
162
163 }