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