Applying bug fixes
[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 package org.onap.music.rest;
23
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletResponse;
27 import javax.ws.rs.Consumes;
28 import javax.ws.rs.DELETE;
29 import javax.ws.rs.GET;
30 import javax.ws.rs.POST;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.MediaType;
36
37 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
38 import org.onap.music.eelf.logging.EELFLoggerDelegate;
39 import org.onap.music.lockingservice.MusicLockState;
40 import org.onap.music.lockingservice.MusicLockState.LockStatus;
41 import org.onap.music.main.MusicCore;
42 import org.onap.music.main.MusicUtil;
43 import org.onap.music.main.ResultType;
44 import org.onap.music.main.ReturnType;
45 import org.onap.music.response.jsonobjects.JsonLockResponse;
46 import org.powermock.core.spi.testresult.Result;
47
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
50
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiParam;
54
55
56 @Path("/v{version: [0-9]+}/locks/")
57 @Api(value="Lock Api")
58 public class RestMusicLocksAPI {
59
60         private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
61         private static String xLatestVersion = "X-latestVersion";
62
63         /**
64          * Puts the requesting process in the q for this lock. The corresponding
65          * node will be created in zookeeper if it did not already exist
66          * 
67          * @param lockName
68          * @return
69          */
70
71         @POST
72         @Path("/create/{lockname}")
73         @ApiOperation(value = "Create Lock",
74                 notes = "Puts the requesting process in the q for this lock." +
75                 " The corresponding node will be created in zookeeper if it did not already exist." +
76                 " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId",
77                 response = Map.class)
78         @Produces(MediaType.APPLICATION_JSON)   
79         public Map<String,Object> createLockReference(
80                         @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
81                         @Context HttpServletResponse response){
82                 response.addHeader(xLatestVersion,MusicUtil.getVersion());              
83                 ResultType status = ResultType.SUCCESS;
84                 String lockId = MusicCore.createLockReference(lockName);
85                 if (lockId == null) { status = ResultType.FAILURE; }
86                 return new JsonLockResponse(status).setLock(lockId).toMap();
87         }
88
89         /**
90          * 
91          * Checks if the node is in the top of the queue and hence acquires the lock
92          * 
93          * @param lockId
94          * @return
95          */
96         @GET
97         @Path("/acquire/{lockreference}")
98         @ApiOperation(value = "Aquire Lock", 
99                 notes = "Checks if the node is in the top of the queue and hence acquires the lock",
100                 response = Map.class)
101         @Produces(MediaType.APPLICATION_JSON)   
102         public Map<String,Object> accquireLock(
103                         @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
104                         @Context HttpServletResponse response){
105                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
106                 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
107                 ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId);
108                 return new JsonLockResponse(lockStatus.getResult()).setLock(lockId)
109                                                                         .setMessage(lockStatus.getMessage()).toMap();
110         }
111         
112
113
114         
115         @POST
116         @Path("/acquire-with-lease/{lockreference}")
117         @ApiOperation(value = "Aquire Lock with Lease", response = Map.class)
118         @Consumes(MediaType.APPLICATION_JSON)
119         @Produces(MediaType.APPLICATION_JSON)   
120         public Map<String,Object> accquireLockWithLease(JsonLeasedLock lockObj, 
121                         @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
122                         @Context HttpServletResponse response){
123                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
124                 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
125                 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
126                 return new JsonLockResponse(lockLeaseStatus.getResult()).setLock(lockName)
127                                                                         .setMessage(lockLeaseStatus.getMessage())
128                                                                         .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap();
129         } 
130         
131
132         @GET
133         @Path("/enquire/{lockname}")
134         @ApiOperation(value = "Get Lock Holder", 
135                 notes = "Gets the current Lock Holder",
136                 response = Map.class)
137         @Produces(MediaType.APPLICATION_JSON)   
138         public Map<String,Object> currentLockHolder(
139                         @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
140                         @Context HttpServletResponse response){
141                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
142                 String who = MusicCore.whoseTurnIsIt(lockName);
143                 ResultType status = ResultType.SUCCESS;
144                 String error = "";
145                 if ( who == null ) { 
146                         status = ResultType.FAILURE; 
147                         error = "There was a problem getting the lock holder";
148                 }
149                 return new JsonLockResponse(status).setError(error)
150                                                 .setLock(lockName).setLockHolder(who).toMap();
151         }
152
153         @GET
154         @Path("/{lockname}")
155         @ApiOperation(value = "Lock State",
156                 notes = "Returns current Lock State and Holder.",
157                 response = Map.class)
158         @Produces(MediaType.APPLICATION_JSON)   
159         public Map<String,Object> currentLockState(
160                         @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
161                         @Context HttpServletResponse response){
162                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
163                 MusicLockState mls = MusicCore.getMusicLockState(lockName);
164                 Map<String,Object> returnMap = null;
165                 JsonLockResponse jsonResponse = new JsonLockResponse(ResultType.FAILURE).setLock(lockName);
166                 if(mls == null) {
167                         jsonResponse.setError("");
168                         jsonResponse.setMessage("No lock object created yet..");
169                 } else { 
170                         jsonResponse.setStatus(ResultType.SUCCESS);
171                         jsonResponse.setLockStatus(mls.getLockStatus());
172                         jsonResponse.setLockHolder(mls.getLockHolder());
173                 } 
174                 return returnMap;
175         }
176
177         /**
178          * 
179          * deletes the process from the zk queue
180          * 
181          * @param lockId
182          */
183         @DELETE
184         @Path("/release/{lockreference}")
185         @ApiOperation(value = "Release Lock",
186                 notes = "deletes the process from the zk queue",
187                 response = Map.class)
188         @Produces(MediaType.APPLICATION_JSON)   
189         public Map<String,Object> unLock(@PathParam("lockreference") String lockId,
190                         @Context HttpServletResponse response){
191                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
192                 boolean voluntaryRelease = true; 
193                 MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
194                 Map<String,Object> returnMap = null;
195                 if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) {
196                         returnMap = new JsonLockResponse(ResultType.SUCCESS).setLock(lockId)
197                                                                 .setLockStatus(mls.getLockStatus()).toMap();
198                 }
199                 if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
200                         returnMap = new JsonLockResponse(ResultType.FAILURE).setLock(lockId)
201                                                                 .setLockStatus(mls.getLockStatus()).toMap();
202                 }
203                 return returnMap;
204         }
205
206         /**
207          * 
208          * @param lockName
209          */
210         @DELETE
211         @Path("/delete/{lockname}")
212         @ApiOperation(value = "Delete Lock", response = Map.class)
213         @Produces(MediaType.APPLICATION_JSON)   
214         public Map<String,Object> deleteLock(@PathParam("lockname") String lockName,
215                         @Context HttpServletResponse response){
216                 response.addHeader(xLatestVersion,MusicUtil.getVersion());
217                 MusicCore.deleteLock(lockName);
218                 return new JsonLockResponse(ResultType.SUCCESS).toMap();
219         }
220
221 }