Merge "JasonUpdate.java: Fixed sonar issue"
[music.git] / src / main / java / org / onap / music / main / MusicCore.java
index 4929edd..aaaeae3 100644 (file)
@@ -73,7 +73,10 @@ public class MusicCore {
         public boolean testCondition() throws Exception {
             // first generate the row
             ResultSet results = quorumGet(selectQueryForTheRow);
-            Row row = results.one();
+            Row row = null;
+            if(results != null) {
+                row = results.one();
+            }
             return getDSHandle().doesRowSatisfyCondition(row, conditions);
         }
     }
@@ -288,7 +291,8 @@ public class MusicCore {
         MusicLockState newMls = null;
         try {
             currentMls = getMusicLockState(key);
-            String currentLockHolder = currentMls.getLockHolder();
+            String currentLockHolder = null;
+            if(currentMls != null) { currentLockHolder = currentMls.getLockHolder(); };
             if (lockId.equals(currentLockHolder)) {
                 logger.info(EELFLoggerDelegate.applicationLogger,"In acquire lock: You already have the lock!");
                 return new ReturnType(ResultType.SUCCESS, "You already have the lock!");
@@ -518,14 +522,16 @@ public class MusicCore {
     /**
      * 
      * @param lockName
+     * @throws MusicLockingException 
      */
-    public static void deleteLock(String lockName) {
+    public static void deleteLock(String lockName) throws MusicLockingException {
         long start = System.currentTimeMillis();
         logger.info(EELFLoggerDelegate.applicationLogger,"Deleting lock for " + lockName);
         try {
             getLockingServiceHandle().deleteLock("/" + lockName);
         } catch (MusicLockingException e) {
                logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.DELTELOCK+lockName  ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+               throw new MusicLockingException(e.getMessage());
         }
         long end = System.currentTimeMillis();
         logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to delete lock:" + (end - start) + " ms");
@@ -706,6 +712,22 @@ public class MusicCore {
         }
         return results;
     }
+    
+    public static String getMyHostId() {
+       PreparedQueryObject pQuery = new PreparedQueryObject();
+       pQuery.appendQueryString("SELECT HOST_ID FROM SYSTEM.LOCAL");
+               ResultSet rs = null;
+               try {
+                       rs = getDSHandle().executeEventualGet(pQuery);
+                       Row row = rs.one();
+                       return (row == null) ? "UNKNOWN" : row.getUUID("HOST_ID").toString();
+               } catch (Exception e) {
+                       e.printStackTrace();
+            logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
+        }
+               logger.error(EELFLoggerDelegate.errorLogger, "Some issue during MusicCore.getMyHostId");
+               return "UNKNOW";
+       }
 
     /**
      * This method performs DDL operations on cassandra, if the the resource is available. Lock ID
@@ -890,24 +912,20 @@ public class MusicCore {
                         operation);
         if (!resultMap.isEmpty())
             return resultMap;
-        String isAAFApp = CachingUtil.isAAFApplication(nameSpace);
+        String isAAFApp = null;
+        try {
+            isAAFApp= CachingUtil.isAAFApplication(nameSpace);
+        } catch(MusicServiceException e) {
+           resultMap.put("Exception", e.getMessage());
+           return resultMap;
+        }
         if(isAAFApp == null) {
             resultMap.put("Exception", "Namespace: "+nameSpace+" doesn't exist. Please make sure ns(appName)"
                     + " is correct and Application is onboarded.");
             return resultMap;
         }
         boolean isAAF = Boolean.valueOf(isAAFApp);
-        if (!isAAF && !(operation.equals("createKeySpace"))) {
-               if(aid == null) {
-                       resultMap.put("Exception", "Aid is mandatory for nonAAF applications ");
-                       return resultMap;
-               }
-            resultMap = CachingUtil.authenticateAIDUser(aid, keyspace);
-            
-            if (!resultMap.isEmpty())
-                return resultMap;
-        }
-        if (aid == null && (userId == null || password == null)) {
+        if (userId == null || password == null) {
                logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO  ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR);
             logger.error(EELFLoggerDelegate.errorLogger,"One or more required headers is missing. userId: " + userId
                             + " :: password: " + password);
@@ -915,7 +933,12 @@ public class MusicCore {
                             "UserId and Password are mandatory for the operation " + operation);
             return resultMap;
         }
-        
+        if(!isAAF && !(operation.equals("createKeySpace"))) {
+            resultMap = CachingUtil.authenticateAIDUser(nameSpace, userId, password, keyspace);
+            if (!resultMap.isEmpty())
+                return resultMap;
+            
+        }
         if (isAAF && nameSpace != null && userId != null && password != null) {
             boolean isValid = true;
             try {