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