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 * ====================================================================
23 package org.onap.music.rest;
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;
40 import org.onap.music.authentication.MusicAAFAuthentication;
41 import org.onap.music.authentication.MusicAuthenticator;
42 import org.onap.music.authentication.MusicAuthenticator.Operation;
43 import org.onap.music.datastore.jsonobjects.JsonLeasedLock;
44 import org.onap.music.eelf.logging.EELFLoggerDelegate;
45 import org.onap.music.eelf.logging.format.AppMessages;
46 import org.onap.music.eelf.logging.format.ErrorSeverity;
47 import org.onap.music.eelf.logging.format.ErrorTypes;
48 import org.onap.music.lockingservice.cassandra.MusicLockState;
49 import org.onap.music.main.MusicCore;
50 import org.onap.music.main.MusicUtil;
51 import org.onap.music.main.ResultType;
52 import org.onap.music.main.ReturnType;
53 import org.onap.music.response.jsonobjects.JsonResponse;
55 import io.swagger.annotations.Api;
56 import io.swagger.annotations.ApiOperation;
57 import io.swagger.annotations.ApiParam;
61 @Api(value="Lock Api")
62 public class RestMusicLocksAPI {
64 private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class);
65 private static final String XMINORVERSION = "X-minorVersion";
66 private static final String XPATCHVERSION = "X-patchVersion";
67 private static final String VERSION = "v2";
69 private MusicAuthenticator authenticator = new MusicAAFAuthentication();
72 * Puts the requesting process in the q for this lock. The corresponding
73 * node will be created if it did not already exist
80 @Path("/create/{lockname}")
81 @ApiOperation(value = "Create Lock",
82 notes = "Puts the requesting process in the q for this lock." +
83 " The corresponding lock will be created if it did not already exist." +
84 " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId",
86 @Produces(MediaType.APPLICATION_JSON)
87 public Response createLockReference(
88 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
89 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
90 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
91 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
92 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
93 @ApiParam(value = "Application namespace",
94 required = true) @HeaderParam("ns") String ns) throws Exception{
96 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
97 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
98 if (resultMap.containsKey("Error")) {
99 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
100 response.status(Status.BAD_REQUEST);
101 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
103 String keyspaceName = (String) resultMap.get("keyspace");
104 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
106 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CREATE_LOCKREF)) {
107 return response.status(Status.UNAUTHORIZED)
108 .entity(new JsonResponse(ResultType.FAILURE)
109 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
113 ResultType status = ResultType.SUCCESS;
114 String lockId = MusicCore.createLockReference(lockName);
116 if (lockId == null) {
117 status = ResultType.FAILURE;
118 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
119 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError("Lock Id is null").toMap()).build();
121 return response.status(Status.OK).entity(new JsonResponse(status).setLock(lockId).toMap()).build();
123 EELFLoggerDelegate.mdcRemove("keyspace");
129 * Checks if the node is in the top of the queue and hence acquires the lock
136 @Path("/acquire/{lockreference}")
137 @ApiOperation(value = "Aquire Lock",
138 notes = "Checks if the node is in the top of the queue and hence acquires the lock",
139 response = Map.class)
140 @Produces(MediaType.APPLICATION_JSON)
141 public Response accquireLock(
142 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
143 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
144 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
145 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
146 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
147 @ApiParam(value = "Application namespace",
148 required = true) @HeaderParam("ns") String ns) throws Exception{
150 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
151 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
152 if (resultMap.containsKey("Error")) {
153 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
154 response.status(Status.BAD_REQUEST);
155 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
158 String keyspaceName = (String) resultMap.get("keyspace");
159 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
161 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.ACQUIRE_LOCK)) {
162 return response.status(Status.UNAUTHORIZED)
163 .entity(new JsonResponse(ResultType.FAILURE)
164 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
169 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
170 ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId);
171 if ( lockStatus.getResult().equals(ResultType.SUCCESS)) {
172 response.status(Status.OK);
174 response.status(Status.BAD_REQUEST);
176 return response.entity(new JsonResponse(lockStatus.getResult()).setLock(lockId).setMessage(lockStatus.getMessage()).toMap()).build();
177 } catch (Exception e) {
178 logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR);
179 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap()).build();
182 EELFLoggerDelegate.mdcRemove("keyspace");
190 @Path("/acquire-with-lease/{lockreference}")
191 @ApiOperation(value = "Aquire Lock with Lease", response = Map.class)
192 @Consumes(MediaType.APPLICATION_JSON)
193 @Produces(MediaType.APPLICATION_JSON)
194 public Response accquireLockWithLease(JsonLeasedLock lockObj,
195 @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId,
196 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
197 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
198 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
199 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
200 @ApiParam(value = "Application namespace",
201 required = true) @HeaderParam("ns") String ns) throws Exception{
203 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
204 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
205 if (resultMap.containsKey("Error")) {
206 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
207 response.status(Status.BAD_REQUEST);
208 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
210 String keyspaceName = (String) resultMap.get("keyspace");
211 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
212 resultMap.remove("keyspace");
213 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.ACQUIRE_LOCK)) {
214 return response.status(Status.UNAUTHORIZED)
215 .entity(new JsonResponse(ResultType.FAILURE)
216 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
220 String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$'));
221 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod());
222 if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) {
223 response.status(Status.OK);
225 response.status(Status.BAD_REQUEST);
227 return response.entity(new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName)
228 .setMessage(lockLeaseStatus.getMessage())
229 .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap()).build();
231 EELFLoggerDelegate.mdcRemove("keyspace");
237 @Path("/enquire/{lockname}")
238 @ApiOperation(value = "Get Lock Holder",
239 notes = "Gets the current Lock Holder",
240 response = Map.class)
241 @Produces(MediaType.APPLICATION_JSON)
242 public Response currentLockHolder(
243 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
244 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
245 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
246 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
247 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
248 @ApiParam(value = "Application namespace",
249 required = true) @HeaderParam("ns") String ns) throws Exception{
251 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
252 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
253 if (resultMap.containsKey("Error")) {
254 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
255 response.status(Status.BAD_REQUEST);
256 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
259 String keyspaceName = (String) resultMap.get("keyspace");
260 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
261 resultMap.remove("keyspace");
262 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CURRENT_LOCK)) {
263 return response.status(Status.UNAUTHORIZED)
264 .entity(new JsonResponse(ResultType.FAILURE)
265 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
269 String who = MusicCore.whoseTurnIsIt(lockName);
270 ResultType status = ResultType.SUCCESS;
273 status = ResultType.FAILURE;
274 error = "There was a problem getting the lock holder";
275 logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
276 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
278 return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
280 EELFLoggerDelegate.mdcRemove("keyspace");
286 @ApiOperation(value = "Lock State",
287 notes = "Returns current Lock State and Holder.",
288 response = Map.class)
289 @Produces(MediaType.APPLICATION_JSON)
290 public Response currentLockState(
291 @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName,
292 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
293 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
294 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
295 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
296 @ApiParam(value = "Application namespace",
297 required = true) @HeaderParam("ns") String ns) throws Exception{
299 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
300 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
301 if (resultMap.containsKey("Error")) {
302 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
303 response.status(Status.BAD_REQUEST);
304 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
306 String keyspaceName = (String) resultMap.get("keyspace");
307 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
308 resultMap.remove("keyspace");
309 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.CURRENT_LOCK)) {
310 return response.status(Status.UNAUTHORIZED)
311 .entity(new JsonResponse(ResultType.FAILURE)
312 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
316 String who = MusicCore.whoseTurnIsIt(lockName);
317 ResultType status = ResultType.SUCCESS;
320 status = ResultType.FAILURE;
321 error = "There was a problem getting the lock holder";
322 logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
323 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
325 return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build();
327 EELFLoggerDelegate.mdcRemove("keyspace");
330 //MusicLockState mls = MusicZKCore.getMusicLockState(lockName);
331 // Map<String,Object> returnMap = null;
332 // JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName);
334 // jsonResponse.setError("");
335 // jsonResponse.setMessage("No lock object created yet..");
336 // logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
337 // return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build();
339 // jsonResponse.setStatus(ResultType.SUCCESS);
340 // jsonResponse.setLockStatus(mls.getLockStatus());
341 // jsonResponse.setLockHolder(mls.getLockHolder());
342 // return response.status(Status.OK).entity(jsonResponse.toMap()).build();
349 * deletes the process from the zk queue
355 @Path("/release/{lockreference}")
356 @ApiOperation(value = "Release Lock",
357 notes = "deletes the process from the zk queue",
358 response = Map.class)
359 @Produces(MediaType.APPLICATION_JSON)
360 public Response unLock(@PathParam("lockreference") String lockId,
361 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
362 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
363 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
364 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
365 @ApiParam(value = "Application namespace",
366 required = true) @HeaderParam("ns") String ns) throws Exception{
368 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
369 Map<String, Object> resultMap = MusicCore.validateLock(lockId);
370 if (resultMap.containsKey("Error")) {
371 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
372 response.status(Status.BAD_REQUEST);
373 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
376 String keyspaceName = (String) resultMap.get("keyspace");
377 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
378 resultMap.remove("keyspace");
379 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.DELETE_LOCK)) {
380 return response.status(Status.UNAUTHORIZED)
381 .entity(new JsonResponse(ResultType.FAILURE)
382 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
386 boolean voluntaryRelease = true;
387 MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease);
388 if(mls.getErrorMessage() != null) {
389 resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage());
390 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
391 return response.status(Status.BAD_REQUEST).entity(resultMap).build();
393 Map<String,Object> returnMap = null;
394 if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) {
395 returnMap = new JsonResponse(ResultType.SUCCESS).setLock(lockId)
396 .setLockStatus(mls.getLockStatus()).toMap();
397 response.status(Status.OK);
399 if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
400 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
401 returnMap = new JsonResponse(ResultType.FAILURE).setLock(lockId)
402 .setLockStatus(mls.getLockStatus()).toMap();
403 response.status(Status.BAD_REQUEST);
405 return response.entity(returnMap).build();
407 EELFLoggerDelegate.mdcRemove("keyspace");
417 @Path("/delete/{lockname}")
418 @ApiOperation(value = "Delete Lock", response = Map.class)
419 @Produces(MediaType.APPLICATION_JSON)
420 public Response deleteLock(@PathParam("lockname") String lockName,
421 @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion,
422 @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion,
423 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
424 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
425 @ApiParam(value = "Application namespace",
426 required = true) @HeaderParam("ns") String ns) throws Exception{
428 ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion);
429 Map<String, Object> resultMap = MusicCore.validateLock(lockName);
430 if (resultMap.containsKey("Error")) {
431 logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR);
432 response.status(Status.BAD_REQUEST);
433 return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(resultMap.get("Error"))).toMap()).build();
436 String keyspaceName = (String) resultMap.get("keyspace");
437 EELFLoggerDelegate.mdcPut("keyspace", "( "+keyspaceName+" ) ");
438 resultMap.remove("keyspace");
439 if (!authenticator.authenticateUser(ns, authorization, keyspaceName, aid, Operation.DELETE_LOCK)) {
440 return response.status(Status.UNAUTHORIZED)
441 .entity(new JsonResponse(ResultType.FAILURE)
442 .setError("Unauthorized: Please check username, password and make sure your app is onboarded")
447 MusicCore.deleteLock(lockName);
448 }catch (Exception e) {
449 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build();
451 return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).toMap()).build();
453 EELFLoggerDelegate.mdcRemove("keyspace");