Merge "Update Netty Library version"
[music.git] / src / main / java / org / onap / music / service / impl / MusicCassaCore.java
index cf6f5ed..0786457 100644 (file)
@@ -29,7 +29,6 @@ import java.io.StringWriter;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
-import java.util.concurrent.TimeUnit;
 
 import org.onap.music.datastore.MusicDataStore;
 import org.onap.music.datastore.MusicDataStoreHandle;
@@ -51,7 +50,6 @@ import org.onap.music.main.ResultType;
 import org.onap.music.main.ReturnType;
 import org.onap.music.service.MusicCoreService;
 
-import com.att.eelf.configuration.EELFLogger;
 import com.datastax.driver.core.DataType;
 import com.datastax.driver.core.ResultSet;
 import com.datastax.driver.core.Row;
@@ -61,14 +59,22 @@ import org.onap.music.datastore.*;
 
 public class MusicCassaCore implements MusicCoreService {
 
-    public static CassaLockStore mLockHandle = null;;
+    private static CassaLockStore mLockHandle = null;
     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicCassaCore.class);
-    private static boolean unitTestRun=true;
     private static MusicCassaCore musicCassaCoreInstance = null;
 
     private MusicCassaCore() {
+        // not going to happen
+    }
+    
+    public static CassaLockStore getmLockHandle() {
+        return mLockHandle;
+    }
 
+    public static void setmLockHandle(CassaLockStore mLockHandle) {
+        MusicCassaCore.mLockHandle = mLockHandle;
     }
