Initial code Import.
[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.lockingservice.MusicLockState;
39 import org.onap.music.main.MusicCore;
40 import org.onap.music.main.MusicUtil;
41 import org.onap.music.response.jsonobjects.JsonLockResponse;
42
43 import com.att.eelf.configuration.EELFLogger;
44 import com.att.eelf.configuration.EELFManager;
45
46 import io.swagger.annotations.Api;
47 import io.swagger.annotations.ApiOperation;
48 import io.swagger.annotations.ApiParam;
49
50
51 @Path("/v{version: [0-9]+}/locks/")
52 @Api(value="Lock Api")
53 public class RestMusicLocksAPI {
54
55         private static EELFLogger logger = EELFManager.getInstance().getLogger(RestMusicLocksAPI.class);
56         private static String xLatestVersion = "X-latestVersion";
57         /**
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
60          * 
61          * @param lockName
62          * @return
63          */
64
65         @POST
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",
71                 response = Map.class)
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();
81         }
82
83         /**
84          * 
85          * Checks if the node is in the top of the queue and hence acquires the lock
86          * 
87          * @param lockId
88          * @return
89          */
90         @GET
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",
94                 response = Map.class)
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();
103         }
104         
105
106
107         
108         @POST
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(); 
120         } 
121         
122
123         @GET
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";
135                 String error = "";
136                 if ( who == null ) { 
137                         status = "false"; 
138                         error = "There was a problem getting the lock holder";
139                 }
140                 return new JsonLockResponse(status,error,lockName,"",who).toMap();
141         }
142
143         @GET
144         @Path("/{lockname}")
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);
156                 if(mls == null) {
157                         jsonResponse.setError("");
158                         jsonResponse.setMessage("No lock object created yet..");
159                 } else { 
160                         jsonResponse.setStatus("true");
161                         jsonResponse.setLockStatus(mls.getLockStatus().toString());
162                         jsonResponse.setLockHolder(mls.getLockHolder());
163                 } 
164                 return returnMap;
165         }
166
167         /**
168          * 
169          * deletes the process from the zk queue
170          * 
171          * @param lockId
172          */
173         @DELETE
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();
187                 }
188                 if ( mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) {
189                         returnMap = new JsonLockResponse("Locked","","").toMap();
190                 }
191                 return returnMap;
192         }
193
194         /**
195          * 
196          * @param lockName
197          */
198         @DELETE
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();
207         }
208
209 }