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.lockingservice.MusicLockState;
39 import org.onap.music.main.MusicCore;
40 import org.onap.music.main.MusicUtil;
41 import org.onap.music.response.jsonobjects.JsonLockResponse;
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
46 import io.swagger.annotations.Api;
47 import io.swagger.annotations.ApiOperation;
48 import io.swagger.annotations.ApiParam;
51 @Path("/v{version: [0-9]+}/locks/")
52 @Api(value="Lock Api")
53 public class RestMusicLocksAPI {
55 private static EELFLogger logger = EELFManager.getInstance().getLogger(RestMusicLocksAPI.class);
56 private static String xLatestVersion = "X-latestVersion";
58 * Puts the requesting process in the q for this lock. The corresponding
59 * node will be created in zookeeper if it did not already exist
66 @Path("/create/{lockname}")
67 @ApiOperation(value = "Create Lock",
68 notes = "Puts the requesting process in the q for this lock." +
69 " The corresponding node will be created in zookeeper if it did not already exist." +
70 " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId",
72 @Produces(MediaType.APPLICATION_JSON)
73 public Map<String,Object> createLockReference(
74 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
75 @Context HttpServletResponse response){
76 response.addHeader(xLatestVersion,MusicUtil.getVersion());
77 Boolean status = true;
78 String lockId = MusicCore.createLockReference(lockName);
79 if ( lockId == null ) { status = false; }
80 return new JsonLockResponse(status.toString(),"",lockId).toMap();
85 * Checks if the node is in the top of the queue and hence acquires the lock
91 @Path("/acquire/{lockreference}")
92 @ApiOperation(value = "Aquire Lock",
93 notes = "Checks if the node is in the top of the queue and hence acquires the lock",
95 @Produces(MediaType.APPLICATION_JSON)
96 public Map<String,Object> accquireLock(
97 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
98 @Context HttpServletResponse response){
99 response.addHeader(xLatestVersion,MusicUtil.getVersion());
100 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
101 Boolean lockStatus = MusicCore.acquireLock(lockName,lockId);
102 return new JsonLockResponse(lockStatus.toString(),"",lockId,lockStatus.toString(),"").toMap();
109 @Path("/acquire-with-lease/{lockreference}")
110 @ApiOperation(value = "Aquire Lock with Lease", response = Map.class)
111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 public Map<String,Object> accquireLockWithLease(JsonLeasedLock lockObj,
114 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
115 @Context HttpServletResponse response){
116 response.addHeader(xLatestVersion,MusicUtil.getVersion());
117 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
118 String lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()).toString();
119 return new JsonLockResponse(lockLeaseStatus,"",lockName,lockLeaseStatus,"",String.valueOf(lockObj.getLeasePeriod())).toMap();
124 @Path("/enquire/{lockname}")
125 @ApiOperation(value = "Get Lock Holder",
126 notes = "Gets the current Lock Holder",
127 response = Map.class)
128 @Produces(MediaType.APPLICATION_JSON)
129 public Map<String,Object> currentLockHolder(
130 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
131 @Context HttpServletResponse response){
132 response.addHeader(xLatestVersion,MusicUtil.getVersion());
133 String who = MusicCore.whoseTurnIsIt(lockName);
134 String status = "true";
138 error = "There was a problem getting the lock holder";
140 return new JsonLockResponse(status,error,lockName,"",who).toMap();
145 @ApiOperation(value = "Lock State",
146 notes = "Returns current Lock State and Holder.",
147 response = Map.class)
148 @Produces(MediaType.APPLICATION_JSON)
149 public Map<String,Object> currentLockState(
150 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
151 @Context HttpServletResponse response){
152 response.addHeader(xLatestVersion,MusicUtil.getVersion());
153 MusicLockState mls = MusicCore.getMusicLockState(lockName);
154 Map<String,Object> returnMap = null;
155 JsonLockResponse jsonResponse = new JsonLockResponse("false","",lockName);
157 jsonResponse.setError("");
158 jsonResponse.setMessage("No lock object created yet..");
160 jsonResponse.setStatus("true");
161 jsonResponse.setLockStatus(mls.getLockStatus().toString());
162 jsonResponse.setLockHolder(mls.getLockHolder());
169 * deletes the process from the zk queue
174 @Path("/release/{lockreference}")
175 @ApiOperation(value = "Release Lock",
176 notes = "deletes the process from the zk queue",
177 response = Map.class)
178 @Produces(MediaType.APPLICATION_JSON)
179 public Map<String,Object> unLock(@PathParam("lockreference") String lockId,
180 @Context HttpServletResponse response){
181 response.addHeader(xLatestVersion,MusicUtil.getVersion());
182 boolean voluntaryRelease = true;
183 MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
184 Map<String,Object> returnMap = null;
185 if ( mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED ) {
186 returnMap = new JsonLockResponse("Unlocked","","").toMap();
188 if ( mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
189 returnMap = new JsonLockResponse("Locked","","").toMap();
199 @Path("/delete/{lockname}")
200 @ApiOperation(value = "Delete Lock", response = Map.class)
201 @Produces(MediaType.APPLICATION_JSON)
202 public Map<String,Object> deleteLock(@PathParam("lockname") String lockName,
203 @Context HttpServletResponse response){
204 response.addHeader(xLatestVersion,MusicUtil.getVersion());
205 MusicCore.deleteLock(lockName);
206 return new JsonLockResponse("true","","").toMap();