2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.rest;
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;
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;
48 import com.att.eelf.configuration.EELFLogger;
49 import com.att.eelf.configuration.EELFManager;
51 import io.swagger.annotations.Api;
52 import io.swagger.annotations.ApiOperation;
53 import io.swagger.annotations.ApiParam;
56 @Path("/v{version: [0-9]+}/locks/")
57 @Api(value="Lock Api")
58 public class RestMusicLocksAPI {
60 private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
61 private static String xLatestVersion = "X-latestVersion";
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
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",
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();
91 * Checks if the node is in the top of the queue and hence acquires the lock
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();
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();
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;
146 status = ResultType.FAILURE;
147 error = "There was a problem getting the lock holder";
149 return new JsonLockResponse(status).setError(error)
150 .setLock(lockName).setLockHolder(who).toMap();
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);
167 jsonResponse.setError("");
168 jsonResponse.setMessage("No lock object created yet..");
170 jsonResponse.setStatus(ResultType.SUCCESS);
171 jsonResponse.setLockStatus(mls.getLockStatus());
172 jsonResponse.setLockHolder(mls.getLockHolder());
179 * deletes the process from the zk queue
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();
199 if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
200 returnMap = new JsonLockResponse(ResultType.FAILURE).setLock(lockId)
201 .setLockStatus(mls.getLockStatus()).toMap();
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();