Sonar Fixes - MusicDataStoreHandle.java
[music.git] / src / main / java / org / onap / music / rest / RestMusicBmAPI.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.rest;
24
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.PUT;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MediaType;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.UriInfo;
39 import org.onap.music.datastore.jsonobjects.JsonInsert;
40 import org.onap.music.eelf.logging.EELFLoggerDelegate;
41 import org.onap.music.main.MusicCore;
42 import org.onap.music.main.MusicUtil;
43 import org.onap.music.main.ResultType;
44 import org.onap.music.main.ReturnType;
45 import org.onap.music.service.impl.MusicZKCore;
46 import org.onap.music.datastore.MusicDataStoreHandle;
47 import org.onap.music.datastore.PreparedQueryObject;
48 import com.datastax.driver.core.DataType;
49 import com.datastax.driver.core.TableMetadata;
50 import io.swagger.annotations.Api;
51
52 /*
53  * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
54  * be undocumented API
55  * 
56  */
57 @Path("/v{version: [0-9]+}/benchmarks/")
58 @Api(value = "Benchmark API", hidden = true)
59 public class RestMusicBmAPI {
60
61     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class);
62     public static final String UPDATE_CONST=" update-";
63     // pure zk calls...
64
65     /**
66      * 
67      * @param nodeName
68      * @throws Exception
69      */
70     @POST
71     @Path("/purezk/{name}")
72     @Consumes(MediaType.APPLICATION_JSON)
73     public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
74         MusicZKCore.pureZkCreate("/" + nodeName);
75     }
76
77
78     /**
79      * 
80      * @param insObj
81      * @param nodeName
82      * @throws Exception
83      */
84     @PUT
85     @Path("/purezk/{name}")
86     @Consumes(MediaType.APPLICATION_JSON)
87     public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
88                     throws Exception {
89         logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------");
90         long start = System.currentTimeMillis();
91         MusicZKCore.pureZkWrite(nodeName, insObj.serialize());
92         long end = System.currentTimeMillis();
93         logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms");
94     }
95
96     /**
97      * 
98      * @param nodeName
99      * @return
100      * @throws Exception
101      */
102     @GET
103     @Path("/purezk/{name}")
104     @Consumes(MediaType.TEXT_PLAIN)
105     public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
106         return MusicZKCore.pureZkRead(nodeName);
107     }
108
109     /**
110      * 
111      * @param insObj
112      * @param lockName
113      * @param nodeName
114      * @throws Exception
115      */
116     @PUT
117     @Path("/purezk/atomic/{lockname}/{name}")
118     @Consumes(MediaType.APPLICATION_JSON)
119     public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname,
120                     @PathParam("name") String nodeName) throws Exception {
121         long startTime = System.currentTimeMillis();
122         String operationId = UUID.randomUUID().toString();// just for debugging purposes.
123         String consistency = updateObj.getConsistencyInfo().get("type");
124
125         logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + UPDATE_CONST + operationId
126                         + "-------------------------");
127
128         byte[] data = updateObj.serialize();
129         long jsonParseCompletionTime = System.currentTimeMillis();
130
131         String lockId = MusicCore.createLockReference(lockname);
132
133         long lockCreationTime = System.currentTimeMillis();
134
135         long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
136         ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod);
137         long lockAcqTime = System.currentTimeMillis();
138         long zkPutTime = 0;
139         long lockReleaseTime = 0;
140
141         if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
142             logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId);
143             MusicZKCore.pureZkWrite(lockname, data);
144             zkPutTime = System.currentTimeMillis();
145             boolean voluntaryRelease = true;
146 /*<<<<<<< HEAD
147             if (("atomic").equals(consistency))
148                 MusicCore.releaseLock(lockId, voluntaryRelease);
149             else if (("atomic_delete_lock").equals(consistency))
150                 MusicCore.deleteLock(lockname);
151 =======*/
152             if (consistency.equals("atomic"))
153                 MusicCore.releaseLock(lockId, voluntaryRelease);
154             else if (consistency.equals("atomic_delete_lock"))
155                 MusicCore.deleteLock(lockname);
156             lockReleaseTime = System.currentTimeMillis();
157         } else {
158             MusicCore.destroyLockRef(lockId);
159         }
160
161         long actualUpdateCompletionTime = System.currentTimeMillis();
162
163
164         long endTime = System.currentTimeMillis();
165
166         String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
167                         + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
168                         + "|zk put time:" + (zkPutTime - lockAcqTime);
169
170         if ("atomic".equals(consistency))
171             lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|";
172         else if ("atomic_delete_lock".equals(consistency))
173             lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|";
174
175         String timingString = "Time taken in ms for Zookeeper " + consistency + UPDATE_CONST
176                         + operationId + ":" + "|total operation time:" + (endTime - startTime)
177                         + "|json parsing time:" + (jsonParseCompletionTime - startTime)
178                         + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
179                         + lockingInfo;
180
181         logger.info(EELFLoggerDelegate.applicationLogger,timingString);
182     }
183
184     /**
185      * 
186      * @param insObj
187      * @param lockName
188      * @param nodeName
189      * @throws Exception
190      */
191     @GET
192     @Path("/purezk/atomic/{lockname}/{name}")
193     @Consumes(MediaType.APPLICATION_JSON)
194     public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName,
195                     @PathParam("name") String nodeName) throws Exception {
196         logger.info("--------------Zk atomic read-------------------------");
197         long start = System.currentTimeMillis();
198         String lockId = MusicCore.createLockReference(lockName);
199         long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
200         ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod);
201         if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
202             logger.info("acquired lock with id " + lockId);
203             MusicZKCore.pureZkRead(nodeName);
204             boolean voluntaryRelease = true;
205             MusicCore.releaseLock(lockId, voluntaryRelease);
206         } else {
207             MusicCore.destroyLockRef(lockId);
208         }
209
210         long end = System.currentTimeMillis();
211         logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms");
212     }
213
214     /**
215      *
216      * doing an update directly to cassa but through the rest api
217      * 
218      * @param insObj
219      * @param keyspace
220      * @param tablename
221      * @param info
222      * @return
223      * @throws Exception
224      */
225     @PUT
226     @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows")
227     @Consumes(MediaType.APPLICATION_JSON)
228     @Produces(MediaType.APPLICATION_JSON)
229     public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace,
230                     @PathParam("tablename") String tablename, @Context UriInfo info)
231                     throws Exception {
232         long startTime = System.currentTimeMillis();
233         String operationId = UUID.randomUUID().toString();// just for debugging purposes.
234         String consistency = insObj.getConsistencyInfo().get("type");
235         logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + UPDATE_CONST + operationId
236                         + "-------------------------");
237         PreparedQueryObject queryObject = new PreparedQueryObject();
238         Map<String, Object> valuesMap = insObj.getValues();
239         TableMetadata tableInfo = MusicDataStoreHandle.returnColumnMetadata(keyspace, tablename);
240         String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'";
241         String fieldValueString = "vector_ts= ? ,";
242         queryObject.addValue(vectorTs);
243
244         int counter = 0;
245         for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
246             Object valueObj = entry.getValue();
247             DataType colType = tableInfo.getColumn(entry.getKey()).getType();
248             Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
249             fieldValueString = fieldValueString + entry.getKey() + "= ?";
250             queryObject.addValue(valueString);
251             if (counter != valuesMap.size() - 1)
252                 fieldValueString = fieldValueString + ",";
253             counter = counter + 1;
254         }
255
256         // get the row specifier
257         String rowSpec = "";
258         counter = 0;
259         queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " ");
260         MultivaluedMap<String, String> rowParams = info.getQueryParameters();
261         String primaryKey = "";
262         for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) {
263             String keyName = entry.getKey();
264             List<String> valueList = entry.getValue();
265             String indValue = valueList.get(0);
266             DataType colType = tableInfo.getColumn(entry.getKey()).getType();
267             Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue);
268             primaryKey = primaryKey + indValue;
269             rowSpec = rowSpec + keyName + "= ? ";
270             queryObject.addValue(formattedValue);
271             if (counter != rowParams.size() - 1)
272                 rowSpec = rowSpec + " AND ";
273             counter = counter + 1;
274         }
275
276
277         String ttl = insObj.getTtl();
278         String timestamp = insObj.getTimestamp();
279
280         if ((ttl != null) && (timestamp != null)) {
281
282             logger.info(EELFLoggerDelegate.applicationLogger,"both there");
283             queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?");
284             queryObject.addValue(Integer.parseInt(ttl));
285             queryObject.addValue(Long.parseLong(timestamp));
286         }
287
288         if ((ttl != null) && (timestamp == null)) {
289             logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there");
290             queryObject.appendQueryString(" USING TTL ?");
291             queryObject.addValue(Integer.parseInt(ttl));
292         }
293
294         if ((ttl == null) && (timestamp != null)) {
295             logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there");
296             queryObject.appendQueryString(" USING TIMESTAMP ?");
297             queryObject.addValue(Long.parseLong(timestamp));
298         }
299         queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
300
301         long jsonParseCompletionTime = System.currentTimeMillis();
302
303         boolean operationResult = true;
304         MusicDataStoreHandle.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
305
306         long actualUpdateCompletionTime = System.currentTimeMillis();
307
308         long endTime = System.currentTimeMillis();
309
310         String timingString = "Time taken in ms for Cassandra " + consistency + UPDATE_CONST
311                         + operationId + ":" + "|total operation time:" + (endTime - startTime)
312                         + "|json parsing time:" + (jsonParseCompletionTime - startTime)
313                         + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime)
314                         + "|";
315         logger.info(EELFLoggerDelegate.applicationLogger,timingString);
316
317         return operationResult;
318     }
319 }