2 * ============LICENSE_START========================================== org.onap.music
3 * =================================================================== Copyright (c) 2017 AT&T
4 * Intellectual Property ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END=============================================
16 * ====================================================================
18 package org.onap.music.rest;
20 import java.util.HashMap;
22 import javax.ws.rs.Consumes;
23 import javax.ws.rs.DELETE;
24 import javax.ws.rs.GET;
25 import javax.ws.rs.HeaderParam;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.PUT;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.core.Context;
32 import javax.ws.rs.core.MediaType;
33 import javax.ws.rs.core.UriInfo;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.ResponseBuilder;
36 import javax.ws.rs.core.Response.Status;
37 // cjcimport javax.servlet.http.HttpServletResponse;
38 import org.onap.music.datastore.jsonobjects.JsonDelete;
39 import org.onap.music.datastore.jsonobjects.JsonInsert;
40 import org.onap.music.datastore.jsonobjects.JsonTable;
41 import org.onap.music.datastore.jsonobjects.JsonUpdate;
42 import org.onap.music.eelf.logging.EELFLoggerDelegate;
44 import org.onap.music.eelf.logging.format.AppMessages;
45 import org.onap.music.eelf.logging.format.ErrorSeverity;
46 import org.onap.music.eelf.logging.format.ErrorTypes;
47 import org.apache.commons.lang3.StringUtils;
48 import org.onap.music.datastore.PreparedQueryObject;
49 import com.datastax.driver.core.ResultSet;
50 import org.onap.music.exceptions.MusicServiceException;
51 import org.onap.music.main.MusicCore;
52 import org.onap.music.main.MusicUtil;
53 import org.onap.music.main.ResultType;
54 // cjc import org.onap.music.main.ReturnType;
55 import org.onap.music.response.jsonobjects.JsonResponse;
56 import io.swagger.annotations.Api;
57 import io.swagger.annotations.ApiOperation;
58 import io.swagger.annotations.ApiParam;
60 // import io.swagger.models.Response;
61 // @Path("/v{version: [0-9]+}/priorityq/")
62 @Path("{version}/priorityq/")
64 public class RestMusicQAPI {
66 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicQAPI.class);
67 // private static String xLatestVersion = "X-latestVersion";
69 * private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class);
70 * private static final String XMINORVERSION = "X-minorVersion"; private static final String
71 * XPATCHVERSION = "X-patchVersion"; private static final String NS = "ns"; private static final
72 * String USERID = "userId"; private static final String PASSWORD = "password";
74 // private static final String VERSION = "v2";
86 @Path("/keyspaces/{keyspace}/{qname}") // is it same as tablename?down
87 @ApiOperation(value = "Create Q", response = String.class)
88 @Consumes(MediaType.APPLICATION_JSON)
89 @Produces(MediaType.APPLICATION_JSON)
93 @Path("/keyspaces/{keyspace}/{qname}")
94 @ApiOperation(value = "", response = Void.class)
95 @Consumes(MediaType.APPLICATION_JSON)
97 public Response createQ(
98 // public Map<String,Object> createQ(
99 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
100 @ApiParam(value = "Minor Version",
101 required = false) @HeaderParam("X-minorVersion") String minorVersion,
102 @ApiParam(value = "Patch Version",
103 required = false) @HeaderParam("X-patchVersion") String patchVersion,
104 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
105 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
106 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
108 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
109 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename)
111 //logger.info(logger, "cjc before start in q 1** major version=" + version);
113 ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
115 Map<String, String> fields = tableObj.getFields();
116 if (fields == null) { // || (!fields.containsKey("order")) ){
117 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
118 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
119 return response.status(Status.BAD_REQUEST)
120 .entity(new JsonResponse(ResultType.FAILURE)
121 .setError("CreateQ/Required table fields are empty or not set").toMap())
125 String primaryKey = tableObj.getPrimaryKey();
126 String partitionKey = tableObj.getPartitionKey();
127 String clusteringKey = tableObj.getClusteringKey();
128 String filteringKey = tableObj.getFilteringKey();
129 String clusteringOrder = tableObj.getClusteringOrder();
131 if(primaryKey == null) {
132 primaryKey = tableObj.getFields().get("PRIMARY KEY");
135 if ((primaryKey == null) && (partitionKey == null)) {
136 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
137 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
138 return response.status(Status.BAD_REQUEST)
139 .entity(new JsonResponse(ResultType.FAILURE)
140 .setError("CreateQ: Partition key cannot be empty").toMap())
144 if ((primaryKey == null) && (clusteringKey == null)) {
145 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
146 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
147 return response.status(Status.BAD_REQUEST)
148 .entity(new JsonResponse(ResultType.FAILURE)
149 .setError("CreateQ: Clustering key cannot be empty").toMap())
153 if (clusteringOrder == null) {
154 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
155 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
156 return response.status(Status.BAD_REQUEST)
157 .entity(new JsonResponse(ResultType.FAILURE)
158 .setError("CreateQ: Clustering Order cannot be empty").toMap())
162 if ((primaryKey!=null) && (partitionKey == null)) {
164 int count1 = StringUtils.countMatches(primaryKey, ')');
165 int count2 = StringUtils.countMatches(primaryKey, '(');
166 if (count1 != count2) {
167 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
168 .setError("CreateQ Error: primary key '(' and ')' do not match, primary key=" + primaryKey)
172 if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(")") +1) == primaryKey.length() ) )
174 if (primaryKey.contains(",") ) {
175 partitionKey= primaryKey.substring(0,primaryKey.indexOf(","));
176 partitionKey=partitionKey.replaceAll("[\\(]+","");
177 clusteringKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index
178 clusteringKey=clusteringKey.replaceAll("[)]+", "");
180 partitionKey=primaryKey;
181 partitionKey=partitionKey.replaceAll("[\\)]+","");
182 partitionKey=partitionKey.replaceAll("[\\(]+","");
186 partitionKey= primaryKey.substring(0,primaryKey.indexOf(')'));
187 partitionKey=partitionKey.replaceAll("[\\(]+","");
189 clusteringKey= primaryKey.substring(primaryKey.indexOf(')'));
190 clusteringKey=clusteringKey.replaceAll("[\\(]+","");
191 clusteringKey=clusteringKey.replaceAll("[\\)]+","");
192 clusteringKey.trim();
193 if (clusteringKey.indexOf(",") == 0) clusteringKey=clusteringKey.substring(1);
194 clusteringKey.trim();
195 if (clusteringKey.equals(",") ) clusteringKey=""; // print error if needed ( ... ),)
199 if (partitionKey.trim().isEmpty()) {
200 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
201 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
202 return response.status(Status.BAD_REQUEST)
203 .entity(new JsonResponse(ResultType.FAILURE)
204 .setError("CreateQ: Partition key cannot be empty").toMap())
208 if (clusteringKey.trim().isEmpty()) {
209 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
210 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
211 return response.status(Status.BAD_REQUEST)
212 .entity(new JsonResponse(ResultType.FAILURE)
213 .setError("CreateQ: Clustering key cannot be empty").toMap())
217 if((filteringKey != null) && (filteringKey.equalsIgnoreCase(partitionKey))) {
218 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
219 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
220 return response.status(Status.BAD_REQUEST)
221 .entity(new JsonResponse(ResultType.FAILURE)
222 .setError("CreateQ: Filtering key cannot be same as Partition Key").toMap())
226 return new RestMusicDataAPI().createTable(version, minorVersion, patchVersion, aid, ns, authorization, tableObj, keyspace, tablename);
237 @Path("/keyspaces/{keyspace}/{qname}/rows")
238 @ApiOperation(value = "", response = Void.class)
239 @Consumes(MediaType.APPLICATION_JSON)
240 @Produces(MediaType.APPLICATION_JSON)
241 // public Map<String, Object> insertIntoQ(
242 public Response insertIntoQ(
243 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
244 @ApiParam(value = "Minor Version",
245 required = false) @HeaderParam("X-minorVersion") String minorVersion,
246 @ApiParam(value = "Patch Version",
247 required = false) @HeaderParam("X-patchVersion") String patchVersion,
248 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
249 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
250 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
252 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
253 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename)
255 // ,@Context HttpServletResponse response) throws Exception {
257 // Map<String, Object> valuesMap = insObj.getValues();
258 // check valuesMap.isEmpty and proceed
259 // if(valuesMap.isEmpty() ) {
260 // response.addHeader(xLatestVersion, MusicUtil.getVersion());
261 ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
262 if (insObj.getValues().isEmpty()) {
263 // response.status(404);
264 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
265 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
266 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
267 .setError("Required HTTP Request body is missing.").toMap()).build();
269 return new RestMusicDataAPI().insertIntoTable(version, minorVersion, patchVersion, aid, ns,
270 authorization, insObj, keyspace, tablename);
283 @Path("/keyspaces/{keyspace}/{qname}/rows")
284 @ApiOperation(value = "updateQ", response = String.class)
285 @Consumes(MediaType.APPLICATION_JSON)
286 @Produces(MediaType.APPLICATION_JSON)
287 public Response updateQ(
288 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
289 @ApiParam(value = "Minor Version",
290 required = false) @HeaderParam("X-minorVersion") String minorVersion,
291 @ApiParam(value = "Patch Version",
292 required = false) @HeaderParam("X-patchVersion") String patchVersion,
293 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
294 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
295 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
296 JsonUpdate updateObj,
297 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
298 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename,
299 @Context UriInfo info) throws Exception {
301 //logger.info(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL,
302 // ErrorTypes.DATAERROR);
303 ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
304 if (updateObj.getValues().isEmpty()) {
305 // response.status(404);
306 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
307 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
308 return response.status(Status.BAD_REQUEST)
309 .entity(new JsonResponse(ResultType.FAILURE).setError(
310 "Required HTTP Request body is missing. JsonUpdate updateObj.getValues() is empty. ")
316 return new RestMusicDataAPI().updateTable(version, minorVersion, patchVersion, aid, ns,
317 authorization,updateObj, keyspace, tablename, info);
332 @Path("/keyspaces/{keyspace}/{qname}/rows")
333 @ApiOperation(value = "deleteQ", response = String.class)
334 @Consumes(MediaType.APPLICATION_JSON)
335 @Produces(MediaType.APPLICATION_JSON)
336 public Response deleteFromQ(
337 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
338 @ApiParam(value = "Minor Version",
339 required = false) @HeaderParam("X-minorVersion") String minorVersion,
340 @ApiParam(value = "Patch Version",
341 required = false) @HeaderParam("X-patchVersion") String patchVersion,
342 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
343 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
344 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
345 // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
346 // @ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
348 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
349 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename,
350 @Context UriInfo info) throws Exception {
351 // added checking as per RestMusicDataAPI
352 ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
353 if (delObj == null) {
354 // response.status(404);
355 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA,
356 ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
357 return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE)
358 .setError("deleteFromQ JsonDelete delObjis empty").toMap()).build();
361 return new RestMusicDataAPI().deleteFromTable(version, minorVersion, patchVersion, aid, ns,
362 authorization, delObj, keyspace, tablename, info);
374 @Path("/keyspaces/{keyspace}/{qname}/peek")
375 @ApiOperation(value = "", response = Map.class)
376 @Produces(MediaType.APPLICATION_JSON)
377 //public Map<String, HashMap<String, Object>> peek(
378 public Response peek(
379 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
380 @ApiParam(value = "Minor Version",
381 required = false) @HeaderParam("X-minorVersion") String minorVersion,
382 @ApiParam(value = "Patch Version",
383 required = false) @HeaderParam("X-patchVersion") String patchVersion,
384 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
385 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
386 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
387 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
388 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename,
389 @Context UriInfo info) throws Exception {
390 int limit =1; //peek must return just the top row
391 Map<String ,String> auth = new HashMap<>();
392 String userId =auth.get(MusicUtil.USERID);
393 String password =auth.get(MusicUtil.PASSWORD);
394 ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion);
396 PreparedQueryObject queryObject = new PreparedQueryObject();
397 if (info.getQueryParameters() == null ) //|| info.getQueryParameters().isEmpty())
398 queryObject.appendQueryString(
399 "SELECT * FROM " + keyspace + "." + tablename + " LIMIT " + limit + ";");
403 queryObject = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion,
404 patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit);
405 } catch (MusicServiceException ex) {
406 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR,
407 ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR);
408 return response.status(Status.BAD_REQUEST)
409 .entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap())
415 ResultSet results = MusicCore.get(queryObject);
416 return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS)
417 .setDataResult(MusicCore.marshallResults(results)).toMap()).build();
418 } catch (MusicServiceException ex) {
419 logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR,
420 ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR);
421 return response.status(Status.BAD_REQUEST)
422 .entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap())
437 @Path("/keyspaces/{keyspace}/{qname}/filter")
438 @ApiOperation(value = "filter", response = Map.class)
439 @Produces(MediaType.APPLICATION_JSON)
440 // public Map<String, HashMap<String, Object>> filter(
441 public Response filter(
442 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
443 @ApiParam(value = "Minor Version",
444 required = false) @HeaderParam("X-minorVersion") String minorVersion,
445 @ApiParam(value = "Patch Version",
446 required = false) @HeaderParam("X-patchVersion") String patchVersion,
447 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
448 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
449 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
450 // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
451 //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
452 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
453 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename,
454 @Context UriInfo info) throws Exception {
457 * PreparedQueryObject query = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion,
458 * patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit); ResultSet results
459 * = MusicCore.get(query); return MusicCore.marshallResults(results);
461 /* Map<String ,String> auth = new HashMap<>();
462 String userId =auth.get(MusicUtil.USERID);
463 String password =auth.get(MusicUtil.PASSWORD);
465 return new RestMusicDataAPI().select(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename, info);// , limit)
477 @ApiOperation(value = "DropQ", response = String.class)
478 @Path("/keyspaces/{keyspace}/{qname}")
479 @Produces(MediaType.APPLICATION_JSON)
480 public Response dropQ(
481 @ApiParam(value = "Major Version", required = true) @PathParam("version") String version,
482 @ApiParam(value = "Minor Version",
483 required = false) @HeaderParam("X-minorVersion") String minorVersion,
484 @ApiParam(value = "Patch Version",
485 required = false) @HeaderParam("X-patchVersion") String patchVersion,
486 @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid,
487 @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns,
488 @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization,
489 // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId,
490 //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password,
491 // cjc JsonTable tabObj,
492 @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace,
493 @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename)
495 // @Context HttpServletResponse response) throws Exception {
496 // tabObj never in use & thus no need to verify
499 return new RestMusicDataAPI().dropTable(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename);