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.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.HeaderParam;
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.MediaType;
35 import javax.ws.rs.core.Response;
36 import javax.ws.rs.core.Response.ResponseBuilder;
37 import javax.ws.rs.core.Response.Status;
38 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
39 import org.onap.music.eelf.logging.EELFLoggerDelegate;
40 import org.onap.music.eelf.logging.format.AppMessages;
41 import org.onap.music.eelf.logging.format.ErrorSeverity;
42 import org.onap.music.eelf.logging.format.ErrorTypes;
43 import org.onap.music.lockingservice.MusicLockState;
44 import org.onap.music.main.MusicCore;
45 import org.onap.music.main.MusicUtil;
46 import org.onap.music.main.ResultType;
47 import org.onap.music.main.ReturnType;
48 import org.onap.music.response.jsonobjects.JsonResponse;
50 import io.swagger.annotations.Api;
51 import io.swagger.annotations.ApiOperation;
52 import io.swagger.annotations.ApiParam;
56 @Api(value="Lock Api")
57 public class RestMusicLocksAPI {
59 private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
60 private static final String XMINORVERSION = "X-minorVersion";
61 private static final String XPATCHVERSION = "X-patchVersion";
62 private static final String VERSION = "v2";
65 * Puts the requesting process in the q for this lock. The corresponding
66 * node will be created in zookeeper if it did not already exist
73 @Path("/create/{lockname}")
74 @ApiOperation(value = "Create Lock",
75 notes = "Puts the requesting process in the q for this lock." +
76 " The corresponding node will be created in zookeeper if it did not already exist." +
77 " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId",
79 @Produces(MediaType.APPLICATION_JSON)
80 public Response createLockReference(
81 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
82 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
83 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
84 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
85 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
86 @ApiParam(value = "Application namespace",
87 required = true) @HeaderParam("ns") String ns) throws Exception{
88 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
89 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
90 if (resultMap.containsKey("Exception")) {
91 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
92 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
94 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
95 String userId = userCredentials.get(MusicUtil.USERID);
96 String password = userCredentials.get(MusicUtil.PASSWORD);
97 String keyspaceName = (String) resultMap.get("keyspace");
98 resultMap.remove("keyspace");
99 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
100 "createLockReference");
101 if (resultMap.containsKey("aid"))
102 resultMap.remove("aid");
103 if (!resultMap.isEmpty()) {
104 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR);
105 return response.status(Status.UNAUTHORIZED).entity(resultMap).build();
107 ResultType status = ResultType.SUCCESS;
108 String lockId = MusicCore.createLockReference(lockName);
110 if (lockId == null) {
111 status = ResultType.FAILURE;
112 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
113 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError("Lock Id is null").toMap()).build();
115 return response.status(Status.OK).entity(new JsonResponse(status).setLock(lockId).toMap()).build();
120 * Checks if the node is in the top of the queue and hence acquires the lock
127 @Path("/acquire/{lockreference}")
128 @ApiOperation(value = "Aquire Lock",
129 notes = "Checks if the node is in the top of the queue and hence acquires the lock",
130 response = Map.class)
131 @Produces(MediaType.APPLICATION_JSON)
132 public Response accquireLock(
133 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
134 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
135 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
136 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
137 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
138 @ApiParam(value = "Application namespace",
139 required = true) @HeaderParam("ns") String ns) throws Exception{
140 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
141 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
142 if (resultMap.containsKey("Exception")) {
143 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
144 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
146 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
147 String userId = userCredentials.get(MusicUtil.USERID);
148 String password = userCredentials.get(MusicUtil.PASSWORD);
149 String keyspaceName = (String) resultMap.get("keyspace");
150 resultMap.remove("keyspace");
151 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
153 if (resultMap.containsKey("aid"))
154 resultMap.remove("aid");
155 if (!resultMap.isEmpty()) {
156 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
157 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
160 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
161 ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId);
162 if ( lockStatus.getResult().equals(ResultType.SUCCESS)) {
163 response.status(Status.OK);
165 response.status(Status.BAD_REQUEST);
167 return response.entity(new JsonResponse(lockStatus.getResult()).setLock(lockId).setMessage(lockStatus.getMessage()).toMap()).build();
168 } catch (Exception e) {
169 logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
170 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap()).build();
178 @Path("/acquire-with-lease/{lockreference}")
179 @ApiOperation(value = "Aquire Lock with Lease", response = Map.class)
180 @Consumes(MediaType.APPLICATION_JSON)
181 @Produces(MediaType.APPLICATION_JSON)
182 public Response accquireLockWithLease(JsonLeasedLock lockObj,
183 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
184 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
185 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
186 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
187 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
188 @ApiParam(value = "Application namespace",
189 required = true) @HeaderParam("ns") String ns) throws Exception{
190 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
191 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
192 if (resultMap.containsKey("Exception")) {
193 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
194 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
196 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
197 String userId = userCredentials.get(MusicUtil.USERID);
198 String password = userCredentials.get(MusicUtil.PASSWORD);
199 String keyspaceName = (String) resultMap.get("keyspace");
200 resultMap.remove("keyspace");
201 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
202 "accquireLockWithLease");
204 if (resultMap.containsKey("aid"))
205 resultMap.remove("aid");
206 if (!resultMap.isEmpty()) {
207 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
208 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
210 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
211 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
212 if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) {
213 response.status(Status.OK);
215 response.status(Status.BAD_REQUEST);
217 return response.entity(new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName)
218 .setMessage(lockLeaseStatus.getMessage())
219 .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap()).build();
224 @Path("/enquire/{lockname}")
225 @ApiOperation(value = "Get Lock Holder",
226 notes = "Gets the current Lock Holder",
227 response = Map.class)
228 @Produces(MediaType.APPLICATION_JSON)
229 public Response currentLockHolder(
230 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
231 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
232 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
233 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
234 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
235 @ApiParam(value = "Application namespace",
236 required = true) @HeaderParam("ns") String ns) throws Exception{
237 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
238 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
239 if (resultMap.containsKey("Exception")) {
240 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
241 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
243 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
244 String userId = userCredentials.get(MusicUtil.USERID);
245 String password = userCredentials.get(MusicUtil.PASSWORD);
246 String keyspaceName = (String) resultMap.get("keyspace");
247 resultMap.remove("keyspace");
248 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
249 "currentLockHolder");
250 if (resultMap.containsKey("aid"))
251 resultMap.remove("aid");
252 if (!resultMap.isEmpty()) {
253 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
254 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
256 String who = MusicCore.whoseTurnIsIt(lockName);
257 ResultType status = ResultType.SUCCESS;
260 status = ResultType.FAILURE;
261 error = "There was a problem getting the lock holder";
262 logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
263 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
265 return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
270 @ApiOperation(value = "Lock State",
271 notes = "Returns current Lock State and Holder.",
272 response = Map.class)
273 @Produces(MediaType.APPLICATION_JSON)
274 public Response currentLockState(
275 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
276 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
277 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
278 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
279 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
280 @ApiParam(value = "Application namespace",
281 required = true) @HeaderParam("ns") String ns,
282 @ApiParam(value = "userId",
283 required = true) @HeaderParam("userId") String userId,
284 @ApiParam(value = "Password",
285 required = true) @HeaderParam("password") String password) throws Exception{
286 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
287 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
288 if (resultMap.containsKey("Exception")) {
289 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
290 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
292 String keyspaceName = (String) resultMap.get("keyspace");
293 resultMap.remove("keyspace");
294 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
297 if (resultMap.containsKey("aid"))
298 resultMap.remove("aid");
299 if (!resultMap.isEmpty()) {
300 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
301 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
304 MusicLockState mls = MusicCore.getMusicLockState(lockName);
305 Map<String,Object> returnMap = null;
306 JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
308 jsonResponse.setError("");
309 jsonResponse.setMessage("No lock object created yet..");
310 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
311 return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build();
313 jsonResponse.setStatus(ResultType.SUCCESS);
314 jsonResponse.setLockStatus(mls.getLockStatus());
315 jsonResponse.setLockHolder(mls.getLockHolder());
316 return response.status(Status.OK).entity(jsonResponse.toMap()).build();
322 * deletes the process from the zk queue
328 @Path("/release/{lockreference}")
329 @ApiOperation(value = "Release Lock",
330 notes = "deletes the process from the zk queue",
331 response = Map.class)
332 @Produces(MediaType.APPLICATION_JSON)
333 public Response unLock(@PathParam("lockreference") String lockId,
334 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
335 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
336 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
337 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
338 @ApiParam(value = "Application namespace",
339 required = true) @HeaderParam("ns") String ns) throws Exception{
340 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
341 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
342 if (resultMap.containsKey("Exception")) {
343 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
344 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
346 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
347 String userId = userCredentials.get(MusicUtil.USERID);
348 String password = userCredentials.get(MusicUtil.PASSWORD);
349 String keyspaceName = (String) resultMap.get("keyspace");
350 resultMap.remove("keyspace");
351 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
353 if (resultMap.containsKey("aid"))
354 resultMap.remove("aid");
355 if (!resultMap.isEmpty()) {
356 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
357 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
359 boolean voluntaryRelease = true;
360 MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
361 if(mls.getErrorMessage() != null) {
362 resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage());
363 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
364 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
366 Map<String,Object> returnMap = null;
367 if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) {
368 returnMap = new JsonResponse(ResultType.SUCCESS).setLock(lockId)
369 .setLockStatus(mls.getLockStatus()).toMap();
370 response.status(Status.OK);
372 if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
373 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
374 returnMap = new JsonResponse(ResultType.FAILURE).setLock(lockId)
375 .setLockStatus(mls.getLockStatus()).toMap();
376 response.status(Status.BAD_REQUEST);
378 return response.entity(returnMap).build();
387 @Path("/delete/{lockname}")
388 @ApiOperation(value = "Delete Lock", response = Map.class)
389 @Produces(MediaType.APPLICATION_JSON)
390 public Response deleteLock(@PathParam("lockname") String lockName,
391 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
392 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
393 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
394 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
395 @ApiParam(value = "Application namespace",
396 required = true) @HeaderParam("ns") String ns) throws Exception{
397 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
398 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
399 if (resultMap.containsKey("Exception")) {
400 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
401 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
403 Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization);
404 String userId = userCredentials.get(MusicUtil.USERID);
405 String password = userCredentials.get(MusicUtil.PASSWORD);
406 String keyspaceName = (String) resultMap.get("keyspace");
407 resultMap.remove("keyspace");
408 resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid,
410 if (resultMap.containsKey("aid"))
411 resultMap.remove("aid");
412 if (!resultMap.isEmpty()) {
413 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
414 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
417 MusicCore.deleteLock(lockName);
418 }catch (Exception e) {
419 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
421 return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).toMap()).build();