Update junit test
[music.git] / src / main / java / org / onap / music / authentication / CachingUtil.java
index 534038e..dba2319 100755 (executable)
@@ -28,13 +28,16 @@ package org.onap.music.authentication;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import javax.ws.rs.core.MediaType;
 
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.jcs.JCS;
 import org.apache.commons.jcs.access.CacheAccess;
+import org.apache.commons.jcs.engine.CompositeCacheAttributes;
+import org.apache.commons.jcs.engine.ElementAttributes;
+import org.apache.commons.jcs.engine.behavior.ICompositeCacheAttributes;
+import org.apache.commons.jcs.engine.behavior.IElementAttributes;
 import org.mindrot.jbcrypt.BCrypt;
 import org.onap.music.authentication.MusicAuthenticator.Operation;
 import org.onap.music.datastore.PreparedQueryObject;
@@ -46,7 +49,6 @@ import org.onap.music.exceptions.MusicServiceException;
 import org.onap.music.main.MusicCore;
 import org.onap.music.main.MusicUtil;
 import com.datastax.driver.core.DataType;
-import com.datastax.driver.core.PreparedStatement;
 import com.datastax.driver.core.ResultSet;
 import com.datastax.driver.core.Row;
 import com.datastax.driver.core.exceptions.InvalidQueryException;
@@ -64,14 +66,14 @@ public class CachingUtil implements Runnable {
 
     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CachingUtil.class);
 
+    /** keyspace & ns */
     private static CacheAccess<String, String> musicCache = JCS.getInstance("musicCache");
-    private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache");
+    /** cache to hold isaaf application */
     private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache");
+    /** hold user creds for namespace */
     private static CacheAccess<String, Map<String, String>> musicValidateCache = JCS.getInstance("musicValidateCache");
-    private static CacheAccess<String, List<String>> callbackNotifyList = JCS.getInstance("eternalCache");
     private static Map<String, Number> userAttempts = new HashMap<>();
     private static Map<String, Calendar> lastFailedTime = new HashMap<>();
-    private static CacheAccess<String, PreparedStatement> queryBank = JCS.getInstance("statementBank");
     private static CacheAccess<String, String> adminUserCache = JCS.getInstance("adminUserCache");
     
     public static CacheAccess<String, String> getAdminUserCache() {
@@ -82,42 +84,9 @@ public class CachingUtil implements Runnable {
         adminUserCache.put(authorization,userId);
     }
     
-    
-    public static  void updateStatementBank(String query,PreparedStatement statement) {
-        queryBank.put(query, statement);
-    }
-    
-    public static void resetStatementBank() {
-        queryBank.clear();
-    }
-    
-     public static CacheAccess<String, PreparedStatement> getStatementBank() {
-            return queryBank;
-        }
-    
     private static final String USERNAME="username";
     private static final String PASSWORD="password";
 
-   
-    public boolean isCacheRefreshNeeded() {
-        if (aafCache.get("initBlankMap") == null)
-            return true;
-        return false;
-    }
-    
-    public static void updateCallbackNotifyList(List<String> notifyList) {
-        logger.info("callbackNotifyList: updating cache.....");
-        callbackNotifyList.put("callbackNotify", notifyList);
-    }
-    
-    public static List<String> getCallbackNotifyList() {
-        return callbackNotifyList.get("callbackNotify");
-    }
-    
-    public void initializeMusicCache() {
-        logger.info(EELFLoggerDelegate.applicationLogger,"Initializing Music Cache...");
-        musicCache.put("isInitialized", "true");
-    }
 
     public void initializeAafCache() throws MusicServiceException {
         logger.info(EELFLoggerDelegate.applicationLogger,"Resetting and initializing AAF Cache...");
@@ -128,7 +97,8 @@ public class CachingUtil implements Runnable {
         try {
             pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), false));
         } catch (Exception e1) {
-            logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
+            logger.error(EELFLoggerDelegate.errorLogger, e1.getMessage(),AppMessages.CACHEERROR, 
+                ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR, e1);
         }
         ResultSet rs = MusicCore.get(pQuery);
         Iterator<Row> it = rs.iterator();
