2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.main;
25 import java.io.StringWriter;
26 import java.util.HashMap;
28 import java.util.StringTokenizer;
29 import org.onap.music.datastore.MusicDataStore;
30 import org.onap.music.datastore.PreparedQueryObject;
31 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
32 // import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.exceptions.MusicLockingException;
34 import org.onap.music.exceptions.MusicQueryException;
35 import org.onap.music.exceptions.MusicServiceException;
36 import org.onap.music.lockingservice.MusicLockState;
37 import org.onap.music.lockingservice.MusicLockState.LockStatus;
38 import org.onap.music.lockingservice.MusicLockingService;
39 import com.att.eelf.configuration.EELFLogger;
40 import com.att.eelf.configuration.EELFManager;
41 import com.datastax.driver.core.ColumnDefinitions;
42 import com.datastax.driver.core.ColumnDefinitions.Definition;
43 import com.datastax.driver.core.DataType;
44 import com.datastax.driver.core.ResultSet;
45 import com.datastax.driver.core.Row;
46 import com.datastax.driver.core.TableMetadata;
53 public class MusicCore {
55 public static MusicLockingService mLockHandle = null;
56 public static MusicDataStore mDstoreHandle = null;
57 private static EELFLogger logger = EELFManager.getInstance().getLogger(MusicCore.class);
59 public static class Condition {
60 Map<String, Object> conditions;
61 String selectQueryForTheRow;
63 public Condition(Map<String, Object> conditions, String selectQueryForTheRow) {
64 this.conditions = conditions;
65 this.selectQueryForTheRow = selectQueryForTheRow;
68 public boolean testCondition() {
69 // first generate the row
70 PreparedQueryObject query = new PreparedQueryObject();
71 query.appendQueryString(selectQueryForTheRow);
72 ResultSet results = quorumGet(query);
73 Row row = results.one();
74 return getDSHandle().doesRowSatisfyCondition(row, conditions);
79 public static MusicLockingService getLockingServiceHandle() throws MusicLockingException {
80 logger.info("Acquiring lock store handle");
81 long start = System.currentTimeMillis();
83 if (mLockHandle == null) {
85 mLockHandle = new MusicLockingService();
86 } catch (Exception e) {
87 logger.error("Failed to aquire Locl store handle" + e.getMessage());
88 throw new MusicLockingException("Failed to aquire Locl store handle " + e);
91 long end = System.currentTimeMillis();
92 logger.info("Time taken to acquire lock store handle:" + (end - start) + " ms");
101 public static MusicDataStore getDSHandle(String remoteIp) {
102 logger.info("Acquiring data store handle");
103 long start = System.currentTimeMillis();
104 if (mDstoreHandle == null) {
105 mDstoreHandle = new MusicDataStore(remoteIp);
107 long end = System.currentTimeMillis();
108 logger.info("Time taken to acquire data store handle:" + (end - start) + " ms");
109 return mDstoreHandle;
116 public static MusicDataStore getDSHandle() {
117 logger.info("Acquiring data store handle");
118 long start = System.currentTimeMillis();
119 if (mDstoreHandle == null) {
120 mDstoreHandle = new MusicDataStore();
122 long end = System.currentTimeMillis();
123 logger.info("Time taken to acquire data store handle:" + (end - start) + " ms");
124 return mDstoreHandle;
127 public static String createLockReference(String lockName) {
128 logger.info("Creating lock reference for lock name:" + lockName);
129 long start = System.currentTimeMillis();
130 String lockId = null;
132 lockId = getLockingServiceHandle().createLockId("/" + lockName);
133 } catch (MusicLockingException e) {
134 logger.error("Failed to create Lock Reference " + lockName);
136 long end = System.currentTimeMillis();
137 logger.info("Time taken to create lock reference:" + (end - start) + " ms");
146 public static boolean isTableOrKeySpaceLock(String key) {
147 String[] splitString = key.split("\\.");
148 if (splitString.length > 2)
159 public static MusicLockState getMusicLockState(String key) {
160 long start = System.currentTimeMillis();
162 String[] splitString = key.split("\\.");
163 String keyspaceName = splitString[0];
164 String tableName = splitString[1];
165 String primaryKey = splitString[2];
167 String lockName = keyspaceName + "." + tableName + "." + primaryKey;
168 mls = getLockingServiceHandle().getLockState(lockName);
169 long end = System.currentTimeMillis();
170 logger.info("Time taken to get lock state:" + (end - start) + " ms");
172 } catch (NullPointerException | MusicLockingException e) {
173 logger.error("No lock object exists as of now.." + e);
178 public static ReturnType acquireLockWithLease(String key, String lockId, long leasePeriod) {
180 long start = System.currentTimeMillis();
181 /* check if the current lock has exceeded its lease and if yes, release that lock */
182 MusicLockState mls = getMusicLockState(key);
184 if (mls.getLockStatus().equals(LockStatus.LOCKED)) {
185 logger.info("The current lock holder for " + key + " is " + mls.getLockHolder()
186 + ". Checking if it has exceeded lease");
187 long currentLockPeriod = System.currentTimeMillis() - mls.getLeaseStartTime();
188 long currentLeasePeriod = mls.getLeasePeriod();
189 if (currentLockPeriod > currentLeasePeriod) {
190 logger.info("Lock period " + currentLockPeriod
191 + " has exceeded lease period " + currentLeasePeriod);
192 boolean voluntaryRelease = false;
193 String currentLockHolder = mls.getLockHolder();
194 mls = releaseLock(currentLockHolder, voluntaryRelease);
198 logger.debug("There is no lock state object for " + key);
201 * call the traditional acquire lock now and if the result returned is true, set the
202 * begin time-stamp and lease period
204 if (acquireLock(key, lockId) == true) {
205 mls = getMusicLockState(key);// get latest state
206 if (mls.getLeaseStartTime() == -1) {// set it again only if it is not set already
207 mls.setLeaseStartTime(System.currentTimeMillis());
208 mls.setLeasePeriod(leasePeriod);
209 getLockingServiceHandle().setLockState(key, mls);
211 long end = System.currentTimeMillis();
212 logger.info("Time taken to acquire leased lock:" + (end - start) + " ms");
213 return new ReturnType(ResultType.SUCCESS, "Accquired lock");
215 long end = System.currentTimeMillis();
216 logger.info("Time taken to fail to acquire leased lock:" + (end - start) + " ms");
217 return new ReturnType(ResultType.FAILURE, "Could not acquire lock");
219 } catch (Exception e) {
220 StringWriter sw = new StringWriter();
221 logger.error(e.getMessage());
222 String exceptionAsString = sw.toString();
223 return new ReturnType(ResultType.FAILURE,
224 "Exception thrown in acquireLockWithLease:\n" + exceptionAsString);
228 public static boolean acquireLock(String key, String lockId) {
230 * first check if I am on top. Since ids are not reusable there is no need to check
231 * lockStatus If the status is unlocked, then the above call will automatically return
234 Boolean result = false;
236 result = getLockingServiceHandle().isMyTurn(lockId);
237 } catch (MusicLockingException e2) {
238 logger.error("Failed to aquireLock lockId " + lockId + " " + e2);
240 if (result == false) {
241 logger.info("In acquire lock: Not your turn, someone else has the lock");
246 // this is for backward compatibility where locks could also be acquired on just
247 // keyspaces or tables.
248 if (isTableOrKeySpaceLock(key) == true) {
249 logger.info("In acquire lock: A table or keyspace lock so no need to perform sync...so returning true");
253 // read the lock name corresponding to the key and if the status is locked or being locked,
255 MusicLockState currentMls = null;
256 MusicLockState newMls = null;
258 currentMls = getMusicLockState(key);
259 String currentLockHolder = currentMls.getLockHolder();
260 if (lockId.equals(currentLockHolder)) {
261 logger.info("In acquire lock: You already have the lock!");
264 } catch (NullPointerException e) {
265 logger.error("In acquire lock:No one has tried to acquire the lock yet..");
268 // change status to "being locked". This state transition is necessary to ensure syncing
269 // before granting the lock
270 String lockHolder = null;
271 boolean needToSyncQuorum = false;
272 if (currentMls != null)
273 needToSyncQuorum = currentMls.isNeedToSyncQuorum();
276 newMls = new MusicLockState(MusicLockState.LockStatus.BEING_LOCKED, lockHolder,
279 getLockingServiceHandle().setLockState(key, newMls);
280 } catch (MusicLockingException e1) {
281 logger.error("Failed to set Lock state " + key + " " + e1);
283 logger.info("In acquire lock: Set lock state to being_locked");
285 // do syncing if this was a forced lock release
286 if (needToSyncQuorum) {
287 logger.info("In acquire lock: Since there was a forcible release, need to sync quorum!");
291 // change status to locked
293 needToSyncQuorum = false;
294 newMls = new MusicLockState(MusicLockState.LockStatus.LOCKED, lockHolder, needToSyncQuorum);
296 getLockingServiceHandle().setLockState(key, newMls);
297 } catch (MusicLockingException e) {
298 logger.error("Failed to set Lock state " + key + " " + e);
300 logger.info("In acquire lock: Set lock state to locked and assigned current lock ref "
301 + lockId + " as holder");
309 * @param keyspaceName
314 public boolean createKeyspace(String keyspaceName, JsonKeySpace kspObject) throws Exception {
319 private static void syncQuorum(String key) {
320 logger.info("Performing sync operation---");
321 String[] splitString = key.split("\\.");
322 String keyspaceName = splitString[0];
323 String tableName = splitString[1];
324 String primaryKeyValue = splitString[2];
325 PreparedQueryObject selectQuery = new PreparedQueryObject();
326 PreparedQueryObject updateQuery = new PreparedQueryObject();
328 // get the primary key d
329 TableMetadata tableInfo = returnColumnMetadata(keyspaceName, tableName);
330 String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName();// we only support single
332 DataType primaryKeyType = tableInfo.getPrimaryKey().get(0).getType();
333 String cqlFormattedPrimaryKeyValue =
334 MusicUtil.convertToCQLDataType(primaryKeyType, primaryKeyValue);
336 // get the row of data from a quorum
337 selectQuery.appendQueryString("SELECT * FROM " + keyspaceName + "." + tableName + " WHERE "
338 + primaryKeyName + "= ?" + ";");
339 selectQuery.addValue(cqlFormattedPrimaryKeyValue);
340 // String selectQuery = "SELECT * FROM "+keyspaceName+"."+tableName+ " WHERE
341 // "+primaryKeyName+"="+cqlFormattedPrimaryKeyValue+";";
342 ResultSet results = null;
344 results = getDSHandle().executeCriticalGet(selectQuery);
345 // write it back to a quorum
346 Row row = results.one();
347 ColumnDefinitions colInfo = row.getColumnDefinitions();
348 int totalColumns = colInfo.size();
350 // String fieldValueString="";
351 StringBuilder fieldValueString = new StringBuilder("");
352 for (Definition definition : colInfo) {
353 String colName = definition.getName();
354 if (colName.equals(primaryKeyName))
356 DataType colType = definition.getType();
357 Object valueObj = getDSHandle().getColValue(row, colName, colType);
358 String valueString = MusicUtil.convertToCQLDataType(colType, valueObj);
359 // fieldValueString = fieldValueString+ colName+"="+valueString;
360 fieldValueString.append(colName + " = ?");
361 updateQuery.addValue(valueString);
362 if (counter != (totalColumns - 1))
363 fieldValueString.append(",");
364 counter = counter + 1;
366 updateQuery.appendQueryString("UPDATE " + keyspaceName + "." + tableName + " SET "
367 + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
368 updateQuery.addValue(cqlFormattedPrimaryKeyValue);
369 // String updateQuery = "UPDATE "+keyspaceName+"."+tableName+" SET "+fieldValueString+"
370 // WHERE "+primaryKeyName+"="+cqlFormattedPrimaryKeyValue+";";
372 getDSHandle().executePut(updateQuery, "critical");
373 } catch (MusicServiceException | MusicQueryException e) {
374 logger.error("Failed to execute update query " + updateQuery + " " + e);
380 * this function is mainly for the benchmarks to see the effect of lock deletion.
382 * @param keyspaceName
386 * @param conditionInfo
389 public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName,
390 String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) {
391 long start = System.currentTimeMillis();
392 String key = keyspaceName + "." + tableName + "." + primaryKey;
393 String lockId = createLockReference(key);
394 long lockCreationTime = System.currentTimeMillis();
395 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
396 ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod);
397 long lockAcqTime = System.currentTimeMillis();
398 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
399 logger.info("acquired lock with id " + lockId);
400 ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey,
401 queryObject, lockId, conditionInfo);
402 long criticalPutTime = System.currentTimeMillis();
404 long lockDeleteTime = System.currentTimeMillis();
405 String timingInfo = "|lock creation time:" + (lockCreationTime - start)
406 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
407 + "|critical put time:" + (criticalPutTime - lockAcqTime)
408 + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|";
409 criticalPutResult.setTimingInfo(timingInfo);
410 return criticalPutResult;
412 logger.info("unable to acquire lock, id " + lockId);
414 return lockAcqResult;
423 public static ResultSet quorumGet(PreparedQueryObject query) {
424 ResultSet results = null;
426 results = getDSHandle().executeCriticalGet(query);
427 } catch (MusicServiceException | MusicQueryException e) {
428 logger.error(e.getMessage());
439 public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) {
440 return getDSHandle().marshalData(results);
448 public static String whoseTurnIsIt(String lockName) {
451 return getLockingServiceHandle().whoseTurnIsIt("/" + lockName) + "";
452 } catch (MusicLockingException e) {
453 logger.error("Failed whoseTurnIsIt " + lockName + " " + e);
465 public static String getLockNameFromId(String lockId) {
466 StringTokenizer st = new StringTokenizer(lockId);
467 return st.nextToken("$");
470 public static void destroyLockRef(String lockId) {
471 long start = System.currentTimeMillis();
473 getLockingServiceHandle().unlockAndDeleteId(lockId);
474 } catch (MusicLockingException e) {
475 logger.error("Failed to Destroy Lock Ref " + lockId + " " + e);
477 long end = System.currentTimeMillis();
478 logger.info("Time taken to destroy lock reference:" + (end - start) + " ms");
481 public static MusicLockState releaseLock(String lockId, boolean voluntaryRelease) {
482 long start = System.currentTimeMillis();
484 getLockingServiceHandle().unlockAndDeleteId(lockId);
485 } catch (MusicLockingException e1) {
486 logger.error("Failed to release Lock " + lockId + " " + e1);
488 String lockName = getLockNameFromId(lockId);
490 String lockHolder = null;
491 if (voluntaryRelease) {
492 mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder);
493 logger.info("In unlock: lock voluntarily released for " + lockId);
495 boolean needToSyncQuorum = true;
496 mls = new MusicLockState(MusicLockState.LockStatus.UNLOCKED, lockHolder,
498 logger.info("In unlock: lock forcibly released for " + lockId);
501 getLockingServiceHandle().setLockState(lockName, mls);
502 } catch (MusicLockingException e) {
503 logger.error("Failed to release Lock " + lockName + " " + e);
505 long end = System.currentTimeMillis();
506 logger.info("Time taken to release lock:" + (end - start) + " ms");
514 public static void deleteLock(String lockName) {
515 long start = System.currentTimeMillis();
516 logger.info("Deleting lock for " + lockName);
518 getLockingServiceHandle().deleteLock("/" + lockName);
519 } catch (MusicLockingException e) {
520 logger.error("Failed to Delete Lock " + lockName + " " + e);
522 long end = System.currentTimeMillis();
523 logger.info("Time taken to delete lock:" + (end - start) + " ms");
534 public static TableMetadata returnColumnMetadata(String keyspace, String tablename) {
535 return getDSHandle().returnColumnMetadata(keyspace, tablename);
543 public static void pureZkCreate(String nodeName) {
545 getLockingServiceHandle().getzkLockHandle().createNode(nodeName);
546 } catch (MusicLockingException e) {
547 logger.error("Failed to get ZK Lock Handle " + e);
556 public static void pureZkWrite(String nodeName, byte[] data) {
557 long start = System.currentTimeMillis();
558 logger.info("Performing zookeeper write to " + nodeName);
560 getLockingServiceHandle().getzkLockHandle().setNodeData(nodeName, data);
561 } catch (MusicLockingException e) {
562 logger.error("Failed to get ZK Lock Handle " + e);
564 logger.info("Performed zookeeper write to " + nodeName);
565 long end = System.currentTimeMillis();
566 logger.info("Time taken for the actual zk put:" + (end - start) + " ms");
574 public static byte[] pureZkRead(String nodeName) {
575 long start = System.currentTimeMillis();
578 data = getLockingServiceHandle().getzkLockHandle().getNodeData(nodeName);
579 } catch (MusicLockingException e) {
580 logger.error("Failed to get ZK Lock Handle " + e);
582 long end = System.currentTimeMillis();
583 logger.info("Time taken for the actual zk put:" + (end - start) + " ms");
589 // Prepared Query Additions.
593 * @param keyspaceName
598 * @throws MusicServiceException
600 public static ReturnType eventualPut(PreparedQueryObject queryObject) {
601 boolean result = false;
603 result = getDSHandle().executePut(queryObject, MusicUtil.EVENTUAL);
604 } catch (MusicServiceException | MusicQueryException ex) {
605 logger.error(ex.getMessage() + " " + ex.getCause() + " " + ex);
608 return new ReturnType(ResultType.SUCCESS, "Success");
610 return new ReturnType(ResultType.FAILURE, "Failure");
616 * @param keyspaceName
623 public static ReturnType criticalPut(String keyspaceName, String tableName, String primaryKey,
624 PreparedQueryObject queryObject, String lockId, Condition conditionInfo) {
625 long start = System.currentTimeMillis();
628 MusicLockState mls = getLockingServiceHandle()
629 .getLockState(keyspaceName + "." + tableName + "." + primaryKey);
630 if (mls.getLockHolder().equals(lockId) == true) {
631 if (conditionInfo != null)// check if condition is true
632 if (conditionInfo.testCondition() == false)
633 return new ReturnType(ResultType.FAILURE,
634 "Lock acquired but the condition is not true");
635 getDSHandle().executePut(queryObject, MusicUtil.CRITICAL);
636 long end = System.currentTimeMillis();
637 logger.info("Time taken for the critical put:" + (end - start) + " ms");
638 return new ReturnType(ResultType.SUCCESS, "Update performed");
640 return new ReturnType(ResultType.FAILURE,
641 "Cannot perform operation since you are the not the lock holder");
642 } catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
643 logger.error(e.getMessage());
644 return new ReturnType(ResultType.FAILURE,
645 "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
655 * @return Boolean Indicates success or failure
659 public static boolean nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) {
660 // this is mainly for some functions like keyspace creation etc which does not
661 // really need the bells and whistles of Music locking.
662 boolean result = false;
664 result = getDSHandle().executePut(queryObject, consistency);
665 } catch (MusicQueryException | MusicServiceException ex) {
666 logger.error(ex.getMessage());
672 * This method performs DDL operation on cassandra.
674 * @param queryObject query object containing prepared query and values
677 public static ResultSet get(PreparedQueryObject queryObject) {
678 ResultSet results = null;
680 results = getDSHandle().executeEventualGet(queryObject);
681 } catch (MusicQueryException | MusicServiceException e) {
682 logger.error(e.getMessage());
688 * This method performs DDL operations on cassandra, if the the resource is available. Lock ID
689 * is used to check if the resource is free.
691 * @param keyspaceName name of the keyspace
692 * @param tableName name of the table
693 * @param primaryKey primary key value
694 * @param queryObject query object containing prepared query and values
695 * @param lockId lock ID to check if the resource is free to perform the operation.
698 public static ResultSet criticalGet(String keyspaceName, String tableName, String primaryKey,
699 PreparedQueryObject queryObject, String lockId) throws MusicServiceException {
700 ResultSet results = null;
702 MusicLockState mls = getLockingServiceHandle()
703 .getLockState(keyspaceName + "." + tableName + "." + primaryKey);
704 if (mls.getLockHolder().equals(lockId)) {
705 results = getDSHandle().executeCriticalGet(queryObject);
707 throw new MusicServiceException("YOU DO NOT HAVE THE LOCK");
708 } catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
709 logger.error(e.getMessage());
715 * This method performs DML operation on cassandra, when the lock of the dd is acquired.
717 * @param keyspaceName name of the keyspace
718 * @param tableName name of the table
719 * @param primaryKey primary key value
720 * @param queryObject query object containing prepared query and values
723 public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey,
724 PreparedQueryObject queryObject, Condition conditionInfo) {
725 long start = System.currentTimeMillis();
726 String key = keyspaceName + "." + tableName + "." + primaryKey;
727 String lockId = createLockReference(key);
728 long lockCreationTime = System.currentTimeMillis();
729 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
730 ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod);
731 long lockAcqTime = System.currentTimeMillis();
732 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
733 logger.info("acquired lock with id " + lockId);
734 ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey,
735 queryObject, lockId, conditionInfo);
736 long criticalPutTime = System.currentTimeMillis();
737 boolean voluntaryRelease = true;
739 long lockDeleteTime = System.currentTimeMillis();
740 String timingInfo = "|lock creation time:" + (lockCreationTime - start)
741 + "|lock accquire time:" + (lockAcqTime - lockCreationTime)
742 + "|critical put time:" + (criticalPutTime - lockAcqTime)
743 + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|";
744 criticalPutResult.setTimingInfo(timingInfo);
745 return criticalPutResult;
747 logger.info("unable to acquire lock, id " + lockId);
748 destroyLockRef(lockId);
749 return lockAcqResult;
755 * This method performs DDL operation on cassasndra, when the lock for the resource is acquired.
757 * @param keyspaceName name of the keyspace
758 * @param tableName name of the table
759 * @param primaryKey primary key value
760 * @param queryObject query object containing prepared query and values
762 * @throws MusicServiceException
764 public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey,
765 PreparedQueryObject queryObject) throws MusicServiceException {
766 String key = keyspaceName + "." + tableName + "." + primaryKey;
767 String lockId = createLockReference(key);
768 long leasePeriod = MusicUtil.getDefaultLockLeasePeriod();
769 ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod);
770 if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
771 logger.info("acquired lock with id " + lockId);
773 criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId);
774 boolean voluntaryRelease = true;
775 releaseLock(lockId, voluntaryRelease);
778 logger.info("unable to acquire lock, id " + lockId);
784 * authenticate user logic
795 public static Map<String, Object> autheticateUser(String nameSpace, String userId,
796 String password, String keyspace, String aid, String operation)
798 Map<String, Object> resultMap = new HashMap<>();
800 resultMap = CachingUtil.validateRequest(nameSpace, userId, password, keyspace, aid,
802 if (!resultMap.isEmpty())
804 if (aid == null && (userId == null || password == null)) {
805 logger.error("One or more required headers is missing. userId: " + userId
806 + " :: password: " + password);
807 resultMap.put("Exception",
808 "UserId and Password are mandatory for the operation " + operation);
811 boolean isAAF = CachingUtil.isAAFApplication(nameSpace);
812 if (!isAAF && aid != null && aid.length() > 0) { // Non AAF app
813 resultMap = CachingUtil.authenticateAIDUser(aid, keyspace);
814 if (!resultMap.isEmpty())
817 if (isAAF && nameSpace != null && userId != null && password != null) {
818 boolean isValid = true;
820 isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace);
821 } catch (Exception e) {
822 logger.error("Got exception while AAF authentication for namespace " + nameSpace);
823 resultMap.put("Exception", e.getMessage());
827 logger.error("User not authenticated with AAF.");
828 resultMap.put("Exception", "User not authenticated...");
831 if (!resultMap.isEmpty())
836 if (operation.equals("createKeySpace")) {
837 logger.info("AID is not provided. Creating new UUID for keyspace.");
838 PreparedQueryObject pQuery = new PreparedQueryObject();
839 pQuery.appendQueryString(
840 "select uuid from admin.keyspace_master where application_name=? and username=? and keyspace_name=? allow filtering");
841 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace));
842 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
843 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(),
844 MusicUtil.DEFAULTKEYSPACENAME));
847 Row rs = MusicCore.get(pQuery).one();
848 uuid = rs.getUUID("uuid").toString();
849 resultMap.put("uuid", "existing");
850 } catch (Exception e) {
851 logger.info("No UUID found in DB. So creating new UUID.");
852 uuid = CachingUtil.generateUUID();
853 resultMap.put("uuid", "new");
856 pQuery = new PreparedQueryObject();
857 pQuery.appendQueryString(
858 "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, "
859 + "password, username, is_aaf) values (?,?,?,?,?,?,?)");
860 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid));
861 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace));
862 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), nameSpace));
863 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True"));
864 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), password));
865 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId));
866 pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF));
867 CachingUtil.updateMusicCache(uuid, keyspace);
868 MusicCore.eventualPut(pQuery);
869 resultMap.put("aid", uuid);