+    
     public static MusicCassaCore getInstance() {
 
         if(musicCassaCoreInstance == null) {
@@ -77,6 +83,9 @@ public class MusicCassaCore implements MusicCoreService {
         return musicCassaCoreInstance;
     }
 
+
+
+
     public static CassaLockStore getLockingServiceHandle() throws MusicLockingException {
         logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring lock store handle");
         long start = System.currentTimeMillis();
@@ -95,7 +104,7 @@ public class MusicCassaCore implements MusicCoreService {
     }
 
     public String createLockReference(String fullyQualifiedKey) throws MusicLockingException {
-       return createLockReference(fullyQualifiedKey, LockType.WRITE);
+        return createLockReference(fullyQualifiedKey, LockType.WRITE);
     }
 
     public String createLockReference(String fullyQualifiedKey, LockType locktype) throws MusicLockingException {
@@ -125,8 +134,8 @@ public class MusicCassaCore implements MusicCoreService {
 
     public ReturnType acquireLockWithLease(String fullyQualifiedKey, String lockReference, long leasePeriod)
             throws MusicLockingException, MusicQueryException, MusicServiceException  {
-         evictExpiredLockHolder(fullyQualifiedKey,leasePeriod);
-         return acquireLock(fullyQualifiedKey, lockReference);
+        evictExpiredLockHolder(fullyQualifiedKey,leasePeriod);
+        return acquireLock(fullyQualifiedKey, lockReference);
     }
 
     private void evictExpiredLockHolder(String fullyQualifiedKey, long leasePeriod)
@@ -167,7 +176,7 @@ public class MusicCassaCore implements MusicCoreService {
         if (!lockInfo.getIsLockOwner()) {
             return new ReturnType(ResultType.FAILURE, lockId + " is not a lock holder");//not top of the lock store q
         }
-   
+        
         //check to see if the value of the key has to be synced in case there was a forceful release
         String syncTable = keyspace+".unsyncedKeys_"+table;
         String query = "select * from "+syncTable+" where key='"+localFullyQualifiedKey+"';";
@@ -286,7 +295,7 @@ public class MusicCassaCore implements MusicCoreService {
         try {
             LockObject lockOwner = getLockingServiceHandle().peekLockQueue(keyspace, table, primaryKeyValue);
             if (!lockOwner.getIsLockOwner()) {
-                return "No lock holder!";
+                return null;
             }
             return "$" + fullyQualifiedKey + "$" + lockOwner.getLockRef();
         } catch (MusicLockingException | MusicServiceException | MusicQueryException e) {
@@ -498,14 +507,22 @@ public class MusicCassaCore implements MusicCoreService {
             PreparedQueryObject queryObject, String lockId, Condition conditionInfo) {
         long start = System.currentTimeMillis();
         try {
+            String keyLock = lockId.substring(lockId.lastIndexOf(".") + 1,lockId.lastIndexOf("$"));
+            if (lockId.contains(".") && !keyLock.equals(primaryKeyValue)) {
+                return new ReturnType(ResultType.FAILURE,"Lock value '" + keyLock + "' and key value '"
+                + primaryKeyValue + "' not match. Please check your values: " 
+                + lockId + " .");
+            }
             LockObject lockObject = getLockingServiceHandle().getLockInfo(keyspace, table, primaryKeyValue,
                     lockId.substring(lockId.lastIndexOf("$") + 1));
 
-            if (!lockObject.getIsLockOwner()) {
+            if ( lockObject == null ) {
+                return new ReturnType(ResultType.FAILURE, lockId + " does not exist.");
+            } else if (!lockObject.getIsLockOwner()) {
                 return new ReturnType(ResultType.FAILURE, lockId + " is not the lock holder");
             } else if (lockObject.getLocktype() != LockType.WRITE) {
                 return new ReturnType(ResultType.FAILURE,
-                        "Attempting to do write operation, but " + lockId + " is a write lock");
+                        "Attempting to do write operation, but " + lockId + " is a read lock");
             }
 
             if (conditionInfo != null) {
@@ -536,10 +553,10 @@ public class MusicCassaCore implements MusicCoreService {
             dsHandle.executePut(queryObject, MusicUtil.CRITICAL);
             long end = System.currentTimeMillis();
             logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms");
-        }catch (MusicQueryException | MusicServiceException | MusicLockingException  e) {
+        } catch (MusicQueryException | MusicServiceException | MusicLockingException  e) {
             logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
             return new ReturnType(ResultType.FAILURE,
-                "Exception thrown while doing the critical put\n"
+                "Exception thrown while doing the critical put"
                 + e.getMessage());
         }
         return new ReturnType(ResultType.SUCCESS, "Update performed");
@@ -555,17 +572,17 @@ public class MusicCassaCore implements MusicCoreService {
      *
      *
      */
-    public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException {
+    public ResultType nonKeyRelatedPut(PreparedQueryObject queryObject, String consistency) throws MusicServiceException,MusicQueryException {
         // this is mainly for some functions like keyspace creation etc which does not
         // really need the bells and whistles of Music locking.
         boolean result = false;
-        try {
-            result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency);
-        } catch (MusicQueryException | MusicServiceException ex) {
-            logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR,
-                    ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR, ex);
-            throw new MusicServiceException(ex.getMessage());
-        }
+//        try {
+        result = MusicDataStoreHandle.getDSHandle().executePut(queryObject, consistency);
+//        } catch (MusicQueryException | MusicServiceException ex) {
+            // logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(), AppMessages.UNKNOWNERROR,
+            //     ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR, ex);
+//            throw new MusicServiceException(ex.getMessage(),ex);
+//        }
         return result ? ResultType.SUCCESS : ResultType.FAILURE;
     }
 
@@ -601,17 +618,33 @@ public class MusicCassaCore implements MusicCoreService {
     public ResultSet criticalGet(String keyspace, String table, String primaryKeyValue,
                     PreparedQueryObject queryObject, String lockId) throws MusicServiceException {
         ResultSet results = null;
-
+        String keyLock = lockId.substring(lockId.lastIndexOf(".") + 1,lockId.lastIndexOf("$"));
         try {
+            if (lockId.contains(".") && !keyLock.equals(primaryKeyValue)) {
+                throw new MusicLockingException("Lock value '" + keyLock + "' and key value '"
+                + primaryKeyValue + "' do not match. Please check your values: " 
+                + lockId + " .");
+            }
             LockObject lockObject = getLockingServiceHandle().getLockInfo(keyspace, table, primaryKeyValue,
-                    lockId.substring(lockId.lastIndexOf("$") + 1));
-            if (!lockObject.getIsLockOwner()) {
+                lockId.substring(lockId.lastIndexOf("$") + 1));
+            if (null == lockObject) {
+                throw new MusicLockingException("No Lock Object. Please check if lock name or key is correct." 
+                    + lockId + " .");
+            }
+            if ( !lockObject.getIsLockOwner()) {
                 return null;// not top of the lock store q
             }
             results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryObject);
-        } catch (MusicQueryException | MusicServiceException | MusicLockingException e) {
-                logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR  ,ErrorSeverity
+        } catch ( MusicLockingException e ) {
+            logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR  ,ErrorSeverity
+                    .WARN, ErrorTypes.MUSICSERVICEERROR);
+                throw new MusicServiceException(
+                    "Cannot perform critical get for key: " + primaryKeyValue + " : " + e.getMessage());
+        } catch (MusicQueryException | MusicServiceException e) {
+            logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR  ,ErrorSeverity
                     .WARN, ErrorTypes.MUSICSERVICEERROR, e);
+                throw new MusicServiceException(
+                    "Cannot perform critical get for key: " + primaryKeyValue + " : " + e.getMessage());    
         }
         return results;
     }
@@ -781,4 +814,5 @@ public class MusicCassaCore implements MusicCoreService {
         return null;
     }
 
+
 }