Some Sonar and CLM issues resolved.
[music.git] / src / main / java / org / onap / music / main / MusicCore.java
index 3d5cf4e..0306560 100644 (file)
@@ -31,7 +31,6 @@ import org.onap.music.datastore.MusicDataStore;
 import org.onap.music.datastore.PreparedQueryObject;
 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
 import org.onap.music.eelf.logging.EELFLoggerDelegate;
-// import org.onap.music.eelf.logging.EELFLoggerDelegate;
 import org.onap.music.exceptions.MusicLockingException;
 import org.onap.music.exceptions.MusicQueryException;
 import org.onap.music.exceptions.MusicServiceException;
@@ -66,7 +65,7 @@ public class MusicCore {
             this.selectQueryForTheRow = selectQueryForTheRow;
         }
 
-        public boolean testCondition() {
+        public boolean testCondition() throws Exception {
             // first generate the row
             ResultSet results = quorumGet(selectQueryForTheRow);
             Row row = results.one();
@@ -111,13 +110,20 @@ public class MusicCore {
     /**
      * 
      * @return
+     * @throws MusicServiceException 
      */
-    public static MusicDataStore getDSHandle() {
+    public static MusicDataStore getDSHandle() throws MusicServiceException {
         logger.info(EELFLoggerDelegate.applicationLogger,"Acquiring data store handle");
         long start = System.currentTimeMillis();
         if (mDstoreHandle == null) {
             mDstoreHandle = new MusicDataStore();
         }
+        if(mDstoreHandle.getSession() == null) {
+               String message = "Connection to Cassandra has not been enstablished."
+                               + " Please check connection properites and reboot.";
+               logger.info(EELFLoggerDelegate.applicationLogger, message);
+            throw new MusicServiceException(message);
+        }
         long end = System.currentTimeMillis();
         logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to acquire data store handle:" + (end - start) + " ms");
         return mDstoreHandle;
@@ -202,6 +208,10 @@ public class MusicCore {
              */
             if (acquireLock(key, lockId).getResult() == ResultType.SUCCESS) {
                 mls = getMusicLockState(key);// get latest state
+                if ( mls == null ) {
+                    logger.info(EELFLoggerDelegate.applicationLogger,"Music Lock State is null");
+                    return new ReturnType(ResultType.FAILURE, "Could not acquire lock, Lock State is null");                    
+                }
                 if (mls.getLeaseStartTime() == -1) {// set it again only if it is not set already
                     mls.setLeaseStartTime(System.currentTimeMillis());
                     mls.setLeasePeriod(leasePeriod);
@@ -236,7 +246,7 @@ public class MusicCore {
         } catch (MusicLockingException e2) {
             logger.error(EELFLoggerDelegate.errorLogger,"Failed to aquireLock lockId " + lockId + " " + e2);
         }
-        if (result == false) {
+        if (!result) {
             logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Not your turn, someone else has the lock");
             try {
                                if (!getLockingServiceHandle().lockIdExists(lockId)) {
@@ -253,7 +263,7 @@ public class MusicCore {
 
         // this is for backward compatibility where locks could also be acquired on just
         // keyspaces or tables.
-        if (isTableOrKeySpaceLock(key) == true) {
+        if (isTableOrKeySpaceLock(key)) {
             logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: A table or keyspace lock so no need to perform sync...so returning true");
             return new ReturnType(ResultType.SUCCESS, "A table or keyspace lock so no need to perform sync...so returning true");
         }
@@ -293,7 +303,11 @@ public class MusicCore {
         // do syncing if this was a forced lock release
         if (needToSyncQuorum) {
             logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: Since there was a forcible release, need to sync quorum!");
-            syncQuorum(key);
+            try {
+              syncQuorum(key);
+            } catch (Exception e) {
+              logger.error(EELFLoggerDelegate.errorLogger,"Failed to set Lock state " + e);
+            }
         }
 
         // change status to locked
@@ -325,7 +339,7 @@ public class MusicCore {
     }
 
 
-    private static void syncQuorum(String key) {
+    private static void syncQuorum(String key) throws Exception {
         logger.info(EELFLoggerDelegate.applicationLogger,"Performing sync operation---");
         String[] splitString = key.split("\\.");
         String keyspaceName = splitString[0];
@@ -346,8 +360,6 @@ public class MusicCore {
         selectQuery.appendQueryString("SELECT *  FROM " + keyspaceName + "." + tableName + " WHERE "
                         + primaryKeyName + "= ?" + ";");
         selectQuery.addValue(cqlFormattedPrimaryKeyValue);
-        // String selectQuery = "SELECT * FROM "+keyspaceName+"."+tableName+ " WHERE
-        // "+primaryKeyName+"="+cqlFormattedPrimaryKeyValue+";";
         ResultSet results = null;
         try {
             results = getDSHandle().executeCriticalGet(selectQuery);
@@ -356,7 +368,6 @@ public class MusicCore {
             ColumnDefinitions colInfo = row.getColumnDefinitions();
             int totalColumns = colInfo.size();
             int counter = 1;
-            // String fieldValueString="";
             StringBuilder fieldValueString = new StringBuilder("");
             for (Definition definition : colInfo) {
                 String colName = definition.getName();
@@ -365,7 +376,6 @@ public class MusicCore {
                 DataType colType = definition.getType();
                 Object valueObj = getDSHandle().getColValue(row, colName, colType);
                 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
-                // fieldValueString = fieldValueString+ colName+"="+valueString;
                 fieldValueString.append(colName + " = ?");
                 updateQuery.addValue(valueString);
                 if (counter != (totalColumns - 1))
@@ -375,8 +385,6 @@ public class MusicCore {
             updateQuery.appendQueryString("UPDATE " + keyspaceName + "." + tableName + " SET "
                             + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
             updateQuery.addValue(cqlFormattedPrimaryKeyValue);
-            // String updateQuery = "UPDATE "+keyspaceName+"."+tableName+" SET "+fieldValueString+"
-            // WHERE "+primaryKeyName+"="+cqlFormattedPrimaryKeyValue+";";
 
             getDSHandle().executePut(updateQuery, "critical");
         } catch (MusicServiceException | MusicQueryException e) {
@@ -407,8 +415,9 @@ public class MusicCore {
      * 
      * @param results
      * @return
+     * @throws MusicServiceException 
      */
-    public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) {
+    public static Map<String, HashMap<String, Object>> marshallResults(ResultSet results) throws MusicServiceException {
         return getDSHandle().marshalData(results);
     }
 
@@ -506,8 +515,9 @@ public class MusicCore {
      * @param keyspace
      * @param tablename
      * @return
+     * @throws MusicServiceException 
      */
-    public static TableMetadata returnColumnMetadata(String keyspace, String tablename) {
+    public static TableMetadata returnColumnMetadata(String keyspace, String tablename) throws MusicServiceException {
         return getDSHandle().returnColumnMetadata(keyspace, tablename);
     }
 
@@ -604,10 +614,16 @@ public class MusicCore {
             MusicLockState mls = getLockingServiceHandle()
                             .getLockState(keyspaceName + "." + tableName + "." + primaryKey);
             if (mls.getLockHolder().equals(lockId) == true) {
-                if (conditionInfo != null)// check if condition is true
+                if (conditionInfo != null)
+                  try {
                     if (conditionInfo.testCondition() == false)
                         return new ReturnType(ResultType.FAILURE,
                                         "Lock acquired but the condition is not true");
+                  } catch (Exception e) {
+                    return new ReturnType(ResultType.FAILURE,
+                            "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
+                                            + e.getMessage());
+                  }
                 getDSHandle().executePut(queryObject, MusicUtil.CRITICAL);
                 long end = System.currentTimeMillis();
                 logger.info(EELFLoggerDelegate.applicationLogger,"Time taken for the critical put:" + (end - start) + " ms");