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