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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
15 * ============LICENSE_END=============================================
16 * ====================================================================
18 package org.onap.music.rest;
20 import java.util.List;
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;
55 * These are functions created purely for benchmarking purposes. Commented out Swagger - This should
59 @Path("/v{version: [0-9]+}/benchmarks/")
60 @Api(value = "Benchmark API", hidden = true)
61 public class RestMusicBmAPI {
63 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class);
73 @Path("/purezk/{name}")
74 @Consumes(MediaType.APPLICATION_JSON)
75 public void pureZkCreate(@PathParam("name") String nodeName) throws Exception {
76 MusicCore.pureZkCreate("/" + nodeName);
87 @Path("/purezk/{name}")
88 @Consumes(MediaType.APPLICATION_JSON)
89 public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName)
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");
105 @Path("/purezk/{name}")
106 @Consumes(MediaType.TEXT_PLAIN)
107 public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception {
108 return MusicCore.pureZkRead(nodeName);
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");
127 logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + " update-" + operationId
128 + "-------------------------");
130 byte[] data = updateObj.serialize();
131 long jsonParseCompletionTime = System.currentTimeMillis();
133 String lockId = MusicCore.createLockReference(lockname);
135 long lockCreationTime = System.currentTimeMillis();
137 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
138 ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod);
139 long lockAcqTime = System.currentTimeMillis();
140 long zkPutTime = 0, lockReleaseTime = 0;
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();
153 MusicCore.destroyLockRef(lockId);
156 long actualUpdateCompletionTime = System.currentTimeMillis();
159 long endTime = System.currentTimeMillis();
161 String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime)
162 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
163 + "|zk put time:" + (zkPutTime - lockAcqTime);
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) + "|";
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)
176 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
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);
202 MusicCore.destroyLockRef(lockId);
205 long end = System.currentTimeMillis();
206 logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms");
211 * doing an update directly to cassa but through the rest api
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)
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);
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;
251 // get the row specifier
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;
272 String ttl = insObj.getTtl();
273 String timestamp = insObj.getTimestamp();
275 if ((ttl != null) && (timestamp != null)) {
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));
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));
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));
294 queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";");
296 long jsonParseCompletionTime = System.currentTimeMillis();
298 boolean operationResult = true;
299 MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type"));
301 long actualUpdateCompletionTime = System.currentTimeMillis();
303 long endTime = System.currentTimeMillis();
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)
310 logger.info(EELFLoggerDelegate.applicationLogger,timingString);
312 return operationResult;