@@ -145,13 +115,14 @@ public class CachingUtil implements Runnable {
                 if (responseObj) {
                     map = new HashMap<>();
                     map.put(userId, password);
-                    aafCache.put(nameSpace, map);
+                    musicValidateCache.put(nameSpace, map);
                     musicCache.put(keySpace, nameSpace);
                     logger.debug("Cronjob: Cache Updated with AAF response for namespace "
                                     + nameSpace);
                 }
             } catch (Exception e) {
-                logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
+                logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity
+                    .INFO, ErrorTypes.GENERALSERVICEERROR, e);
                 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),"Something at AAF was changed for ns: " + nameSpace+" So not updating Cache for the namespace. ");
             }
         }
@@ -164,17 +135,18 @@ public class CachingUtil implements Runnable {
         try {
             initializeAafCache();
         } catch (MusicServiceException e) {
-            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO, ErrorTypes.GENERALSERVICEERROR);
+            logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.INFO,
+                ErrorTypes.GENERALSERVICEERROR, e);
         }
     }
 
     public static boolean authenticateAAFUser(String nameSpace, String userId, String password,
                     String keySpace) throws Exception {
 
-        if (aafCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) {
+        if (musicValidateCache.get(nameSpace) != null && musicCache.get(keySpace)!=null) {
             if (keySpace != null && !musicCache.get(keySpace).equals(nameSpace)) {
                 logger.info(EELFLoggerDelegate.applicationLogger,"Create new application for the same namespace.");
-            } else if (aafCache.get(nameSpace).get(userId).equals(password)) {
+            } else if (musicValidateCache.get(nameSpace).get(userId).equals(password)) {
                 logger.info(EELFLoggerDelegate.applicationLogger,"Authenticated with cache value..");
                 // reset invalid attempts to 0
                 userAttempts.put(nameSpace, 0);
@@ -206,7 +178,7 @@ public class CachingUtil implements Runnable {
         try {
             responseObj = triggerAAF(nameSpace, userId, password);
         }catch (Exception ex) {
-            logger.info("Exception while trigger aaf");
+            logger.info("Exception while trigger aaf", ex);
             logger.info("Exception: " + ex.getMessage());
             throw new Exception("Exception raised while triggering AAF authentication" +ex.getMessage());
         }
@@ -214,7 +186,7 @@ public class CachingUtil implements Runnable {
             logger.info(EELFLoggerDelegate.applicationLogger,"Valid user. Cache is updated for "+nameSpace);
                 Map<String, String> map = new HashMap<>();
                 map.put(userId, password);
-                aafCache.put(nameSpace, map);
+                musicValidateCache.put(nameSpace, map);
                 musicCache.put(keySpace, nameSpace);
                 return true;
         }
@@ -230,8 +202,6 @@ public class CachingUtil implements Runnable {
             throw new Exception("AAF endpoint is not set. Please specify in the properties file.");
         }
         Client client = Client.create();
-        // WebResource webResource =
-        // client.resource("https://aaftest.test.att.com:8095/proxy/authz/nss/"+nameSpace);
         WebResource webResource = client.resource(MusicUtil.getAafEndpointUrl().concat(nameSpace));
         String plainCreds = userId + ":" + password;
         byte[] plainCredsBytes = plainCreds.getBytes();
@@ -302,7 +272,8 @@ public class CachingUtil implements Runnable {
             try {
                 rs = MusicCore.get(pQuery).one();
             } catch(InvalidQueryException e) {
-                logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage());
+                logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+ e.getMessage
+                    (), e);
                 throw new MusicServiceException("Please make sure admin.keyspace_master table is configured.");
             }
             try {
@@ -310,7 +281,8 @@ public class CachingUtil implements Runnable {
                 if(isAAF != null)
                     appNameCache.put(namespace, isAAF);
             } catch (Exception e) {
-                logger.error(EELFLoggerDelegate.errorLogger,  e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+                logger.error(EELFLoggerDelegate.errorLogger,  e.getMessage(), AppMessages.QUERYERROR,ErrorSeverity
+                    .ERROR, ErrorTypes.QUERYERROR, e);
             }
         }
         return isAAF;
@@ -327,7 +299,8 @@ public class CachingUtil implements Runnable {
             try {
                 uuid = rs.getUUID("uuid").toString();
             } catch (Exception e) {
-                logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred during uuid retrieval from DB."+e.getMessage());
+                logger.error(EELFLoggerDelegate.errorLogger,"Exception occurred during uuid retrieval from DB."+e
+                    .getMessage(), e);
             }
         }
         return uuid;
@@ -343,7 +316,8 @@ public class CachingUtil implements Runnable {
         try {
             appName = rs.getString("application_name");
         } catch (Exception e) {
-            logger.error(EELFLoggerDelegate.errorLogger,  e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity.ERROR, ErrorTypes.QUERYERROR);
+            logger.error(EELFLoggerDelegate.errorLogger,  e.getMessage(), AppMessages.QUERYERROR, ErrorSeverity
+                .ERROR, ErrorTypes.QUERYERROR, e);
         }
         return appName;
     }
@@ -395,11 +369,11 @@ public class CachingUtil implements Runnable {
             rs = MusicCore.get(queryObject).one();
         } catch (MusicServiceException e) {
             String errorMsg = "Unable to process operation. Error is "+e.getMessage();
-            logger.error(EELFLoggerDelegate.errorLogger, errorMsg);
+            logger.error(EELFLoggerDelegate.errorLogger, errorMsg, e);
             resultMap.put("Exception", errorMsg);
             return resultMap;
         } catch (InvalidQueryException e) {
-            logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage());
+            logger.error(EELFLoggerDelegate.errorLogger,"Exception admin keyspace not configured."+e.getMessage(), e);
             resultMap.put("Exception", "Please make sure admin.keyspace_master table is configured.");
             return resultMap;
         }
@@ -418,7 +392,7 @@ public class CachingUtil implements Runnable {
     }
 
     public static Map<String, Object> authenticateAIDUser(String nameSpace, String userId, String password,
-           String keyspace) {
+            String keyspace) {
         Map<String, Object> resultMap = new HashMap<>();
         String pwd = null;
         if((musicCache.get(keyspace) != null) && (musicValidateCache.get(nameSpace) != null) 
@@ -439,14 +413,14 @@ public class CachingUtil implements Runnable {
         try {
             queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspace));
         } catch (Exception e) {
-            logger.error(EELFLoggerDelegate.errorLogger,"Adding value to query object failed: " + e.getMessage());
+            logger.error(EELFLoggerDelegate.errorLogger,"Adding value to query object failed: " + e.getMessage(), e);
         }
         Row rs = null;
         try {
             rs = MusicCore.get(queryObject).one();
         } catch (MusicServiceException e) {
             String errMsg = "Unable to process operation. Error is "+e.getMessage();
-            logger.error(EELFLoggerDelegate.errorLogger, errMsg);
+            logger.error(EELFLoggerDelegate.errorLogger, errMsg, e);
             resultMap.put("Exception", errMsg);
             return resultMap;
         }
@@ -475,16 +449,4 @@ public class CachingUtil implements Runnable {
         CachingUtil.updateMusicValidateCache(nameSpace, userId, pwd);
         return resultMap;
     }
-
-    public static void deleteKeysFromDB(String deleteKeys) {
-        PreparedQueryObject pQuery = new PreparedQueryObject();
-        pQuery.appendQueryString(
-                        "DELETE FROM admin.locks WHERE lock_id IN ("+deleteKeys+")");
-        try {
-            MusicCore.nonKeyRelatedPut(pQuery, "eventual");
-        } catch (Exception e) {
-            logger.error(EELFLoggerDelegate.errorLogger,  e.getMessage(), "Deleting keys from "
-                + "DB failed.");
-        }
-    }
 }