Docker update and POM fix
[music.git] / src / main / java / org / onap / music / rest / RestMusicLocksAPI.java
index 8612b1f..d871a50 100644 (file)
@@ -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;
@@ -35,13 +36,16 @@ import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 
 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
+import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.eelf.logging.format.AppMessages;
+import org.onap.music.eelf.logging.format.ErrorSeverity;
+import org.onap.music.eelf.logging.format.ErrorTypes;
 import org.onap.music.lockingservice.MusicLockState;
 import org.onap.music.main.MusicCore;
 import org.onap.music.main.MusicUtil;
-import org.onap.music.response.jsonobjects.JsonLockResponse;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
+import org.onap.music.main.ResultType;
+import org.onap.music.main.ReturnType;
+import org.onap.music.response.jsonobjects.JsonResponse;
 
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -52,14 +56,17 @@ import io.swagger.annotations.ApiParam;
 @Api(value="Lock Api")
 public class RestMusicLocksAPI {
 
-       private static EELFLogger logger = EELFManager.getInstance().getLogger(RestMusicLocksAPI.class);
+       @SuppressWarnings("unused")
+    private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
        private static String xLatestVersion = "X-latestVersion";
+
        /**
         * Puts the requesting process in the q for this lock. The corresponding
         * node will be created in zookeeper if it did not already exist
         * 
         * @param lockName
         * @return
+        * @throws Exception 
         */
 
        @POST
@@ -72,12 +79,36 @@ 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());              
-               Boolean status = true;
+                       @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.validateLock(lockName);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "createLockReference");
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+                return resultMap;
+        }
+               ResultType status = ResultType.SUCCESS;
                String lockId = MusicCore.createLockReference(lockName);
-               if ( lockId == null ) { status = false; }
-               return new JsonLockResponse(status.toString(),"",lockId).toMap();
+               
+               if (lockId == null) { 
+                       status = ResultType.FAILURE; 
+                       response.setStatus(400);
+               }
+               return new JsonResponse(status).setLock(lockId).toMap();
        }
 
        /**
@@ -86,6 +117,7 @@ public class RestMusicLocksAPI {
         * 
         * @param lockId
         * @return
+        * @throws Exception 
         */
        @GET
        @Path("/acquire/{lockreference}")
@@ -95,11 +127,39 @@ 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());
-               String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
-               Boolean lockStatus = MusicCore.acquireLock(lockName,lockId);
-               return new JsonLockResponse(lockStatus.toString(),"",lockId,lockStatus.toString(),"").toMap();
+        Map<String, Object> resultMap = MusicCore.validateLock(lockId);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "accquireLock");
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+                return resultMap;
+        }
+               try {
+                       String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
+                       ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId);
+                       return new JsonResponse(lockStatus.getResult()).setLock(lockId)
+                                                                               .setMessage(lockStatus.getMessage()).toMap();
+               } catch (Exception e) {
+                       logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
+                       resultMap.put("Exception","Unable to aquire lock");
+                       response.setStatus(400);
+                       return new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap();
+               }
        }
        
 
@@ -112,11 +172,35 @@ 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.validateLock(lockId);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "accquireLockWithLease");
+
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+            response.setStatus(400);    
+               return resultMap;
+        }
                String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
-               String lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()).toString();
-               return new JsonLockResponse(lockLeaseStatus,"",lockName,lockLeaseStatus,"",String.valueOf(lockObj.getLeasePeriod())).toMap(); 
+               ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
+               return new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName)
+                                                                       .setMessage(lockLeaseStatus.getMessage())
+                                                                       .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap();
        } 
        
 
@@ -128,16 +212,38 @@ 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.validateLock(lockName);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "currentLockHolder");
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+                return resultMap;
+        }
                String who = MusicCore.whoseTurnIsIt(lockName);
-               String status = "true";
+               ResultType status = ResultType.SUCCESS;
                String error = "";
                if ( who == null ) { 
-                       status = "false"
+                       status = ResultType.FAILURE
                        error = "There was a problem getting the lock holder";
+                       response.setStatus(400);
                }
-               return new JsonLockResponse(status,error,lockName,"",who).toMap();
+               return new JsonResponse(status).setError(error)
+                                               .setLock(lockName).setLockHolder(who).toMap();
        }
 
        @GET
@@ -148,20 +254,43 @@ 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());
-               MusicLockState mls = MusicCore.getMusicLockState(lockName);
+        Map<String, Object> resultMap = MusicCore.validateLock(lockName);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "currentLockState");
+        
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+               response.setStatus(400);
+                return resultMap;
+        }
+               
+        MusicLockState mls = MusicCore.getMusicLockState(lockName);
                Map<String,Object> returnMap = null;
-               JsonLockResponse jsonResponse = new JsonLockResponse("false","",lockName);
+               JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
                if(mls == null) {
                        jsonResponse.setError("");
                        jsonResponse.setMessage("No lock object created yet..");
                } else { 
-                       jsonResponse.setStatus("true");
-                       jsonResponse.setLockStatus(mls.getLockStatus().toString());
+                       jsonResponse.setStatus(ResultType.SUCCESS);
+                       jsonResponse.setLockStatus(mls.getLockStatus());
                        jsonResponse.setLockHolder(mls.getLockHolder());
                } 
-               return returnMap;
+               return jsonResponse.toMap();
        }
 
        /**
@@ -169,6 +298,7 @@ public class RestMusicLocksAPI {
         * deletes the process from the zk queue
         * 
         * @param lockId
+        * @throws Exception 
         */
        @DELETE
        @Path("/release/{lockreference}")
@@ -177,16 +307,43 @@ 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.validateLock(lockId);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "unLock");
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+               response.setStatus(400);
+            return resultMap;
+        }
                boolean voluntaryRelease = true; 
                MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
+               if(mls.getErrorMessage() != null) {
+                       resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage());
+                       return resultMap;
+               }
                Map<String,Object> returnMap = null;
-               if ( mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED ) {
-                       returnMap = new JsonLockResponse("Unlocked","","").toMap();
+               if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) {
+                       returnMap = new JsonResponse(ResultType.SUCCESS).setLock(lockId)
+                                                               .setLockStatus(mls.getLockStatus()).toMap();
                }
-               if ( mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
-                       returnMap = new JsonLockResponse("Locked","","").toMap();
+               if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
+                       returnMap = new JsonResponse(ResultType.FAILURE).setLock(lockId)
+                                                               .setLockStatus(mls.getLockStatus()).toMap();
                }
                return returnMap;
        }
@@ -194,16 +351,38 @@ 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.validateLock(lockName);
+        if (resultMap.containsKey("Exception")) {
+            return resultMap;
+        }
+        String keyspaceName = (String) resultMap.get("keyspace");
+        resultMap.remove("keyspace");
+        resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
+                "deleteLock");
+        if (resultMap.containsKey("aid"))
+            resultMap.remove("aid");
+        if (!resultMap.isEmpty()) {
+               response.setStatus(400);
+            return resultMap;
+        }
                MusicCore.deleteLock(lockName);
-               return new JsonLockResponse("true","","").toMap();
+               return new JsonResponse(ResultType.SUCCESS).toMap();
        }
 
 }