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.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;
/**
* All Caching related logic is handled by this class and a schedule cron runs to update cache.
- *
+ *
* @author Vikram
*
*/
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() {
return adminUserCache;
}
-
+
public static void updateAdminUserCache(String authorization,String userId) {
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...");
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();
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. ");
}
}
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);
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());
}
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;
}
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();
response.bufferEntity();
String x = response.getEntity(String.class);
AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class);*/
-
+
return true;
}
public static void updateCadiCache(String user, String keyspace) {
musicCache.put(user, keyspace);
}
-
+
public static String getKSFromCadiCache(String user) {
return musicCache.get(user);
}
-
+
public static void updateMusicValidateCache(String nameSpace, String userId, String password) {
logger.info(EELFLoggerDelegate.applicationLogger,"Updating musicCache for nameSpacce " + nameSpace + " with userId " + userId);
Map<String, String> map = new HashMap<>();
map.put(userId, password);
musicValidateCache.put(nameSpace, map);
}
-
+
public static void updateisAAFCache(String namespace, String isAAF) {
appNameCache.put(namespace, isAAF);
}
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 {
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;
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;
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;
}
}
return resultMap;
}
-
+
public static Map<String, Object> verifyOnboarding(String ns, String userId, String password) {
Map<String, Object> resultMap = new HashMap<>();
if (ns == null || userId == null || password == null) {
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;
}
String keyspace) {
Map<String, Object> resultMap = new HashMap<>();
String pwd = null;
- if((musicCache.get(keyspace) != null) && (musicValidateCache.get(nameSpace) != null)
+ if((musicCache.get(keyspace) != null) && (musicValidateCache.get(nameSpace) != null)
&& (musicValidateCache.get(nameSpace).containsKey(userId))) {
if(!musicCache.get(keyspace).equals(nameSpace)) {
resultMap.put("Exception", "Namespace and keyspace doesn't match");
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;
}
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.");
- }
- }
}