Merge "MusicLockingExceptionTest - Junit test cases"
[music.git] / src / main / java / org / onap / music / rest / RestMusicLocksAPI.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.rest;
24
25 import java.util.Map;
26
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.HeaderParam;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.Path;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.Produces;
35 import javax.ws.rs.core.MediaType;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.ResponseBuilder;
38 import javax.ws.rs.core.Response.Status;
39
40 import org.onap.music.authentication.MusicAuthentication;
41 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
42 import org.onap.music.eelf.logging.EELFLoggerDelegate;
43 import org.onap.music.eelf.logging.format.AppMessages;
44 import org.onap.music.eelf.logging.format.ErrorSeverity;
45 import org.onap.music.eelf.logging.format.ErrorTypes;
46 import org.onap.music.lockingservice.cassandra.MusicLockState;
47 import org.onap.music.main.MusicCore;
48 import org.onap.music.main.MusicUtil;
49 import org.onap.music.main.ResultType;
50 import org.onap.music.main.ReturnType;
51 import org.onap.music.response.jsonobjects.JsonResponse;
52
53 import io.swagger.annotations.Api;
54 import io.swagger.annotations.ApiOperation;
55 import io.swagger.annotations.ApiParam;
56
57
58 @Path("/v2/locks/")
59 @Api(value="Lock Api")
60 public class RestMusicLocksAPI {
61
62     private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
63     private static final String XMINORVERSION = "X-minorVersion";
64     private static final String XPATCHVERSION = "X-patchVersion";
65     private static final String VERSION = "v2";
66
67     /**
68      * Puts the requesting process in the q for this lock. The corresponding
69      * node will be created in zookeeper if it did not already exist
70      * 
71      * @param lockName
72      * @return
73      * @throws Exception 
74      */
75     @POST
76     @Path("/create/{lockname}")
77     @ApiOperation(value = "Create Lock",
78         notes = "Puts the requesting process in the q for this lock." +
79         " The corresponding node will be created in zookeeper if it did not already exist." +
80         " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId",
81         response = Map.class)
82     @Produces(MediaType.APPLICATION_JSON)    
83     public Response createLockReference(
84             @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
85             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
86             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
87             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
88             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
89             @ApiParam(value = "Application namespace",
90                             required = true) @HeaderParam("ns") String ns) throws Exception{
91         try {
92         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
93         Map<String, Object> resultMap = MusicCore.validateLock(lockName);
94         if (resultMap.containsKey("Error")) {
95             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
96             response.status(Status.BAD_REQUEST);
97             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
98         }
99         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
100         String userId = userCredentials.get(MusicUtil.USERID);
101         String password = userCredentials.get(MusicUtil.PASSWORD);
102         String keyspaceName = (String) resultMap.get("keyspace");
103         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
104         resultMap.remove("keyspace");
105         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
106                 "createLockReference");
107         if (resultMap.containsKey("aid"))
108             resultMap.remove("aid");
109         if (!resultMap.isEmpty()) {
110             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
111             response.status(Status.UNAUTHORIZED);
112             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
113         }
114         ResultType status = ResultType.SUCCESS;
115         String lockId = MusicCore.createLockReference(lockName);
116         
117         if (lockId == null) { 
118             status = ResultType.FAILURE; 
119             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
120             return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError("Lock Id is null").toMap()).build();
121         }
122         return response.status(Status.OK).entity(new JsonResponse(status).setLock(lockId).toMap()).build();
123         } finally {
124             EELFLoggerDelegate.mdcRemove("keyspace");
125         }
126     }
127
128     /**
129      * 
130      * Checks if the node is in the top of the queue and hence acquires the lock
131      * 
132      * @param lockId
133      * @return
134      * @throws Exception 
135      */
136     @GET
137     @Path("/acquire/{lockreference}")
138     @ApiOperation(value = "Aquire Lock", 
139         notes = "Checks if the node is in the top of the queue and hence acquires the lock",
140         response = Map.class)
141     @Produces(MediaType.APPLICATION_JSON)    
142     public Response accquireLock(
143             @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
144             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
145             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
146             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
147             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
148             @ApiParam(value = "Application namespace",
149                             required = true) @HeaderParam("ns") String ns) throws Exception{
150         try { 
151         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
152         Map<String, Object> resultMap = MusicCore.validateLock(lockId);
153         if (resultMap.containsKey("Error")) {
154             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
155             response.status(Status.BAD_REQUEST);
156             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
157         }
158         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
159         String userId = userCredentials.get(MusicUtil.USERID);
160         String password = userCredentials.get(MusicUtil.PASSWORD);
161         String keyspaceName = (String) resultMap.get("keyspace");
162         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
163         resultMap.remove("keyspace");
164         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
165                 "accquireLock");
166         if (resultMap.containsKey("aid"))
167             resultMap.remove("aid");
168         if (!resultMap.isEmpty()) {
169             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
170             response.status(Status.UNAUTHORIZED);
171             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
172         }
173         try {
174             String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
175             ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId);
176             if ( lockStatus.getResult().equals(ResultType.SUCCESS)) {
177                 response.status(Status.OK);
178             } else {
179                 response.status(Status.BAD_REQUEST);
180             }
181             return response.entity(new JsonResponse(lockStatus.getResult()).setLock(lockId).setMessage(lockStatus.getMessage()).toMap()).build();
182         } catch (Exception e) {
183             logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
184             return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap()).build();
185         }
186         } finally {
187             EELFLoggerDelegate.mdcRemove("keyspace");
188         }
189     }
190     
191
192
193     
194     @POST
195     @Path("/acquire-with-lease/{lockreference}")
196     @ApiOperation(value = "Aquire Lock with Lease", response = Map.class)
197     @Consumes(MediaType.APPLICATION_JSON)
198     @Produces(MediaType.APPLICATION_JSON)    
199     public Response accquireLockWithLease(JsonLeasedLock lockObj, 
200             @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
201             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
202             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
203             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
204             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
205             @ApiParam(value = "Application namespace",
206                             required = true) @HeaderParam("ns") String ns) throws Exception{
207         try {
208         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
209         Map<String, Object> resultMap = MusicCore.validateLock(lockId);
210         if (resultMap.containsKey("Error")) {
211             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
212             response.status(Status.BAD_REQUEST);
213             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
214         }
215         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
216         String userId = userCredentials.get(MusicUtil.USERID);
217         String password = userCredentials.get(MusicUtil.PASSWORD);
218         String keyspaceName = (String) resultMap.get("keyspace");
219         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
220         resultMap.remove("keyspace");
221         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
222                 "accquireLockWithLease");
223
224         if (resultMap.containsKey("aid"))
225             resultMap.remove("aid");
226         if (!resultMap.isEmpty()) {
227             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
228             response.status(Status.UNAUTHORIZED);
229             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
230         }
231         String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
232         ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
233         if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) {
234             response.status(Status.OK);
235         } else {
236             response.status(Status.BAD_REQUEST);
237         }
238         return response.entity(new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName)
239                                     .setMessage(lockLeaseStatus.getMessage())
240                                     .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap()).build();
241         } finally {
242             EELFLoggerDelegate.mdcRemove("keyspace");
243         }
244     } 
245     
246
247     @GET
248     @Path("/enquire/{lockname}")
249     @ApiOperation(value = "Get Lock Holder", 
250         notes = "Gets the current Lock Holder",
251         response = Map.class)
252     @Produces(MediaType.APPLICATION_JSON)    
253     public Response currentLockHolder(
254             @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
255             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
256             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
257             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
258             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
259             @ApiParam(value = "Application namespace",
260                             required = true) @HeaderParam("ns") String ns) throws Exception{
261         try {
262         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
263         Map<String, Object> resultMap = MusicCore.validateLock(lockName);
264         if (resultMap.containsKey("Error")) {
265             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
266             response.status(Status.BAD_REQUEST);
267             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
268         }
269         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
270         String userId = userCredentials.get(MusicUtil.USERID);
271         String password = userCredentials.get(MusicUtil.PASSWORD);
272         String keyspaceName = (String) resultMap.get("keyspace");
273         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
274         resultMap.remove("keyspace");
275         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
276                 "currentLockHolder");
277         if (resultMap.containsKey("aid"))
278             resultMap.remove("aid");
279         if (!resultMap.isEmpty()) {
280             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
281             response.status(Status.UNAUTHORIZED);
282             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
283         }
284         String who = MusicCore.whoseTurnIsIt(lockName);
285         ResultType status = ResultType.SUCCESS;
286         String error = "";
287         if ( who == null ) { 
288             status = ResultType.FAILURE; 
289             error = "There was a problem getting the lock holder";
290             logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
291             return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
292         }
293         return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
294         } finally {
295             EELFLoggerDelegate.mdcRemove("keyspace");
296         }
297     }
298
299     @GET
300     @Path("/{lockname}")
301     @ApiOperation(value = "Lock State",
302         notes = "Returns current Lock State and Holder.",
303         response = Map.class)
304     @Produces(MediaType.APPLICATION_JSON)    
305     public Response currentLockState(
306             @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
307             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
308             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
309             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
310             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
311             @ApiParam(value = "Application namespace",
312                             required = true) @HeaderParam("ns") String ns) throws Exception{
313         try {
314         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
315         Map<String, Object> resultMap = MusicCore.validateLock(lockName);
316         if (resultMap.containsKey("Error")) {
317             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
318             response.status(Status.BAD_REQUEST);
319             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
320         }
321         String keyspaceName = (String) resultMap.get("keyspace");
322         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
323         resultMap.remove("keyspace");
324         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
325         String userId = userCredentials.get(MusicUtil.USERID);
326         String password = userCredentials.get(MusicUtil.PASSWORD);
327         resultMap.remove("keyspace");
328         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
329                 "currentLockHolder");
330         if (resultMap.containsKey("aid"))
331             resultMap.remove("aid");
332         if (!resultMap.isEmpty()) {
333             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
334             response.status(Status.UNAUTHORIZED);
335             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
336         }
337         
338         String who = MusicCore.whoseTurnIsIt(lockName);
339         ResultType status = ResultType.SUCCESS;
340         String error = "";
341         if ( who == null ) { 
342             status = ResultType.FAILURE; 
343             error = "There was a problem getting the lock holder";
344             logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
345             return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
346         }
347         return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
348         } finally {
349             EELFLoggerDelegate.mdcRemove("keyspace");
350         } 
351         
352         //MusicLockState mls = MusicZKCore.getMusicLockState(lockName);
353 //        Map<String,Object> returnMap = null;
354 //        JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
355 //        if(mls == null) {
356 //            jsonResponse.setError("");
357 //            jsonResponse.setMessage("No lock object created yet..");
358 //            logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
359 //            return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build();
360 //        } else { 
361 //            jsonResponse.setStatus(ResultType.SUCCESS);
362 //            jsonResponse.setLockStatus(mls.getLockStatus());
363 //            jsonResponse.setLockHolder(mls.getLockHolder());
364 //            return response.status(Status.OK).entity(jsonResponse.toMap()).build();
365 //        }
366
367     }
368
369     /**
370      * 
371      * deletes the process from the zk queue
372      * 
373      * @param lockId
374      * @throws Exception 
375      */
376     @DELETE
377     @Path("/release/{lockreference}")
378     @ApiOperation(value = "Release Lock",
379         notes = "deletes the process from the zk queue",
380         response = Map.class)
381     @Produces(MediaType.APPLICATION_JSON)    
382     public Response unLock(@PathParam("lockreference") String lockId,
383             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
384             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
385             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
386             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
387             @ApiParam(value = "Application namespace",
388                             required = true) @HeaderParam("ns") String ns) throws Exception{
389         try {
390         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
391         Map<String, Object> resultMap = MusicCore.validateLock(lockId);
392         if (resultMap.containsKey("Error")) {
393             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
394             response.status(Status.BAD_REQUEST);
395             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
396         }
397         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
398         String userId = userCredentials.get(MusicUtil.USERID);
399         String password = userCredentials.get(MusicUtil.PASSWORD);
400         String keyspaceName = (String) resultMap.get("keyspace");
401         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
402         resultMap.remove("keyspace");
403         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
404                 "unLock");
405         if (resultMap.containsKey("aid"))
406             resultMap.remove("aid");
407         if (!resultMap.isEmpty()) {
408             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
409             response.status(Status.UNAUTHORIZED);
410             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
411         }
412         boolean voluntaryRelease = true; 
413         MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
414         if(mls.getErrorMessage() != null) {
415             resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage());
416             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
417             return response.status(Status.BAD_REQUEST).entity(resultMap).build();
418         }
419         Map<String,Object> returnMap = null;
420         if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) {
421             returnMap = new JsonResponse(ResultType.SUCCESS).setLock(lockId)
422                                 .setLockStatus(mls.getLockStatus()).toMap();
423             response.status(Status.OK);
424         }
425         if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
426             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
427             returnMap = new JsonResponse(ResultType.FAILURE).setLock(lockId)
428                                 .setLockStatus(mls.getLockStatus()).toMap();
429             response.status(Status.BAD_REQUEST);
430         }
431         return response.entity(returnMap).build();
432         } finally {
433             EELFLoggerDelegate.mdcRemove("keyspace");
434         }
435     }
436
437     /**
438      * 
439      * @param lockName
440      * @throws Exception 
441      */
442     @DELETE
443     @Path("/delete/{lockname}")
444     @ApiOperation(value = "Delete Lock", response = Map.class)
445     @Produces(MediaType.APPLICATION_JSON)    
446     public Response deleteLock(@PathParam("lockname") String lockName,
447             @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
448             @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
449             @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
450             @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
451             @ApiParam(value = "Application namespace",
452                             required = true) @HeaderParam("ns") String ns) throws Exception{
453         try {
454         ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
455         Map<String, Object> resultMap = MusicCore.validateLock(lockName);
456         if (resultMap.containsKey("Error")) {
457             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR  ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
458             response.status(Status.BAD_REQUEST);
459             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
460         }
461         Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
462         String userId = userCredentials.get(MusicUtil.USERID);
463         String password = userCredentials.get(MusicUtil.PASSWORD);
464         String keyspaceName = (String) resultMap.get("keyspace");
465         EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
466         resultMap.remove("keyspace");
467         resultMap = MusicAuthentication.autheticateUser(ns, userId, password, keyspaceName, aid,
468                 "deleteLock");
469         if (resultMap.containsKey("aid"))
470             resultMap.remove("aid");
471         if (!resultMap.isEmpty()) {
472             logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
473             response.status(Status.UNAUTHORIZED);
474             return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
475         }
476         try{
477             MusicCore.deleteLock(lockName);
478         }catch (Exception e) {
479             return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
480         }
481         return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).toMap()).build();
482         } finally {
483             EELFLoggerDelegate.mdcRemove("keyspace");
484         }
485     }
486
487 }