diff options
Diffstat (limited to 'src/main/java')
8 files changed, 278 insertions, 104 deletions
diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java index 6ced1e48..e1ae5b08 100644 --- a/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -45,6 +45,7 @@ import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; import com.datastax.driver.core.Session; import com.datastax.driver.core.TableMetadata; +import com.datastax.driver.core.exceptions.AlreadyExistsException; import com.datastax.driver.core.exceptions.NoHostAvailableException; /** @@ -322,7 +323,12 @@ public class MusicDataStore { ResultSet rs = session.execute(preparedInsert.bind(queryObject.getValues().toArray())); result = rs.wasApplied(); - } catch (Exception e) { + } + catch (AlreadyExistsException ae) { + logger.error(EELFLoggerDelegate.errorLogger, "Executing Session Failure for Request = " + + "[" + queryObject.getQuery() + "]" + " Reason = " + ae.getMessage()); + } + catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, "Executing Session Failure for Request = " + "[" + queryObject.getQuery() + "]" + " Reason = " + e.getMessage()); throw new MusicServiceException("Executing Session Failure for Request = " + "[" diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index cd1d565d..4b2b4824 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -57,6 +57,7 @@ public class CachingUtil implements Runnable { private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CachingUtil.class); private static CacheAccess<String, String> musicCache = JCS.getInstance("musicCache"); + private static CacheAccess<String, String> musicLockCache = JCS.getInstance("musicLockCache"); private static CacheAccess<String, Map<String, String>> aafCache = JCS.getInstance("aafCache"); private static CacheAccess<String, String> appNameCache = JCS.getInstance("appNameCache"); private static Map<String, Number> userAttempts = new HashMap<>(); @@ -76,7 +77,7 @@ public class CachingUtil implements Runnable { public void initializeAafCache() throws MusicServiceException { logger.info(EELFLoggerDelegate.applicationLogger,"Resetting and initializing AAF Cache..."); - String query = "SELECT application_name, keyspace_name, username, password FROM admin.keyspace_master WHERE is_api = ? allow filtering"; + String query = "SELECT uuid, application_name, keyspace_name, username, password FROM admin.keyspace_master WHERE is_api = ? allow filtering"; PreparedQueryObject pQuery = new PreparedQueryObject(); pQuery.appendQueryString(query); try { @@ -94,6 +95,7 @@ public class CachingUtil implements Runnable { String userId = row.getString("username"); String password = row.getString("password"); String keySpace = row.getString("application_name"); + String uuid = row.getUUID("uuid").toString(); try { userAttempts.put(nameSpace, 0); AAFResponse responseObj = triggerAAF(nameSpace, userId, password); @@ -102,6 +104,7 @@ public class CachingUtil implements Runnable { map.put(userId, password); aafCache.put(nameSpace, map); musicCache.put(nameSpace, keySpace); + musicLockCache.put(nameSpace, uuid); logger.debug("Cronjob: Cache Updated with AAF response for namespace " + nameSpace); } @@ -130,7 +133,7 @@ public class CachingUtil implements Runnable { String keySpace) throws Exception { if (aafCache.get(nameSpace) != null) { - if (!musicCache.get(nameSpace).equals(keySpace)) { + if (keySpace != null && !musicCache.get(nameSpace).equals(keySpace)) { logger.debug("Create new application for the same namespace."); } else if (aafCache.get(nameSpace).get(userId).equals(password)) { logger.debug("Authenticated with cache value.."); @@ -161,9 +164,12 @@ public class CachingUtil implements Runnable { AAFResponse responseObj = triggerAAF(nameSpace, userId, password); if (responseObj.getNs().size() > 0) { - if (responseObj.getNs().get(0).getAdmin().contains(userId)) - return true; - + if (responseObj.getNs().get(0).getAdmin().contains(userId)) { + //Map<String, String> map = new HashMap<>(); + //map.put(userId, password); + //aafCache.put(nameSpace, map); + return true; + } } logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated"); return false; @@ -250,6 +256,43 @@ public class CachingUtil implements Runnable { resultMap.put("aid", uuid); return resultMap; } + + + public static Map<String, Object> authenticateAIDUserLock(String aid, String nameSpace) + throws Exception { + Map<String, Object> resultMap = new HashMap<>(); + String uuid = null; + + if (musicLockCache.get(nameSpace) == null) { + PreparedQueryObject pQuery = new PreparedQueryObject(); + pQuery.appendQueryString( + "SELECT uuid from admin.keyspace_master where application_name = '" + + nameSpace + "' allow filtering"); + Row rs = MusicCore.get(pQuery).one(); + try { + uuid = rs.getUUID("uuid").toString(); + musicLockCache.put(nameSpace, uuid); + } catch (Exception e) { + logger.error("Exception occured during uuid retrieval from DB." + e.getMessage()); + resultMap.put("Exception", "Unauthorized operation. Check AID and Namespace. "); + return resultMap; + } + if (!musicLockCache.get(nameSpace).toString().equals(aid)) { + resultMap.put("Exception Message", + "Unauthorized operation. Invalid AID for the Namespace"); + return resultMap; + } + } else if (musicLockCache.get(nameSpace) != null + && !musicLockCache.get(nameSpace).toString().equals(aid)) { + resultMap.put("Exception Message", + "Unauthorized operation. Invalid AID for the Namespace"); + return resultMap; + } + return resultMap; + } + + + public static void updateMusicCache(String aid, String keyspace) { logger.info("Updating musicCache for keyspace " + keyspace + " with aid " + aid); @@ -346,14 +389,15 @@ public class CachingUtil implements Runnable { } PreparedQueryObject queryObject = new PreparedQueryObject(); queryObject.appendQueryString( - "select * from admin.keyspace_master where application_name=? and username=? allow filtering"); + "select * from admin.keyspace_master where application_name = ? and username = ? and password = ? allow filtering"); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), password)); Row rs = MusicCore.get(queryObject).one(); if (rs == null) { - logger.error(EELFLoggerDelegate.errorLogger,"Namespace and UserId doesn't match. namespace: "+ns+" and userId: "+userId); + logger.error(EELFLoggerDelegate.errorLogger,"Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); - resultMap.put("Exception", "Namespace and UserId doesn't match. namespace: "+ns+" and userId: "+userId); + resultMap.put("Exception", "Namespace, UserId and password doesn't match. namespace: "+ns+" and userId: "+userId); } else { boolean is_aaf = rs.getBool("is_aaf"); String keyspace = rs.getString("keyspace_name"); diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index 96056160..3d5cf4e5 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -385,44 +385,7 @@ public class MusicCore { } - /** - * this function is mainly for the benchmarks to see the effect of lock deletion. - * - * @param keyspaceName - * @param tableName - * @param primaryKey - * @param queryObject - * @param conditionInfo - * @return - */ - public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, - String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) { - long start = System.currentTimeMillis(); - String key = keyspaceName + "." + tableName + "." + primaryKey; - String lockId = createLockReference(key); - long lockCreationTime = System.currentTimeMillis(); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); - long lockAcqTime = System.currentTimeMillis(); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, - queryObject, lockId, conditionInfo); - long criticalPutTime = System.currentTimeMillis(); - deleteLock(key); - long lockDeleteTime = System.currentTimeMillis(); - String timingInfo = "|lock creation time:" + (lockCreationTime - start) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|critical put time:" + (criticalPutTime - lockAcqTime) - + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; - criticalPutResult.setTimingInfo(timingInfo); - return criticalPutResult; - } else { - logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - deleteLock(key); - return lockAcqResult; - } - } + /** * @@ -515,6 +478,10 @@ public class MusicCore { logger.info(EELFLoggerDelegate.applicationLogger,"Time taken to release lock:" + (end - start) + " ms"); return mls; } + + public static void voluntaryReleaseLock(String lockId) throws MusicLockingException{ + getLockingServiceHandle().unlockAndDeleteId(lockId); + } /** * @@ -734,22 +701,63 @@ public class MusicCore { * @param primaryKey primary key value * @param queryObject query object containing prepared query and values * @return ReturnType + * @throws MusicLockingException */ public static ReturnType atomicPut(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject, Condition conditionInfo) { + PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { + + long start = System.currentTimeMillis(); + String key = keyspaceName + "." + tableName + "." + primaryKey; + String lockId = createLockReference(key); + long lockCreationTime = System.currentTimeMillis(); + ReturnType lockAcqResult = acquireLock(key, lockId); + long lockAcqTime = System.currentTimeMillis(); + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); + ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, + queryObject, lockId, conditionInfo); + long criticalPutTime = System.currentTimeMillis(); + voluntaryReleaseLock(lockId); + long lockDeleteTime = System.currentTimeMillis(); + String timingInfo = "|lock creation time:" + (lockCreationTime - start) + + "|lock accquire time:" + (lockAcqTime - lockCreationTime) + + "|critical put time:" + (criticalPutTime - lockAcqTime) + + "|lock delete time:" + (lockDeleteTime - criticalPutTime) + "|"; + criticalPutResult.setTimingInfo(timingInfo); + return criticalPutResult; + } else { + logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); + destroyLockRef(lockId); + return lockAcqResult; + } + } + + /** + * this function is mainly for the benchmarks to see the effect of lock deletion. + * + * @param keyspaceName + * @param tableName + * @param primaryKey + * @param queryObject + * @param conditionInfo + * @return + * @throws MusicLockingException + */ + public static ReturnType atomicPutWithDeleteLock(String keyspaceName, String tableName, + String primaryKey, PreparedQueryObject queryObject, Condition conditionInfo) throws MusicLockingException { + long start = System.currentTimeMillis(); String key = keyspaceName + "." + tableName + "." + primaryKey; String lockId = createLockReference(key); long lockCreationTime = System.currentTimeMillis(); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = acquireLock(key, lockId); long lockAcqTime = System.currentTimeMillis(); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); ReturnType criticalPutResult = criticalPut(keyspaceName, tableName, primaryKey, queryObject, lockId, conditionInfo); long criticalPutTime = System.currentTimeMillis(); - boolean voluntaryRelease = true; deleteLock(key); long lockDeleteTime = System.currentTimeMillis(); String timingInfo = "|lock creation time:" + (lockCreationTime - start) @@ -760,10 +768,12 @@ public class MusicCore { return criticalPutResult; } else { logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); - destroyLockRef(lockId); + deleteLock(key); return lockAcqResult; } } + + /** @@ -775,25 +785,48 @@ public class MusicCore { * @param queryObject query object containing prepared query and values * @return ResultSet * @throws MusicServiceException + * @throws MusicLockingException */ public static ResultSet atomicGet(String keyspaceName, String tableName, String primaryKey, - PreparedQueryObject queryObject) throws MusicServiceException { + PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { String key = keyspaceName + "." + tableName + "." + primaryKey; String lockId = createLockReference(key); long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = acquireLockWithLease(key, lockId, leasePeriod); + ReturnType lockAcqResult = acquireLock(key, lockId); if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); - boolean voluntaryRelease = true; - releaseLock(lockId, voluntaryRelease); + voluntaryReleaseLock(lockId); return result; } else { + destroyLockRef(lockId); logger.info(EELFLoggerDelegate.applicationLogger,"unable to acquire lock, id " + lockId); return null; } } + + public static ResultSet atomicGetWithDeleteLock(String keyspaceName, String tableName, String primaryKey, + PreparedQueryObject queryObject) throws MusicServiceException, MusicLockingException { + String key = keyspaceName + "." + tableName + "." + primaryKey; + String lockId = createLockReference(key); + long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); + + ReturnType lockAcqResult = acquireLock(key, lockId); + + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + logger.info(EELFLoggerDelegate.applicationLogger, "acquired lock with id " + lockId); + ResultSet result = criticalGet(keyspaceName, tableName, primaryKey, queryObject, lockId); + deleteLock(key); + return result; + } else { + deleteLock(key); + logger.info(EELFLoggerDelegate.applicationLogger, "unable to acquire lock, id " + lockId); + return null; + } + } + + /** * authenticate user logic @@ -822,7 +855,13 @@ public class MusicCore { resultMap.put("Exception", "Aid is mandatory for nonAAF applications "); return resultMap; } - resultMap = CachingUtil.authenticateAIDUser(aid, keyspace); + if(operation.contains("Lock")) { + resultMap = CachingUtil.authenticateAIDUserLock(aid, nameSpace); + } + else { + resultMap = CachingUtil.authenticateAIDUser(aid, keyspace); + } + if (!resultMap.isEmpty()) return resultMap; } @@ -837,7 +876,7 @@ public class MusicCore { if (isAAF && nameSpace != null && userId != null && password != null) { boolean isValid = true; try { - isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); + isValid = CachingUtil.authenticateAAFUser(nameSpace, userId, password, keyspace); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger,"Got exception while AAF authentication for namespace " + nameSpace); resultMap.put("Exception", e.getMessage()); diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 47e23973..317f2560 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -66,6 +66,8 @@ public class MusicUtil { public static final String ATOMIC = "atomic"; public static final String EVENTUAL = "eventual"; public static final String CRITICAL = "critical"; + public static final String ATOMICDELETELOCK = "atomic_delete_lock"; + public static final String DEFAULTKEYSPACENAME = "TBD"; private static String cassName = "cassandra"; private static String cassPwd = "cassandra"; diff --git a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java b/src/main/java/org/onap/music/rest/RestMusicBmAPI.java index 8f62f9c1..55eb47f2 100644 --- a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicBmAPI.java @@ -31,25 +31,18 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; -import org.apache.log4j.Logger; import org.onap.music.datastore.jsonobjects.JsonInsert; -import org.onap.music.datastore.jsonobjects.JsonOnboard; -import org.onap.music.datastore.jsonobjects.JsonUpdate; import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.onap.music.datastore.PreparedQueryObject; import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.TableMetadata; import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; +//import io.swagger.annotations.ApiOperation; +//import io.swagger.annotations.ApiParam; /* * These are functions created purely for benchmarking purposes. Commented out Swagger - This should diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index e16cc373..f1d88efa 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -36,8 +36,6 @@ import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.UriInfo; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import org.onap.music.datastore.PreparedQueryObject; import org.onap.music.datastore.jsonobjects.JsonDelete; import org.onap.music.datastore.jsonobjects.JsonInsert; @@ -93,6 +91,7 @@ public class RestMusicDataAPI { private class RowIdentifier { public String primarKeyValue; public StringBuilder rowIdString; + @SuppressWarnings("unused") public PreparedQueryObject queryObject;// the string with all the row // identifiers separated by AND @@ -104,6 +103,7 @@ public class RestMusicDataAPI { } } + @SuppressWarnings("unused") private String buildVersion(String major, String minor, String patch) { if (minor != null) { major += "." + minor; @@ -187,12 +187,14 @@ public class RestMusicDataAPI { logger.error(EELFLoggerDelegate.errorLogger, "resulta = " + result); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put("Exception", "Couldn't create keyspace. Please make sure all the information is correct."); } logger.debug("result = " + result); if (!result) { resultMap.put("Status", String.valueOf(result)); - resultMap.put("Exception", "Keyspace already exists. Please contact admin."); + if(!resultMap.containsKey("Exception")) + resultMap.put("Exception", "Keyspace already exists. Please contact admin."); if (resultMap.get("uuid").equals("new")) { queryObject = new PreparedQueryObject(); queryObject.appendQueryString( @@ -206,10 +208,9 @@ public class RestMusicDataAPI { } else { queryObject = new PreparedQueryObject(); queryObject.appendQueryString( - "UPDATE admin.keyspace_master SET keyspace_name=?,password=?,is_api=null where uuid = ?;"); + "UPDATE admin.keyspace_master SET keyspace_name=? where uuid = ?;"); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicUtil.DEFAULTKEYSPACENAME)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), null)); queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), newAid)); result = MusicCore.nonKeyRelatedPut(queryObject, consistency); resultMap.remove("aid"); @@ -223,17 +224,11 @@ public class RestMusicDataAPI { queryObject.appendQueryString("CREATE ROLE IF NOT EXISTS '" + userId + "' WITH PASSWORD = '" + password + "' AND LOGIN = true;"); result = MusicCore.nonKeyRelatedPut(queryObject, consistency); - if (result) { - queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName - + " to " + userId); - queryObject.appendQueryString(";"); - result = MusicCore.nonKeyRelatedPut(queryObject, consistency); - } else { - resultMap.remove("uuid"); - resultMap.put("Exception", "Exception while creating user."); - return resultMap; - } + queryObject = new PreparedQueryObject(); + queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName + + " to '" + userId + "'"); + queryObject.appendQueryString(";"); + result = MusicCore.nonKeyRelatedPut(queryObject, consistency); } catch (Exception e) { logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); } @@ -393,6 +388,7 @@ public class RestMusicDataAPI { if (ot instanceof String) { value = "'" + value + "'"; } else if (ot instanceof Map) { + @SuppressWarnings("unchecked") Map<String, Object> otMap = (Map<String, Object>) ot; value = "{" + MusicUtil.jsonMaptoSqlString(otMap, ",") + "}"; } @@ -589,6 +585,10 @@ public class RestMusicDataAPI { result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); } + else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { + result = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, primaryKey, queryObject, null); + + } return (result != null) ? result.toMap() : new ReturnType(ResultType.FAILURE, "Null result - Please Contact admin").toMap(); @@ -862,6 +862,10 @@ public class RestMusicDataAPI { operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, queryObject, conditionInfo); } + else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { + operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, + queryObject, conditionInfo); + } try { return operationResult.toMap(); } catch (NullPointerException e) { @@ -970,7 +974,6 @@ public class RestMusicDataAPI { String lockId = selObj.getConsistencyInfo().get("lockId"); PreparedQueryObject queryObject = new PreparedQueryObject(); - StringBuilder rowSpec = new StringBuilder(); RowIdentifier rowId = null; try { @@ -979,18 +982,22 @@ public class RestMusicDataAPI { return MusicUtil.setErrorResponse(ex); } queryObject.appendQueryString( - "SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowSpec + ";"); + "SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowId.rowIdString + ";"); ResultSet results = null; String consistency = selObj.getConsistencyInfo().get("type"); - if (consistency.equalsIgnoreCase("critical")) { + if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { results = MusicCore.criticalGet(keyspace, tablename, rowId.primarKeyValue, queryObject, lockId); - } else if (consistency.equalsIgnoreCase("atomic")) { + } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { results = MusicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); } + + else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { + results = MusicCore.atomicGetWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, queryObject); + } return MusicCore.marshallResults(results); } diff --git a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java index 5f28f447..e565e829 100644 --- a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java @@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletResponse; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; @@ -37,16 +38,11 @@ import javax.ws.rs.core.MediaType; import org.onap.music.datastore.jsonobjects.JsonLeasedLock; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.lockingservice.MusicLockState; -import org.onap.music.lockingservice.MusicLockState.LockStatus; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; import org.onap.music.main.ResultType; import org.onap.music.main.ReturnType; import org.onap.music.response.jsonobjects.JsonLockResponse; -import org.powermock.core.spi.testresult.Result; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -57,7 +53,8 @@ import io.swagger.annotations.ApiParam; @Api(value="Lock Api") public class RestMusicLocksAPI { - private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class); + @SuppressWarnings("unused") + private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class); private static String xLatestVersion = "X-latestVersion"; /** @@ -66,6 +63,7 @@ public class RestMusicLocksAPI { * * @param lockName * @return + * @throws Exception */ @POST @@ -78,8 +76,20 @@ public class RestMusicLocksAPI { @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> createLockReference( @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @Context HttpServletResponse response){ - response.addHeader(xLatestVersion,MusicUtil.getVersion()); + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ + response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "createLockReference"); + if (!resultMap.isEmpty()) { + return resultMap; + } ResultType status = ResultType.SUCCESS; String lockId = MusicCore.createLockReference(lockName); if (lockId == null) { status = ResultType.FAILURE; } @@ -92,6 +102,7 @@ public class RestMusicLocksAPI { * * @param lockId * @return + * @throws Exception */ @GET @Path("/acquire/{lockreference}") @@ -101,8 +112,20 @@ public class RestMusicLocksAPI { @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> accquireLock( @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "accquireLock"); + if (!resultMap.isEmpty()) { + return resultMap; + } String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId); return new JsonLockResponse(lockStatus.getResult()).setLock(lockId) @@ -119,8 +142,20 @@ public class RestMusicLocksAPI { @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> accquireLockWithLease(JsonLeasedLock lockObj, @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "accquireLockWithLease"); + if (!resultMap.isEmpty()) { + return resultMap; + } String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()); return new JsonLockResponse(lockLeaseStatus.getResult()).setLock(lockName) @@ -137,8 +172,20 @@ public class RestMusicLocksAPI { @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> currentLockHolder( @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "currentLockHolder"); + if (!resultMap.isEmpty()) { + return resultMap; + } String who = MusicCore.whoseTurnIsIt(lockName); ResultType status = ResultType.SUCCESS; String error = ""; @@ -158,8 +205,20 @@ public class RestMusicLocksAPI { @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> currentLockState( @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "currentLockState"); + if (!resultMap.isEmpty()) { + return resultMap; + } MusicLockState mls = MusicCore.getMusicLockState(lockName); Map<String,Object> returnMap = null; JsonLockResponse jsonResponse = new JsonLockResponse(ResultType.FAILURE).setLock(lockName); @@ -179,6 +238,7 @@ public class RestMusicLocksAPI { * deletes the process from the zk queue * * @param lockId + * @throws Exception */ @DELETE @Path("/release/{lockreference}") @@ -187,8 +247,20 @@ public class RestMusicLocksAPI { response = Map.class) @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> unLock(@PathParam("lockreference") String lockId, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "unLock"); + if (!resultMap.isEmpty()) { + return resultMap; + } boolean voluntaryRelease = true; MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease); Map<String,Object> returnMap = null; @@ -206,14 +278,27 @@ public class RestMusicLocksAPI { /** * * @param lockName + * @throws Exception */ @DELETE @Path("/delete/{lockname}") @ApiOperation(value = "Delete Lock", response = Map.class) @Produces(MediaType.APPLICATION_JSON) public Map<String,Object> deleteLock(@PathParam("lockname") String lockName, - @Context HttpServletResponse response){ + @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, + @ApiParam(value = "Application namespace", + required = true) @HeaderParam("ns") String ns, + @ApiParam(value = "userId", + required = true) @HeaderParam("userId") String userId, + @ApiParam(value = "Password", + required = true) @HeaderParam("password") String password, + @Context HttpServletResponse response) throws Exception{ response.addHeader(xLatestVersion,MusicUtil.getVersion()); + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, null, aid, + "deleteLock"); + if (!resultMap.isEmpty()) { + return resultMap; + } MusicCore.deleteLock(lockName); return new JsonLockResponse(ResultType.SUCCESS).toMap(); } diff --git a/src/main/java/org/onap/music/rest/RestMusicTestAPI.java b/src/main/java/org/onap/music/rest/RestMusicTestAPI.java index f606eb8d..287fa176 100644 --- a/src/main/java/org/onap/music/rest/RestMusicTestAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicTestAPI.java @@ -34,9 +34,6 @@ import javax.ws.rs.core.MediaType; import org.onap.music.eelf.logging.EELFLoggerDelegate; import org.onap.music.main.MusicUtil; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -45,7 +42,8 @@ import io.swagger.annotations.ApiOperation; @Api(value="Test Api") public class RestMusicTestAPI { - private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicTestAPI.class); + @SuppressWarnings("unused") + private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicTestAPI.class); /** * Returns a test JSON. This will confirm that REST is working. |