diff options
Diffstat (limited to 'src/main/java/org/onap/music/rest')
8 files changed, 0 insertions, 3912 deletions
diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java deleted file mode 100755 index 7fb7b1d5..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java +++ /dev/null @@ -1,958 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Modifications Copyright (C) 2018 IBM. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - - -import java.nio.ByteBuffer; -import java.nio.charset.Charset; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.UUID; - -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.core.Response.Status; - -import org.codehaus.jackson.map.ObjectMapper; -import org.mindrot.jbcrypt.BCrypt; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.datastore.jsonobjects.JSONCallbackResponse; -import org.onap.music.datastore.jsonobjects.JSONObject; -import org.onap.music.datastore.jsonobjects.JsonCallback; -import org.onap.music.datastore.jsonobjects.JsonNotification; -import org.onap.music.datastore.jsonobjects.JsonNotifyClientResponse; -import org.onap.music.datastore.jsonobjects.JsonOnboard; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -//import org.onap.music.main.CacheAccess; -import org.onap.music.main.CachingUtil; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.response.jsonobjects.JsonResponse; - -import com.datastax.driver.core.ColumnDefinitions; -import com.datastax.driver.core.ColumnDefinitions.Definition; -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.TableMetadata; -import com.datastax.driver.core.exceptions.InvalidQueryException; -import com.sun.jersey.api.client.Client; -import com.sun.jersey.api.client.ClientResponse; -import com.sun.jersey.api.client.WebResource; -import com.sun.jersey.api.client.config.ClientConfig; -import com.sun.jersey.api.client.config.DefaultClientConfig; -import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.client.urlconnection.HTTPSProperties; -import com.sun.jersey.core.util.Base64; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; - -@Path("/v2/admin") -// @Path("/v{version: [0-9]+}/admin") -// @Path("/admin") -@Api(value = "Admin Api", hidden = true) -public class RestMusicAdminAPI { - private static EELFLoggerDelegate logger = - EELFLoggerDelegate.getLogger(RestMusicAdminAPI.class); - /* - * API to onboard an application with MUSIC. This is the mandatory first step. - * - */ - @POST - @Path("/onboardAppWithMusic") - @ApiOperation(value = "Onboard application", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response onboardAppWithMusic(JsonOnboard jsonObj) throws Exception { - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - Map<String, Object> resultMap = new HashMap<>(); - String appName = jsonObj.getAppname(); - String userId = jsonObj.getUserId(); - String isAAF = jsonObj.getIsAAF(); - String password = jsonObj.getPassword(); - if (appName == null || userId == null || isAAF == null || password == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, - ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - resultMap.put("Exception", - "Unauthorized: Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing."); - return Response.status(Status.UNAUTHORIZED).entity(resultMap).build(); - } - - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(pQuery); - if (!rs.all().isEmpty()) { - resultMap.put("Exception", "Application " + appName - + " has already been onboarded. Please contact admin."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - pQuery = new PreparedQueryObject(); - String uuid = CachingUtil.generateUUID(); - pQuery.appendQueryString( - "INSERT INTO admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) VALUES (?,?,?,?,?,?,?)"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - - String returnStr = MusicCore.eventualPut(pQuery).toString(); - if (returnStr.contains("Failure")) { - resultMap.put("Exception", - "Oops. Something wrong with onboarding process. Please retry later or contact admin."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - CachingUtil.updateisAAFCache(appName, isAAF); - resultMap.put("Success", "Your application " + appName + " has been onboarded with MUSIC."); - resultMap.put("Generated AID", uuid); - return Response.status(Status.OK).entity(resultMap).build(); - } - - - @POST - @Path("/search") - @ApiOperation(value = "Search Onboard application", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response getOnboardedInfoSearch(JsonOnboard jsonObj) throws Exception { - Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - String appName = jsonObj.getAppname(); - String uuid = jsonObj.getAid(); - String isAAF = jsonObj.getIsAAF(); - - if (appName == null && uuid == null && isAAF == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, - ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - resultMap.put("Exception", - "Unauthorized: Please check the request parameters. Enter atleast one of the following parameters: appName(ns), aid, isAAF."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - PreparedQueryObject pQuery = new PreparedQueryObject(); - String cql = "select uuid, keyspace_name from admin.keyspace_master where "; - if (appName != null) - cql = cql + "application_name = ? AND "; - if (uuid != null) - cql = cql + "uuid = ? AND "; - if (isAAF != null) - cql = cql + "is_aaf = ?"; - - if (cql.endsWith("AND ")) - cql = cql.trim().substring(0, cql.length() - 4); - logger.info("Query in callback is: " + cql); - cql = cql + " allow filtering"; - pQuery.appendQueryString(cql); - if (appName != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - if (uuid != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - if (isAAF != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), - Boolean.parseBoolean(isAAF))); - ResultSet rs = MusicCore.get(pQuery); - Iterator<Row> it = rs.iterator(); - while (it.hasNext()) { - Row row = (Row) it.next(); - resultMap.put(row.getUUID("uuid").toString(), row.getString("keyspace_name")); - } - if (resultMap.isEmpty()) { - if (uuid != null) { - resultMap.put("Exception", - "Please make sure Aid is correct and application is onboarded."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } else { - resultMap.put("Exception", - "Application is not onboarded. Please make sure all the information is correct."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - return Response.status(Status.OK).entity(resultMap).build(); - } - - - @DELETE - @Path("/onboardAppWithMusic") - @ApiOperation(value = "Delete Onboard application", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response deleteOnboardApp(JsonOnboard jsonObj) throws Exception { - Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - String appName = jsonObj.getAppname(); - String aid = jsonObj.getAid(); - PreparedQueryObject pQuery = new PreparedQueryObject(); - String consistency = MusicUtil.EVENTUAL;; - if (appName == null && aid == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - resultMap.put("Exception", "Please make sure either appName(ns) or Aid is present"); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - if (aid != null) { - pQuery.appendQueryString( - "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), - UUID.fromString(aid))); - Row row = MusicCore.get(pQuery).one(); - if (row != null) { - String ks = row.getString("keyspace_name"); - if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) { - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";"); - MusicCore.nonKeyRelatedPut(queryObject, consistency); - } - } - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ? IF EXISTS"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), - UUID.fromString(aid))); - ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); - if (result == ResultType.SUCCESS) { - resultMap.put("Success", "Your application has been deleted successfully"); - } else { - resultMap.put("Exception", - "Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded"); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - - } - return Response.status(Status.OK).entity(resultMap).build(); - } - - pQuery.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(pQuery); - List<Row> rows = rs.all(); - String uuid = null; - if (rows.size() == 0) { - resultMap.put("Exception", - "Application not found. Please make sure Application exists."); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } else if (rows.size() == 1) { - uuid = rows.get(0).getUUID("uuid").toString(); - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "SELECT keyspace_name FROM admin.keyspace_master WHERE uuid = ?"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), - UUID.fromString(uuid))); - Row row = MusicCore.get(pQuery).one(); - String ks = row.getString("keyspace_name"); - if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) { - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("DROP KEYSPACE " + ks + ";"); - MusicCore.nonKeyRelatedPut(queryObject, consistency); - } - - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), - UUID.fromString(uuid))); - MusicCore.eventualPut(pQuery); - resultMap.put("Success", "Your application " + appName + " has been deleted."); - return Response.status(Status.OK).entity(resultMap).build(); - } else { - resultMap.put("Failure", - "More than one Aid exists for this application, so please provide Aid."); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MULTIPLERECORDS, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - - - @PUT - @Path("/onboardAppWithMusic") - @ApiOperation(value = "Update Onboard application", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response updateOnboardApp(JsonOnboard jsonObj) throws Exception { - Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - String aid = jsonObj.getAid(); - String appName = jsonObj.getAppname(); - String userId = jsonObj.getUserId(); - String isAAF = jsonObj.getIsAAF(); - String password = jsonObj.getPassword(); - String consistency = "eventual"; - PreparedQueryObject pQuery; - - if (aid == null) { - resultMap.put("Exception", "Please make sure Aid is present"); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - if (appName == null && userId == null && password == null && isAAF == null) { - resultMap.put("Exception", - "No parameters found to update. Please update atleast one parameter."); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - if (appName != null) { - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select uuid from admin.keyspace_master where application_name = ? allow filtering"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - ResultSet rs = MusicCore.get(pQuery); - if (!rs.all().isEmpty()) { - resultMap.put("Exception", "Application " + appName - + " has already been onboarded. Please contact admin."); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.ALREADYEXIST, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - - pQuery = new PreparedQueryObject(); - StringBuilder preCql = new StringBuilder("UPDATE admin.keyspace_master SET "); - if (appName != null) - preCql.append(" application_name = ?,"); - if (userId != null) - preCql.append(" username = ?,"); - if (password != null) - preCql.append(" password = ?,"); - if (isAAF != null) - preCql.append(" is_aaf = ?,"); - preCql.deleteCharAt(preCql.length() - 1); - preCql.append(" WHERE uuid = ? IF EXISTS"); - pQuery.appendQueryString(preCql.toString()); - if (appName != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - if (userId != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - if (password != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), BCrypt.hashpw(password, BCrypt.gensalt()))); - if (isAAF != null) - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), UUID.fromString(aid))); - ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency); - - if (result == ResultType.SUCCESS) { - resultMap.put("Success", "Your application has been updated successfully"); - } else { - resultMap.put("Exception", - "Oops. Something went wrong. Please make sure Aid is correct and application is onboarded"); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - return Response.status(Status.OK).entity(resultMap).build(); - } - - Client client = Client.create(); - ObjectMapper mapper = new ObjectMapper(); - - @POST - @Path("/callbackOps") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - public Response callbackOps(final JSONObject inputJsonObj) { - // {"keyspace":"conductor","full_table":"conductor.plans","changeValue":{"conductor.plans.status":"Who??","position":"3"},"operation":"update","table_name":"plans","primary_key":"3"} - Map<String, Object> resultMap = new HashMap<>(); - new Thread(new Runnable() { - public void run() { - makeAsyncCall(inputJsonObj); - } - }).start(); - - return Response.status(Status.OK).entity(resultMap).build(); - } - - private Response makeAsyncCall(JSONObject inputJsonObj) { - - Map<String, Object> resultMap = new HashMap<>(); - try { - logger.info(EELFLoggerDelegate.applicationLogger, "Got notification: " + inputJsonObj.getData()); - logger.info("Got notification: " + inputJsonObj.getData()); - String dataStr = inputJsonObj.getData(); - JSONCallbackResponse jsonResponse = mapper.readValue(dataStr, JSONCallbackResponse.class); - String operation = jsonResponse.getOperation(); - Map<String, String> changeValueMap = jsonResponse.getChangeValue(); - String primaryKey = jsonResponse.getPrimary_key(); - String ksTableName = jsonResponse.getFull_table(); //conductor.plans - if(ksTableName.equals("admin.notification_master")) { - CachingUtil.updateCallbackNotifyList(new ArrayList<String>()); - return Response.status(Status.OK).entity(resultMap).build(); - } - List<String> inputUpdateList = jsonResponse.getUpdateList(); - /*String field_value = changeValueMap.get("field_value"); - if(field_value == null) - field_value = jsonResponse.getFull_table();*/ - String field_value = null; - List<String> notifiyList = CachingUtil.getCallbackNotifyList(); - if(notifiyList == null || notifiyList.isEmpty()) { - logger.info("Is cache empty? reconstructing Object from cache.."); - constructJsonCallbackFromCache(); - } - notifiyList = CachingUtil.getCallbackNotifyList(); - JsonCallback baseRequestObj = null; - - if("update".equals(operation)) { - for(String element: inputUpdateList) { - baseRequestObj = CachingUtil.getCallBackCache(element); - if(baseRequestObj != null) { - logger.info("Found the element that was changed... "+element); - break; - } - } - - List<String> updateList = jsonResponse.getUpdateList(); - //logger.info("update list from trigger: "+updateList); - for(String element : updateList) { - if(notifiyList.contains(element)) { - logger.info("Found the notifyOn property: "+element); - field_value = element; - break; - } - } - if(baseRequestObj == null || field_value == null) { - for(String element: inputUpdateList) { - String[] elementArr = element.split(":"); - String newElement = null; - if(elementArr.length >= 2) { - newElement = elementArr[0]+":"+elementArr[1]; - } - baseRequestObj = CachingUtil.getCallBackCache(newElement); - if(baseRequestObj != null) { - logger.info("Found the element that was changed... "+newElement); - break; - } - } - for(String element : updateList) { - String[] elementArr = element.split(":"); - String newElement = null; - if(elementArr.length >= 2) { - newElement = elementArr[0]+":"+elementArr[1]; - } - if(notifiyList.contains(newElement)) { - logger.info("Found the notifyOn property: "+newElement); - field_value = newElement; - break; - } - } - } - } else { - field_value = jsonResponse.getFull_table(); - baseRequestObj = CachingUtil.getCallBackCache(field_value); - } - - if(baseRequestObj == null || field_value == null) { - resultMap.put("Exception", - "Oops. Something went wrong. Please make sure Callback properties are onboarded."); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.INCORRECTDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - logger.info(EELFLoggerDelegate.applicationLogger, "Going through list: "+operation+ " && List: "+jsonResponse.getUpdateList()); - - String key = "admin" + "." + "notification_master" + "." + baseRequestObj.getUuid(); - String lockId = MusicCore.createLockReference(key); - ReturnType lockAcqResult = MusicCore.acquireLock(key, lockId); - if(! lockAcqResult.getResult().toString().equals("SUCCESS")) { - logger.error(EELFLoggerDelegate.errorLogger, "Some other node is notifying the caller..: "); - } - - logger.info(EELFLoggerDelegate.applicationLogger, operation+ ": Operation :: changeValue: "+changeValueMap); - if(operation.equals("update")) { - String notifyWhenChangeIn = baseRequestObj.getNotifyWhenChangeIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenChangeIn)) { - notifyCallBackAppl(jsonResponse, baseRequestObj); - } - } - } else if(operation.equals("delete")) { - String notifyWhenDeletesIn = baseRequestObj.getNotifyWhenDeletesIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenDeletesIn)) { - notifyCallBackAppl(jsonResponse, baseRequestObj); - } - } - } else if(operation.equals("insert")) { - String notifyWhenInsertsIn = baseRequestObj.getNotifyWhenInsertsIn(); // conductor.plans.status - if(null!=field_value) { - if(field_value.equals(notifyWhenInsertsIn)) { - notifyCallBackAppl(jsonResponse, baseRequestObj); - } - } - } - MusicCore.releaseLock(lockId, true); - } catch(Exception e) { - e.printStackTrace(); - logger.error(EELFLoggerDelegate.errorLogger, "Exception while notifying...."+e.getMessage()); - } - logger.info(EELFLoggerDelegate.applicationLogger, "callback is completed. Notification was sent from Music..."); - return Response.status(Status.OK).entity(resultMap).build(); - } - - private void notifyCallBackAppl(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) throws Exception { - int notifytimeout = MusicUtil.getNotifyTimeout(); - int notifyinterval = MusicUtil.getNotifyInterval(); - String endpoint = baseRequestObj.getApplicationNotificationEndpoint(); - String username = baseRequestObj.getApplicationUsername(); - String password = baseRequestObj.getApplicationPassword(); - JsonNotification jsonNotification = constructJsonNotification(jsonResponse, baseRequestObj); - jsonNotification.setPassword("************"); - jsonNotification.setOperation_type(jsonResponse.getOperation()); - logger.info(EELFLoggerDelegate.applicationLogger, "Notification Response sending is: "+jsonNotification); - logger.info("Notification Response sending is: "+jsonNotification); - jsonNotification.setPassword(baseRequestObj.getApplicationPassword()); - WebResource webResource = client.resource(endpoint); - String authData = username+":"+password; - byte[] plainCredsBytes = authData.getBytes(); - byte[] base64CredsBytes = Base64.encode(plainCredsBytes); - String base64Creds = new String(base64CredsBytes); - ClientConfig config = new DefaultClientConfig(); - config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - ClientResponse response = null; - WebResource service = null; - boolean ok = false; - try { - Client client = Client.create(config); - TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager(){ - public X509Certificate[] getAcceptedIssuers(){return null;} - public void checkClientTrusted(X509Certificate[] certs, String authType){} - public void checkServerTrusted(X509Certificate[] certs, String authType){} - }}; - - // Install the all-trusting trust manager - try { - SSLContext sc = SSLContext.getInstance("TLS"); - sc.init(null, trustAllCerts, new SecureRandom()); - HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); - } catch (Exception e) { - ; - } - - try { - SSLContext sslcontext = SSLContext.getInstance( "TLS" ); - sslcontext.init( null, null, null ); - Map<String, Object> properties = config.getProperties(); - HTTPSProperties httpsProperties = new HTTPSProperties( - new HostnameVerifier() { - @Override - public boolean verify( String s, SSLSession sslSession ) { - return true; - } - }, sslcontext - ); - properties.put( HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties ); - HttpsURLConnection.setDefaultHostnameVerifier (new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; - } - }); - Client.create( config ); - } - catch ( KeyManagementException | NoSuchAlgorithmException e ) { - throw new RuntimeException( e ); - } - - service = client.resource(endpoint); - - response = service.header("Authorization", "Basic " + base64Creds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, jsonNotification); - - } catch (Exception chf) { - logger.info(EELFLoggerDelegate.applicationLogger, "Is Service down?"); - logger.info("An Exception occured while notifying. "+chf+ " : "+chf.getMessage() +" ...Retrying for: "+notifytimeout); - } - if(response != null && response.getStatus() == 200) ok = true; - if(!ok) { - long now= System.currentTimeMillis(); - long end = now+notifytimeout; - while(! ok) { - logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback for "+notifytimeout+"ms"); - logger.info("retrying since error in notifying callback.. response status: "+ (response == null ? "404" : response.getStatus())); - try { - ok = true; - response = service.header("Authorization", "Basic " + base64Creds).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON) - .post(ClientResponse.class, jsonNotification); - if(response != null && response.getStatus() == 200) ok = true; - else if(System.currentTimeMillis() < end) { - try{ Thread.sleep(notifyinterval); } catch(Exception e1) {} - ok = false; - } - }catch (Exception e) { - logger.info(EELFLoggerDelegate.applicationLogger, "Retry until "+(end-System.currentTimeMillis())); - if(response == null && System.currentTimeMillis() < end) ok = false; - else ok = true; - try{ Thread.sleep(notifyinterval); } catch(Exception e1) {} - } - } - } - - if(response == null) { - logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond.."); - return; - } - try { - JsonNotifyClientResponse responseStr = response.getEntity(JsonNotifyClientResponse.class); - logger.info(EELFLoggerDelegate.applicationLogger, "Response from Notified client: "+responseStr); - logger.info("Response from Notified client: "+responseStr); - } catch(Exception e) { - logger.info("Exception while reading response from Caller"); - logger.error("Exception while reading response from Caller"); - logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond.."); - } - - /*ClientResponse response = null; - try { - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - } catch (com.sun.jersey.api.client.ClientHandlerException chf) { - boolean ok = false; - logger.info(EELFLoggerDelegate.applicationLogger, "Is Service down?"); - long now= System.currentTimeMillis(); - long end = now+notifytimeout; - while(! ok) { - logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback.."); - try { - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - if(response.getStatus() == 200) ok = true; - }catch (Exception e) { - logger.info(EELFLoggerDelegate.applicationLogger, "Retry until "+(end-System.currentTimeMillis())); - if(response == null && System.currentTimeMillis() < end) ok = false; - else ok = true; - try{ Thread.sleep(notifyinterval); } catch(Exception e1) {} - } - } - } - if(response == null) { - logger.error(EELFLoggerDelegate.errorLogger, "Can NOT notify the caller as caller failed to respond.."); - return; - } - JsonNotifyClientResponse responseStr = response.getEntity(JsonNotifyClientResponse.class); - logger.info(EELFLoggerDelegate.applicationLogger, "Response from Notified client: "+responseStr); - - if(response.getStatus() != 200){ - long now= System.currentTimeMillis(); - long end = now+30000; - while(response.getStatus() != 200 && System.currentTimeMillis() < end) { - logger.info(EELFLoggerDelegate.applicationLogger, "retrying since error in notifying callback.."); - response = webResource.header("Authorization", "Basic " + base64Creds).accept("application/json").type("application/json") - .post(ClientResponse.class, jsonNotification); - } - logger.info(EELFLoggerDelegate.applicationLogger, "Exception while notifying.. "+response.getStatus()); - }*/ - } - - private JsonNotification constructJsonNotification(JSONCallbackResponse jsonResponse, JsonCallback baseRequestObj) { - - JsonNotification jsonNotification = new JsonNotification(); - try { - jsonNotification.setNotify_field(baseRequestObj.getNotifyOn()); - jsonNotification.setEndpoint(baseRequestObj.getApplicationNotificationEndpoint()); - jsonNotification.setUsername(baseRequestObj.getApplicationUsername()); - jsonNotification.setPassword(baseRequestObj.getApplicationPassword()); - String pkValue = jsonResponse.getPrimary_key(); - - String[] fullNotifyArr = baseRequestObj.getNotifyOn().split(":"); - - String[] tableArr = fullNotifyArr[0].split("\\."); - TableMetadata tableInfo = MusicCore.returnColumnMetadata(tableArr[0], tableArr[1]); - DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType(); - String primaryId = tableInfo.getPrimaryKey().get(0).getName(); - - Map<String, String> responseBodyMap = baseRequestObj.getResponseBody(); - for (Entry<String, String> entry : new HashSet<>(responseBodyMap.entrySet())) { - String trimmed = entry.getKey().trim(); - if (!trimmed.equals(entry.getKey())) { - responseBodyMap.remove(entry.getKey()); - responseBodyMap.put(trimmed, entry.getValue()); - } - } - - Set<String> keySet = responseBodyMap.keySet(); - Map<String, String> newMap = new HashMap<>(); - if(responseBodyMap.size() == 1 && responseBodyMap.containsKey("")) { - jsonNotification.setResponse_body(newMap); - return jsonNotification; - } - logger.info(EELFLoggerDelegate.applicationLogger, "responseBodyMap is not blank: "+responseBodyMap); - String cql = "select *"; - /*for(String keys: keySet) { - cql = cql + keys + ","; - }*/ - //cql = cql.substring(0, cql.length()-1); - cql = cql + " FROM "+fullNotifyArr[0]+" WHERE "+primaryId+" = ?"; - logger.info(EELFLoggerDelegate.applicationLogger, "CQL in constructJsonNotification: "+cql); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString(cql); - pQuery.addValue(MusicUtil.convertToActualDataType(primaryIdType, pkValue)); - Row row = MusicCore.get(pQuery).one(); - if(row != null) { - ColumnDefinitions colInfo = row.getColumnDefinitions(); - for (Definition definition : colInfo) { - String colName = definition.getName(); - if(keySet.contains(colName)) { - DataType colType = definition.getType(); - Object valueObj = MusicCore.getDSHandle().getColValue(row, colName, colType); - Object valueString = MusicUtil.convertToActualDataType(colType, valueObj); - logger.info(colName+" : "+valueString); - newMap.put(colName, valueString.toString()); - keySet.remove(colName); - } - } - } - if(! keySet.isEmpty()) { - Iterator<String> iterator = keySet.iterator(); - while (iterator.hasNext()) { - String element = iterator.next(); - newMap.put(element,"COLUMN_NOT_FOUND"); - } - } - - if("delete".equals(jsonResponse.getOperation()) || newMap.isEmpty()) { - newMap.put(primaryId, pkValue); - } - jsonNotification.setResponse_body(newMap); - } catch(Exception e) { - e.printStackTrace(); - } - return jsonNotification; - } - - - - private void constructJsonCallbackFromCache() throws Exception{ - PreparedQueryObject pQuery = new PreparedQueryObject(); - JsonCallback jsonCallback = null; - List<String> notifyList = new java.util.ArrayList<>(); - String cql = - "select id, endpoint_userid, endpoint_password, notify_to_endpoint, notify_insert_on," - + " notify_delete_on, notify_update_on, request, notifyon from admin.notification_master allow filtering"; - pQuery.appendQueryString(cql); - //pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), fullTable)); - - ResultSet rs = MusicCore.get(pQuery); - Iterator<Row> it = rs.iterator(); - while (it.hasNext()) { - Row row = (Row) it.next(); - String endpoint = row.getString("notify_to_endpoint"); - String username = row.getString("endpoint_userid"); - ByteBuffer passwordBytes = row.getBytes("endpoint_password"); - String insert = row.getString("notify_insert_on"); - String delete = row.getString("notify_delete_on"); - String update = row.getString("notify_update_on"); - String request = row.getString("request"); - String notifyon = row.getString("notifyon"); - String uuid = row.getUUID("id").toString(); - notifyList.add(notifyon); - jsonCallback = new JsonCallback(); - jsonCallback.setApplicationNotificationEndpoint(endpoint); - - Charset charset = Charset.forName("ISO-8859-1"); - String decodedPwd = charset.decode(passwordBytes).toString(); - jsonCallback.setApplicationPassword(decodedPwd); - jsonCallback.setApplicationUsername(username); - jsonCallback.setNotifyOn(notifyon); - jsonCallback.setNotifyWhenInsertsIn(insert); - jsonCallback.setNotifyWhenDeletesIn(delete); - jsonCallback.setNotifyWhenChangeIn(update); - jsonCallback.setUuid(uuid); - logger.info(EELFLoggerDelegate.applicationLogger, "From DB. Saved request_body: "+request); - request = request.substring(1, request.length()-1); - String[] keyValuePairs = request.split(","); - Map<String,String> responseBody = new HashMap<>(); - - for(String pair : keyValuePairs) { - String[] entry = pair.split("="); - String val = ""; - if(entry.length == 2) - val = entry[1]; - responseBody.put(entry[0], val); - } - logger.info(EELFLoggerDelegate.applicationLogger, "After parsing. Saved request_body: "+responseBody); - jsonCallback.setResponseBody(responseBody); - logger.info(EELFLoggerDelegate.applicationLogger, "Updating Cache with updateCallBackCache: "+notifyon+ " :::: "+jsonCallback); - CachingUtil.updateCallBackCache(notifyon, jsonCallback); - } - CachingUtil.updateCallbackNotifyList(notifyList); - } - - @POST - @Path("/onboardCallback") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - public Response addCallback(JsonNotification jsonNotification) { - Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - String username = jsonNotification.getUsername(); - String password = jsonNotification.getPassword(); - String endpoint = jsonNotification.getEndpoint(); - String notify_field = jsonNotification.getNotify_field(); - Map<String, String> responseBody = jsonNotification.getResponse_body(); - String triggerName = jsonNotification.getTriggerName(); - if(triggerName == null || triggerName.length() == 0) - triggerName = "MusicTrigger"; - - /*JsonCallback callBackCache = CachingUtil.getCallBackCache(notify_field); - if(callBackCache != null) { - resultMap.put("Exception", "The notification property has already been onboarded."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - }*/ - - String[] allFields = notify_field.split(":"); - String inserts = null; - String updates = null; - String deletes = null; - String tableName = null; - if(allFields.length >= 2) { - inserts = updates = notify_field; - } else if(allFields.length == 1) { - inserts = deletes = notify_field;; - } - tableName = allFields[0]; - String cql = "CREATE TRIGGER IF NOT EXISTS musictrigger ON "+tableName+" Using '"+triggerName+"'"; - PreparedQueryObject pQuery = new PreparedQueryObject(); - - String uuid = CachingUtil.generateUUID(); - try { - pQuery.appendQueryString( - "INSERT INTO admin.notification_master (id, endpoint_userid, endpoint_password, notify_to_endpoint, " - + "notifyon, notify_insert_on, notify_delete_on, notify_update_on, request, current_notifier) VALUES (?,?,?,?,?,?,?,?,?,?)"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), username)); - Charset charset = Charset.forName("ISO-8859-1"); - ByteBuffer decodedPwd = charset.encode(password); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.blob(), decodedPwd.array())); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), endpoint)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), notify_field)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), inserts)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), deletes)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), updates)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), responseBody)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicCore.getMyHostId())); - MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL); - JsonCallback jsonCallback = new JsonCallback(); - jsonCallback.setUuid(uuid); - jsonCallback.setApplicationNotificationEndpoint(endpoint); - jsonCallback.setApplicationPassword(password); - jsonCallback.setApplicationUsername(username); - jsonCallback.setNotifyOn(notify_field); - jsonCallback.setNotifyWhenChangeIn(updates); - jsonCallback.setNotifyWhenDeletesIn(deletes); - jsonCallback.setNotifyWhenInsertsIn(inserts); - jsonCallback.setResponseBody(responseBody); - CachingUtil.updateCallBackCache(notify_field, jsonCallback); - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString(cql); - ResultType nonKeyRelatedPut = MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL); - logger.info(EELFLoggerDelegate.applicationLogger, "Created trigger"); - //callBackCache.put(jsonCallback.getApplicationName(), jsonMap); - } catch (InvalidQueryException e) { - logger.error(EELFLoggerDelegate.errorLogger,"Exception callback_api table not configured."+e.getMessage()); - resultMap.put("Exception", "Please make sure admin.notification_master table is configured."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } catch(Exception e) { - e.printStackTrace(); - resultMap.put("Exception", "Exception Occured."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully registered").toMap()).build(); - } - - @DELETE - @Path("/onboardCallback") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - public Response deleteCallbackProp(JsonNotification jsonNotification) { - Map<String, Object> resultMap = new HashMap<>(); - ResponseBuilder response = - Response.noContent().header("X-latestVersion", MusicUtil.getVersion()); - String notifyOn = jsonNotification.getNotify_field(); - PreparedQueryObject pQuery = new PreparedQueryObject(); - try { - pQuery.appendQueryString("DELETE FROM admin.notification_master WHERE notifyon = ?"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), notifyOn)); - MusicCore.nonKeyRelatedPut(pQuery, MusicUtil.EVENTUAL); - } catch(Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setMessage("Callback api registration failed").toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Callback api successfully deleted").toMap()).build(); - } - - /*public String encodePwd(String password) { - return Base64.getEncoder().encodeToString(password.getBytes()); - } - - public String decodePwd(String password) { - byte[] bytes = Base64.getDecoder().decode(password); - return new String(bytes); - }*/ -} diff --git a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java b/src/main/java/org/onap/music/rest/RestMusicBmAPI.java deleted file mode 100644 index 55eb47f2..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicBmAPI.java +++ /dev/null @@ -1,307 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== Copyright (c) 2017 AT&T - * Intellectual Property =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.List; -import java.util.Map; -import java.util.UUID; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.UriInfo; -import org.onap.music.datastore.jsonobjects.JsonInsert; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.datastore.PreparedQueryObject; -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.TableMetadata; -import io.swagger.annotations.Api; -//import io.swagger.annotations.ApiOperation; -//import io.swagger.annotations.ApiParam; - -/* - * These are functions created purely for benchmarking purposes. Commented out Swagger - This should - * be undocumented API - * - */ -@Path("/v{version: [0-9]+}/benchmarks/") -@Api(value = "Benchmark API", hidden = true) -public class RestMusicBmAPI { - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicBmAPI.class); - - // pure zk calls... - - /** - * - * @param nodeName - * @throws Exception - */ - @POST - @Path("/purezk/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkCreate(@PathParam("name") String nodeName) throws Exception { - MusicCore.pureZkCreate("/" + nodeName); - } - - - /** - * - * @param insObj - * @param nodeName - * @throws Exception - */ - @PUT - @Path("/purezk/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkUpdate(JsonInsert insObj, @PathParam("name") String nodeName) - throws Exception { - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zk normal update-------------------------"); - long start = System.currentTimeMillis(); - MusicCore.pureZkWrite(nodeName, insObj.serialize()); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk normal update:" + (end - start) + " ms"); - } - - /** - * - * @param nodeName - * @return - * @throws Exception - */ - @GET - @Path("/purezk/{name}") - @Consumes(MediaType.TEXT_PLAIN) - public byte[] pureZkGet(@PathParam("name") String nodeName) throws Exception { - return MusicCore.pureZkRead(nodeName); - } - - /** - * - * @param insObj - * @param lockName - * @param nodeName - * @throws Exception - */ - @PUT - @Path("/purezk/atomic/{lockname}/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkAtomicPut(JsonInsert updateObj, @PathParam("lockname") String lockname, - @PathParam("name") String nodeName) throws Exception { - long startTime = System.currentTimeMillis(); - String operationId = UUID.randomUUID().toString();// just for debugging purposes. - String consistency = updateObj.getConsistencyInfo().get("type"); - - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Zookeeper " + consistency + " update-" + operationId - + "-------------------------"); - - byte[] data = updateObj.serialize(); - long jsonParseCompletionTime = System.currentTimeMillis(); - - String lockId = MusicCore.createLockReference(lockname); - - long lockCreationTime = System.currentTimeMillis(); - - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockname, lockId, leasePeriod); - long lockAcqTime = System.currentTimeMillis(); - long zkPutTime = 0, lockReleaseTime = 0; - - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info(EELFLoggerDelegate.applicationLogger,"acquired lock with id " + lockId); - MusicCore.pureZkWrite(lockname, data); - zkPutTime = System.currentTimeMillis(); - boolean voluntaryRelease = true; - if (consistency.equals("atomic")) - MusicCore.releaseLock(lockId, voluntaryRelease); - else if (consistency.equals("atomic_delete_lock")) - MusicCore.deleteLock(lockname); - lockReleaseTime = System.currentTimeMillis(); - } else { - MusicCore.destroyLockRef(lockId); - } - - long actualUpdateCompletionTime = System.currentTimeMillis(); - - - long endTime = System.currentTimeMillis(); - - String lockingInfo = "|lock creation time:" + (lockCreationTime - jsonParseCompletionTime) - + "|lock accquire time:" + (lockAcqTime - lockCreationTime) - + "|zk put time:" + (zkPutTime - lockAcqTime); - - if (consistency.equals("atomic")) - lockingInfo = lockingInfo + "|lock release time:" + (lockReleaseTime - zkPutTime) + "|"; - else if (consistency.equals("atomic_delete_lock")) - lockingInfo = lockingInfo + "|lock delete time:" + (lockReleaseTime - zkPutTime) + "|"; - - String timingString = "Time taken in ms for Zookeeper " + consistency + " update-" - + operationId + ":" + "|total operation time:" + (endTime - startTime) - + "|json parsing time:" + (jsonParseCompletionTime - startTime) - + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime) - + lockingInfo; - - logger.info(EELFLoggerDelegate.applicationLogger,timingString); - } - - /** - * - * @param insObj - * @param lockName - * @param nodeName - * @throws Exception - */ - @GET - @Path("/purezk/atomic/{lockname}/{name}") - @Consumes(MediaType.APPLICATION_JSON) - public void pureZkAtomicGet(JsonInsert insObj, @PathParam("lockname") String lockName, - @PathParam("name") String nodeName) throws Exception { - logger.info("--------------Zk atomic read-------------------------"); - long start = System.currentTimeMillis(); - String lockId = MusicCore.createLockReference(lockName); - long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); - ReturnType lockAcqResult = MusicCore.acquireLockWithLease(lockName, lockId, leasePeriod); - if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { - logger.info("acquired lock with id " + lockId); - MusicCore.pureZkRead(nodeName); - boolean voluntaryRelease = true; - MusicCore.releaseLock(lockId, voluntaryRelease); - } else { - MusicCore.destroyLockRef(lockId); - } - - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger,"Total time taken for Zk atomic read:" + (end - start) + " ms"); - } - - /** - * - * doing an update directly to cassa but through the rest api - * - * @param insObj - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @PUT - @Path("/cassa/keyspaces/{keyspace}/tables/{tablename}/rows") - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public boolean updateTableCassa(JsonInsert insObj, @PathParam("keyspace") String keyspace, - @PathParam("tablename") String tablename, @Context UriInfo info) - throws Exception { - long startTime = System.currentTimeMillis(); - String operationId = UUID.randomUUID().toString();// just for debugging purposes. - String consistency = insObj.getConsistencyInfo().get("type"); - logger.info(EELFLoggerDelegate.applicationLogger,"--------------Cassandra " + consistency + " update-" + operationId - + "-------------------------"); - PreparedQueryObject queryObject = new PreparedQueryObject(); - Map<String, Object> valuesMap = insObj.getValues(); - TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); - String vectorTs = "'" + Thread.currentThread().getId() + System.currentTimeMillis() + "'"; - String fieldValueString = "vector_ts= ? ,"; - queryObject.addValue(vectorTs); - - int counter = 0; - for (Map.Entry<String, Object> entry : valuesMap.entrySet()) { - Object valueObj = entry.getValue(); - DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object valueString = MusicUtil.convertToActualDataType(colType, valueObj); - fieldValueString = fieldValueString + entry.getKey() + "= ?"; - queryObject.addValue(valueString); - if (counter != valuesMap.size() - 1) - fieldValueString = fieldValueString + ","; - counter = counter + 1; - } - - // get the row specifier - String rowSpec = ""; - counter = 0; - queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " "); - MultivaluedMap<String, String> rowParams = info.getQueryParameters(); - String primaryKey = ""; - for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) { - String keyName = entry.getKey(); - List<String> valueList = entry.getValue(); - String indValue = valueList.get(0); - DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue); - primaryKey = primaryKey + indValue; - rowSpec = rowSpec + keyName + "= ? "; - queryObject.addValue(formattedValue); - if (counter != rowParams.size() - 1) - rowSpec = rowSpec + " AND "; - counter = counter + 1; - } - - - String ttl = insObj.getTtl(); - String timestamp = insObj.getTimestamp(); - - if ((ttl != null) && (timestamp != null)) { - - logger.info(EELFLoggerDelegate.applicationLogger,"both there"); - queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?"); - queryObject.addValue(Integer.parseInt(ttl)); - queryObject.addValue(Long.parseLong(timestamp)); - } - - if ((ttl != null) && (timestamp == null)) { - logger.info(EELFLoggerDelegate.applicationLogger,"ONLY TTL there"); - queryObject.appendQueryString(" USING TTL ?"); - queryObject.addValue(Integer.parseInt(ttl)); - } - - if ((ttl == null) && (timestamp != null)) { - logger.info(EELFLoggerDelegate.applicationLogger,"ONLY timestamp there"); - queryObject.appendQueryString(" USING TIMESTAMP ?"); - queryObject.addValue(Long.parseLong(timestamp)); - } - queryObject.appendQueryString(" SET " + fieldValueString + " WHERE " + rowSpec + ";"); - - long jsonParseCompletionTime = System.currentTimeMillis(); - - boolean operationResult = true; - MusicCore.getDSHandle().executePut(queryObject, insObj.getConsistencyInfo().get("type")); - - long actualUpdateCompletionTime = System.currentTimeMillis(); - - long endTime = System.currentTimeMillis(); - - String timingString = "Time taken in ms for Cassandra " + consistency + " update-" - + operationId + ":" + "|total operation time:" + (endTime - startTime) - + "|json parsing time:" + (jsonParseCompletionTime - startTime) - + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime) - + "|"; - logger.info(EELFLoggerDelegate.applicationLogger,timingString); - - return operationResult; - } -} diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java deleted file mode 100755 index 592b744b..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ /dev/null @@ -1,1466 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.HttpHeaders; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.core.Response.Status; -import javax.ws.rs.core.UriInfo; - -import org.apache.commons.lang3.StringUtils; -import org.mindrot.jbcrypt.BCrypt; -import org.onap.music.datastore.PreparedQueryObject; -import org.onap.music.datastore.jsonobjects.JsonDelete; -import org.onap.music.datastore.jsonobjects.JsonInsert; -import org.onap.music.datastore.jsonobjects.JsonKeySpace; -import org.onap.music.datastore.jsonobjects.JsonTable; -import org.onap.music.datastore.jsonobjects.JsonUpdate; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.exceptions.MusicLockingException; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.CachingUtil; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicCore.Condition; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.response.jsonobjects.JsonResponse; - -import com.datastax.driver.core.DataType; -import com.datastax.driver.core.ResultSet; -import com.datastax.driver.core.Row; -import com.datastax.driver.core.TableMetadata; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; - -/* Version 2 Class */ -//@Path("/v{version: [0-9]+}/keyspaces") -@Path("/v2/keyspaces") -@Api(value = "Data Api") -public class RestMusicDataAPI { - /* - * Header values for Versioning X-minorVersion *** - Used to request or communicate a MINOR - * version back from the client to the server, and from the server back to the client - This - * will be the MINOR version requested by the client, or the MINOR version of the last MAJOR - * version (if not specified by the client on the request) - Contains a single position value - * (e.g. if the full version is 1.24.5, X-minorVersion = "24") - Is optional for the client on - * request; however, this header should be provided if the client needs to take advantage of - * MINOR incremented version functionality - Is mandatory for the server on response - * - *** X-patchVersion *** - Used only to communicate a PATCH version in a response for - * troubleshooting purposes only, and will not be provided by the client on request - This will - * be the latest PATCH version of the MINOR requested by the client, or the latest PATCH version - * of the MAJOR (if not specified by the client on the request) - Contains a single position - * value (e.g. if the full version is 1.24.5, X-patchVersion = "5") - Is mandatory for the - * server on response (CURRENTLY NOT USED) - * - *** X-latestVersion *** - Used only to communicate an API's latest version - Is mandatory for the - * server on response, and shall include the entire version of the API (e.g. if the full version - * is 1.24.5, X-latestVersion = "1.24.5") - Used in the response to inform clients that they are - * not using the latest version of the API (CURRENTLY NOT USED) - * - */ - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class); - private static final String XMINORVERSION = "X-minorVersion"; - private static final String XPATCHVERSION = "X-patchVersion"; - private static final String NS = "ns"; - private static final String VERSION = "v2"; - - private class RowIdentifier { - public String primarKeyValue; - public StringBuilder rowIdString; - @SuppressWarnings("unused") - public PreparedQueryObject queryObject;// the string with all the row - // identifiers separated by AND - - public RowIdentifier(String primaryKeyValue, StringBuilder rowIdString, - PreparedQueryObject queryObject) { - this.primarKeyValue = primaryKeyValue; - this.rowIdString = rowIdString; - this.queryObject = queryObject; - } - } - - - /** - * Create Keyspace REST - * - * @param kspObject - * @param keyspaceName - * @return - * @throws Exception - */ - @POST - @Path("/{name}") - @ApiOperation(value = "Create Keyspace", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - //public Map<String, Object> createKeySpace( - public Response createKeySpace( - @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns, - JsonKeySpace kspObject, - @ApiParam(value = "Keyspace Name",required = true) @PathParam("name") String keyspaceName) { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = CachingUtil.verifyOnboarding(ns, userId, password); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"authMap has an error. verifyOnboarding may have failed silently", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - if(kspObject == null || kspObject.getReplicationInfo() == null) { - response.status(Status.BAD_REQUEST); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(ResultType.BODYMISSING.getResult()).toMap()).build(); - } - - - try { - authMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "createKeySpace"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - response.status(Status.BAD_REQUEST); - return response.entity(new JsonResponse(ResultType.FAILURE).setError("Unable to authenticate.").toMap()).build(); - } - String newAid = null; - if (!authMap.isEmpty()) { - if (authMap.containsKey("aid")) { - newAid = (String) authMap.get("aid"); - } else { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - } - - String consistency = MusicUtil.EVENTUAL;// for now this needs only - // eventual consistency - - PreparedQueryObject queryObject = new PreparedQueryObject(); - long start = System.currentTimeMillis(); - Map<String, Object> replicationInfo = kspObject.getReplicationInfo(); - String repString = null; - try { - repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}"; - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - - } - queryObject.appendQueryString( - "CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString); - if (kspObject.getDurabilityOfWrites() != null) { - queryObject.appendQueryString( - " AND durable_writes = " + kspObject.getDurabilityOfWrites()); - } - - queryObject.appendQueryString(";"); - long end = System.currentTimeMillis(); - logger.info(EELFLoggerDelegate.applicationLogger, - "Time taken for setting up query in create keyspace:" + (end - start)); - - ResultType result = ResultType.FAILURE; - try { - result = MusicCore.nonKeyRelatedPut(queryObject, consistency); - logger.info(EELFLoggerDelegate.applicationLogger, "result = " + result); - } catch ( MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("err:" + ex.getMessage()).toMap()).build(); - } - - try { - queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("CREATE ROLE IF NOT EXISTS '" + userId - + "' WITH PASSWORD = '" + password + "' AND LOGIN = true;"); - MusicCore.nonKeyRelatedPut(queryObject, consistency); - queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("GRANT ALL PERMISSIONS on KEYSPACE " + keyspaceName - + " to '" + userId + "'"); - queryObject.appendQueryString(";"); - MusicCore.nonKeyRelatedPut(queryObject, consistency); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - } - - try { - boolean isAAF = Boolean.valueOf(CachingUtil.isAAFApplication(ns)); - String hashedpwd = BCrypt.hashpw(password, BCrypt.gensalt()); - queryObject = new PreparedQueryObject(); - queryObject.appendQueryString( - "INSERT into admin.keyspace_master (uuid, keyspace_name, application_name, is_api, " - + "password, username, is_aaf) values (?,?,?,?,?,?,?)"); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), newAid)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), keyspaceName)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), ns)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), "True")); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), hashedpwd)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), userId)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.cboolean(), isAAF)); - CachingUtil.updateMusicCache(keyspaceName, ns); - CachingUtil.updateMusicValidateCache(ns, userId, hashedpwd); - MusicCore.eventualPut(queryObject); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - return response.status(Response.Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Created").toMap()).build(); - } - - /** - * - * @param kspObject - * @param keyspaceName - * @return - * @throws Exception - */ - @DELETE - @Path("/{name}") - @ApiOperation(value = "Delete Keyspace", response = String.class) - @Produces(MediaType.APPLICATION_JSON) - //public Map<String, Object> dropKeySpace( - public Response dropKeySpace( - @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Keyspace Name",required = true) @PathParam("name") String keyspaceName) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password,keyspaceName, aid, "dropKeySpace"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - return response.status(Status.UNAUTHORIZED).entity(authMap).build(); - } - - String consistency = MusicUtil.EVENTUAL;// for now this needs only - // eventual - // consistency - String appName = CachingUtil.getAppName(keyspaceName); - String uuid = CachingUtil.getUuidFromMusicCache(keyspaceName); - PreparedQueryObject pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "select count(*) as count from admin.keyspace_master where application_name=? allow filtering;"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), appName)); - Row row = MusicCore.get(pQuery).one(); - long count = row.getLong(0); - - if (count == 0) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Keyspace not found. Please make sure keyspace exists.").toMap()).build(); - // Admin Functions: - } else if (count == 1) { - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString( - "UPDATE admin.keyspace_master SET keyspace_name=? where uuid = ?;"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.text(), - MusicUtil.DEFAULTKEYSPACENAME)); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - MusicCore.nonKeyRelatedPut(pQuery, consistency); - } else { - pQuery = new PreparedQueryObject(); - pQuery.appendQueryString("delete from admin.keyspace_master where uuid = ?"); - pQuery.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), uuid)); - MusicCore.nonKeyRelatedPut(pQuery, consistency); - } - - PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("DROP KEYSPACE " + keyspaceName + ";"); - ResultType result = MusicCore.nonKeyRelatedPut(queryObject, consistency); - if ( result.equals(ResultType.FAILURE) ) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Deleteing Keyspace " + keyspaceName).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("Keyspace " + keyspaceName + " Deleted").toMap()).build(); - } - - /** - * - * @param tableObj - * @param version - * @param keyspace - * @param tablename - * @param headers - * @return - * @throws Exception - */ - @POST - @Path("/{keyspace}/tables/{tablename}") - @ApiOperation(value = "Create Table", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - //public Map<String, Object> createTable( - public Response createTable( - @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonTable tableObj, - @ApiParam(value = "Keyspace Name",required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name",required = true) @PathParam("tablename") String tablename) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "createTable"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - String consistency = MusicUtil.EVENTUAL; - // for now this needs only eventual consistency - - String primaryKey = null; - String partitionKey = tableObj.getPartitionKey(); - String clusterKey = tableObj.getClusteringKey(); - String filteringKey = tableObj.getFilteringKey(); - if(filteringKey != null) { - clusterKey = clusterKey + "," + filteringKey; - } - primaryKey = tableObj.getPrimaryKey(); // get primaryKey if available - - PreparedQueryObject queryObject = new PreparedQueryObject(); - // first read the information about the table fields - Map<String, String> fields = tableObj.getFields(); - StringBuilder fieldsString = new StringBuilder("(vector_ts text,"); - int counter = 0; - for (Map.Entry<String, String> entry : fields.entrySet()) { - if (entry.getKey().equals("PRIMARY KEY")) { - primaryKey = entry.getValue(); // replaces primaryKey - primaryKey.trim(); - } else { - if (counter == 0 ) fieldsString.append("" + entry.getKey() + " " + entry.getValue() + ""); - else fieldsString.append("," + entry.getKey() + " " + entry.getValue() + ""); - } - - if (counter != (fields.size() - 1) ) { - - //logger.info("cjc2 field="+entry.getValue()+"counter=" + counter+"fieldsize-1="+(fields.size() -1) + ","); - counter = counter + 1; - } else { - //logger.info("cjc3 field="+entry.getValue()+"counter=" + counter+"fieldsize="+fields.size() + ","); - if((primaryKey != null) && (partitionKey == null)) { - primaryKey.trim(); - int count1 = StringUtils.countMatches(primaryKey, ')'); - int count2 = StringUtils.countMatches(primaryKey, '('); - if (count1 != count2) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Create Table Error: primary key '(' and ')' do not match, primary key=" + primaryKey) - .toMap()).build(); - } - - if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(")") +1) == primaryKey.length() ) ) - { - if (primaryKey.contains(",") ) { - partitionKey= primaryKey.substring(0,primaryKey.indexOf(",")); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusterKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index - clusterKey=clusterKey.replaceAll("[)]+", ""); - } else { - partitionKey=primaryKey; - partitionKey=partitionKey.replaceAll("[\\)]+",""); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusterKey=""; - } - } else { // not null and has ) before the last char - partitionKey= primaryKey.substring(0,primaryKey.indexOf(')')); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - partitionKey.trim(); - clusterKey= primaryKey.substring(primaryKey.indexOf(')')); - clusterKey=clusterKey.replaceAll("[\\(]+",""); - clusterKey=clusterKey.replaceAll("[\\)]+",""); - clusterKey.trim(); - if (clusterKey.indexOf(",") == 0) clusterKey=clusterKey.substring(1); - clusterKey.trim(); - if (clusterKey.equals(",") ) clusterKey=""; // print error if needed ( ... ),) - - } - - if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) - && (partitionKey.equalsIgnoreCase(clusterKey) || - clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) - { - logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey + " and primary key=" + primaryKey ); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( - "Create Table primary key error: clusterKey(" + clusterKey + ") equals/contains/overlaps partitionKey(" +partitionKey+ ") of" - + " primary key=" + primaryKey) - .toMap()).build(); - - } - - if (partitionKey.isEmpty() ) primaryKey=""; - else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; - else primaryKey=" (" + partitionKey + ")," + clusterKey; - - //if (primaryKey != null) fieldsString.append("" + entry.getKey() + " (" + primaryKey + " )"); - if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); - - } // end of length > 0 - else { - if (!(partitionKey.isEmpty() || clusterKey.isEmpty()) - && (partitionKey.equalsIgnoreCase(clusterKey) || - clusterKey.contains(partitionKey) || partitionKey.contains(clusterKey)) ) - { - logger.error("DataAPI createTable partition/cluster key ERROR: partitionKey="+partitionKey+", clusterKey=" + clusterKey); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError( - "Create Table primary key error: clusterKey(" + clusterKey + ") equals/contains/overlaps partitionKey(" +partitionKey+ ")") - .toMap()).build(); - } - - if (partitionKey.isEmpty() ) primaryKey=""; - else if (clusterKey.isEmpty() ) primaryKey=" (" + partitionKey + ")"; - else primaryKey=" (" + partitionKey + ")," + clusterKey; - - //if (primaryKey != null) fieldsString.append("" + entry.getKey() + " (" + primaryKey + " )"); - if (primaryKey != null) fieldsString.append(", PRIMARY KEY (" + primaryKey + " )"); - } - fieldsString.append(")"); - - } // end of last field check - - } // end of for each - // information about the name-value style properties - Map<String, Object> propertiesMap = tableObj.getProperties(); - StringBuilder propertiesString = new StringBuilder(); - if (propertiesMap != null) { - counter = 0; - for (Map.Entry<String, Object> entry : propertiesMap.entrySet()) { - Object ot = entry.getValue(); - String value = ot + ""; - if (ot instanceof String) { - value = "'" + value + "'"; - } else if (ot instanceof Map) { - @SuppressWarnings("unchecked") - Map<String, Object> otMap = (Map<String, Object>) ot; - value = "{" + MusicUtil.jsonMaptoSqlString(otMap, ",") + "}"; - } - - propertiesString.append(entry.getKey() + "=" + value + ""); - if (counter != propertiesMap.size() - 1) - propertiesString.append(" AND "); - - counter = counter + 1; - } - } - - String clusteringOrder = tableObj.getClusteringOrder(); - - if (clusteringOrder != null && !(clusteringOrder.isEmpty())) { - String[] arrayClusterOrder = clusteringOrder.split("[,]+"); - - for (int i = 0; i < arrayClusterOrder.length; i++) - { - String[] clusterS = arrayClusterOrder[i].trim().split("[ ]+"); - if ( (clusterS.length ==2) && (clusterS[1].equalsIgnoreCase("ASC") || clusterS[1].equalsIgnoreCase("DESC"))) continue; - else { - //logger.error("createTable/Clustering Order vlaue ERROR: valid clustering order is ASC or DESC or expecting colname order; please correct clusteringOrder:\"+ clusteringOrder+\".\"", " valid clustering order is ASC or DESC; please correct clusteringOrder:"+ clusteringOrder+"."); - // logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - // ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("createTable/Clustering Order vlaue ERROR: valid clustering order is ASC or DESC or expecting colname order; please correct clusteringOrder:"+ clusteringOrder+".") - .toMap()).build(); - } - // add validation for column names in cluster key - } - - if (!(clusterKey.isEmpty())) - { - clusteringOrder = "CLUSTERING ORDER BY (" +clusteringOrder +")"; - //cjc check if propertiesString.length() >0 instead propertiesMap - if (propertiesMap != null) propertiesString.append(" AND "+ clusteringOrder); - else propertiesString.append(clusteringOrder); - } else { - logger.warn("Skipping clustering order=("+clusteringOrder+ ") since clustering key is empty "); - } - } //if non empty - - queryObject.appendQueryString( - "CREATE TABLE " + keyspace + "." + tablename + " " + fieldsString); - - - if (propertiesString != null && propertiesString.length()>0 ) - queryObject.appendQueryString(" WITH " + propertiesString); - queryObject.appendQueryString(";"); - ResultType result = ResultType.FAILURE; - try { - //logger.info("cjc query="+queryObject.getQuery()); - result = MusicCore.nonKeyRelatedPut(queryObject, consistency); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.MUSICSERVICEERROR); - response.status(Status.BAD_REQUEST); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - if ( result.equals(ResultType.FAILURE) ) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Error Creating Table " + tablename).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setMessage("TableName " + tablename + " Created under keyspace " + keyspace).toMap()).build(); - } - - /** - * - * @param keyspace - * @param tablename - * @param fieldName - * @param info - * @throws Exception - */ - @POST - @Path("/{keyspace}/tables/{tablename}/index/{field}") - @ApiOperation(value = "Create Index", response = String.class) - @Produces(MediaType.APPLICATION_JSON) - public Response createIndex( - @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Keyspace Name",required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name",required = true) @PathParam("tablename") String tablename, - @ApiParam(value = "Field Name",required = true) @PathParam("field") String fieldName, - @Context UriInfo info) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace,aid, "createIndex"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - response.status(Status.UNAUTHORIZED); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - MultivaluedMap<String, String> rowParams = info.getQueryParameters(); - String indexName = ""; - if (rowParams.getFirst("index_name") != null) - indexName = rowParams.getFirst("index_name"); - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("Create index if not exists " + indexName + " on " + keyspace + "." - + tablename + " (" + fieldName + ");"); - - ResultType result = ResultType.FAILURE; - try { - result = MusicCore.nonKeyRelatedPut(query, "eventual"); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - response.status(Status.BAD_REQUEST); - return response.entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - if ( result.equals(ResultType.SUCCESS) ) { - return response.status(Status.OK).entity(new JsonResponse(result).setMessage("Index Created on " + keyspace+"."+tablename+"."+fieldName).toMap()).build(); - } else { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(result).setError("Unknown Error in create index.").toMap()).build(); - } - } - - /** - * - * @param insObj - * @param keyspace - * @param tablename - * @return - * @throws Exception - */ - @POST - @Path("/{keyspace}/tables/{tablename}/rows") - @ApiOperation(value = "Insert Into Table", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response insertIntoTable( - @ApiParam(value = "Major Version",required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace",required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonInsert insObj, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename) { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = null; - - try { - authMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "insertIntoTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - - Map<String, Object> valuesMap = insObj.getValues(); - PreparedQueryObject queryObject = new PreparedQueryObject(); - TableMetadata tableInfo = null; - try { - tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); - if(tableInfo == null) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Table name doesn't exists. Please check the table name.").toMap()).build(); - } - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName(); - StringBuilder fieldsString = new StringBuilder("(vector_ts,"); - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - StringBuilder valueString = new StringBuilder("(" + "?" + ","); - queryObject.addValue(vectorTs); - int counter = 0; - String primaryKey = ""; - Map<String, byte[]> objectMap = insObj.getObjectMap(); - for (Map.Entry<String, Object> entry : valuesMap.entrySet()) { - fieldsString.append("" + entry.getKey()); - Object valueObj = entry.getValue(); - if (primaryKeyName.equals(entry.getKey())) { - primaryKey = entry.getValue() + ""; - primaryKey = primaryKey.replace("'", "''"); - } - DataType colType = null; - try { - colType = tableInfo.getColumn(entry.getKey()).getType(); - } catch(NullPointerException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() +" Invalid column name : "+entry.getKey(), AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Invalid column name : "+entry.getKey()).toMap()).build(); - } - - Object formattedValue = null; - try { - formattedValue = MusicUtil.convertToActualDataType(colType, valueObj); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - } - valueString.append("?"); - - queryObject.addValue(formattedValue); - - if (counter == valuesMap.size() - 1) { - fieldsString.append(")"); - valueString.append(")"); - } else { - fieldsString.append(","); - valueString.append(","); - } - counter = counter + 1; - } - - //blobs.. - if(objectMap != null) { - for (Map.Entry<String, byte[]> entry : objectMap.entrySet()) { - if(counter > 0) { - fieldsString.replace(fieldsString.length()-1, fieldsString.length(), ","); - valueString.replace(valueString.length()-1, valueString.length(), ","); - } - fieldsString.append("" + entry.getKey()); - byte[] valueObj = entry.getValue(); - if (primaryKeyName.equals(entry.getKey())) { - primaryKey = entry.getValue() + ""; - primaryKey = primaryKey.replace("'", "''"); - } - - DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - - ByteBuffer formattedValue = null; - - if(colType.toString().toLowerCase().contains("blob")) - formattedValue = MusicUtil.convertToActualDataType(colType, valueObj); - - valueString.append("?"); - - queryObject.addValue(formattedValue); - counter = counter + 1; - /*if (counter == valuesMap.size() - 1) { - fieldsString.append(")"); - valueString.append(")"); - } else {*/ - fieldsString.append(","); - valueString.append(","); - //} - } } - - if(primaryKey == null || primaryKey.length() <= 0) { - logger.error(EELFLoggerDelegate.errorLogger, "Some required partition key parts are missing: "+primaryKeyName ); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.SYNTAXERROR).setError("Some required partition key parts are missing: "+primaryKeyName).toMap()).build(); - } - - fieldsString.replace(fieldsString.length()-1, fieldsString.length(), ")"); - valueString.replace(valueString.length()-1, valueString.length(), ")"); - - queryObject.appendQueryString("INSERT INTO " + keyspace + "." + tablename + " " - + fieldsString + " VALUES " + valueString); - - String ttl = insObj.getTtl(); - String timestamp = insObj.getTimestamp(); - - if ((ttl != null) && (timestamp != null)) { - logger.info(EELFLoggerDelegate.applicationLogger, "both there"); - queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?"); - queryObject.addValue(Integer.parseInt(ttl)); - queryObject.addValue(Long.parseLong(timestamp)); - } - - if ((ttl != null) && (timestamp == null)) { - logger.info(EELFLoggerDelegate.applicationLogger, "ONLY TTL there"); - queryObject.appendQueryString(" USING TTL ?"); - queryObject.addValue(Integer.parseInt(ttl)); - } - - if ((ttl == null) && (timestamp != null)) { - logger.info(EELFLoggerDelegate.applicationLogger, "ONLY timestamp there"); - queryObject.appendQueryString(" USING TIMESTAMP ?"); - queryObject.addValue(Long.parseLong(timestamp)); - } - - queryObject.appendQueryString(";"); - - ReturnType result = null; - String consistency = insObj.getConsistencyInfo().get("type"); - try { - if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) { - result = MusicCore.eventualPut(queryObject); - } else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - String lockId = insObj.getConsistencyInfo().get("lockId"); - if(lockId == null) { - logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or" - + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " - + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); - } - result = MusicCore.criticalPut(keyspace, tablename, primaryKey, queryObject, lockId,null); - } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - result = MusicCore.atomicPut(keyspace, tablename, primaryKey, queryObject, null); - - } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - result = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, primaryKey, queryObject, null); - - } - } catch (Exception ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - - if (result==null) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(result.getResult()).setMessage("Insert Successful").toMap()).build(); - } - - /** - * - * @param insObj - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @PUT - @Path("/{keyspace}/tables/{tablename}/rows") - @ApiOperation(value = "Update Table", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response updateTable( - @ApiParam(value = "Major Version", - required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonUpdate updateObj, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename, - @Context UriInfo info) { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap; - try { - authMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "updateTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - long startTime = System.currentTimeMillis(); - String operationId = UUID.randomUUID().toString();// just for infoging - // purposes. - String consistency = updateObj.getConsistencyInfo().get("type"); - logger.info(EELFLoggerDelegate.applicationLogger, "--------------Music " + consistency - + " update-" + operationId + "-------------------------"); - // obtain the field value pairs of the update - - PreparedQueryObject queryObject = new PreparedQueryObject(); - Map<String, Object> valuesMap = updateObj.getValues(); - - TableMetadata tableInfo; - try { - tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); - } catch (MusicServiceException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (tableInfo == null) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Table information not found. Please check input for table name= " - + keyspace + "." + tablename).toMap()).build(); - } - String vectorTs = - String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); - StringBuilder fieldValueString = new StringBuilder("vector_ts=?,"); - queryObject.addValue(vectorTs); - int counter = 0; - for (Map.Entry<String, Object> entry : valuesMap.entrySet()) { - Object valueObj = entry.getValue(); - DataType colType = null; - try { - colType = tableInfo.getColumn(entry.getKey()).getType(); - } catch(NullPointerException ex) { - logger.error(EELFLoggerDelegate.errorLogger, "Invalid column name : "+entry.getKey()); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Invalid column name : "+entry.getKey()).toMap()).build(); - } - Object valueString = null; - try { - valueString = MusicUtil.convertToActualDataType(colType, valueObj); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - } - fieldValueString.append(entry.getKey() + "= ?"); - queryObject.addValue(valueString); - if (counter != valuesMap.size() - 1) - fieldValueString.append(","); - counter = counter + 1; - } - String ttl = updateObj.getTtl(); - String timestamp = updateObj.getTimestamp(); - - queryObject.appendQueryString("UPDATE " + keyspace + "." + tablename + " "); - if ((ttl != null) && (timestamp != null)) { - - logger.info("both there"); - queryObject.appendQueryString(" USING TTL ? AND TIMESTAMP ?"); - queryObject.addValue(Integer.parseInt(ttl)); - queryObject.addValue(Long.parseLong(timestamp)); - } - - if ((ttl != null) && (timestamp == null)) { - logger.info("ONLY TTL there"); - queryObject.appendQueryString(" USING TTL ?"); - queryObject.addValue(Integer.parseInt(ttl)); - } - - if ((ttl == null) && (timestamp != null)) { - logger.info("ONLY timestamp there"); - queryObject.appendQueryString(" USING TIMESTAMP ?"); - queryObject.addValue(Long.parseLong(timestamp)); - } - // get the row specifier - RowIdentifier rowId = null; - try { - rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); - if(rowId == null || rowId.primarKeyValue.isEmpty()) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Mandatory WHERE clause is missing. Please check the input request.").toMap()).build(); - } - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - - queryObject.appendQueryString( - " SET " + fieldValueString + " WHERE " + rowId.rowIdString + ";"); - - // get the conditional, if any - Condition conditionInfo; - if (updateObj.getConditions() == null) - conditionInfo = null; - else {// to avoid parsing repeatedly, just send the select query to - // obtain row - PreparedQueryObject selectQuery = new PreparedQueryObject(); - selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE " - + rowId.rowIdString + ";"); - selectQuery.addValue(rowId.primarKeyValue); - conditionInfo = new MusicCore.Condition(updateObj.getConditions(), selectQuery); - } - - ReturnType operationResult = null; - long jsonParseCompletionTime = System.currentTimeMillis(); - - if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) - operationResult = MusicCore.eventualPut(queryObject); - else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - String lockId = updateObj.getConsistencyInfo().get("lockId"); - if(lockId == null) { - logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or" - + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " - + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); - } - operationResult = MusicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, - queryObject, lockId, conditionInfo); - } else if (consistency.equalsIgnoreCase("atomic_delete_lock")) { - // this function is mainly for the benchmarks - try { - operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, - rowId.primarKeyValue, queryObject, conditionInfo); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - try { - operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - } - long actualUpdateCompletionTime = System.currentTimeMillis(); - - long endTime = System.currentTimeMillis(); - String timingString = "Time taken in ms for Music " + consistency + " update-" + operationId - + ":" + "|total operation time:" + (endTime - startTime) - + "|json parsing time:" + (jsonParseCompletionTime - startTime) - + "|update time:" + (actualUpdateCompletionTime - jsonParseCompletionTime) - + "|"; - - if (operationResult != null && operationResult.getTimingInfo() != null) { - String lockManagementTime = operationResult.getTimingInfo(); - timingString = timingString + lockManagementTime; - } - logger.info(EELFLoggerDelegate.applicationLogger, timingString); - - if (operationResult==null) { - logger.error(EELFLoggerDelegate.errorLogger,"Null result - Please Contact admin", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build(); - } - if ( operationResult.getResult() == ResultType.SUCCESS ) { - return response.status(Status.OK).entity(new JsonResponse(operationResult.getResult()).setMessage(operationResult.getMessage()).toMap()).build(); - } else { - logger.error(EELFLoggerDelegate.errorLogger,operationResult.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(operationResult.getResult()).setError(operationResult.getMessage()).toMap()).build(); - } - - } - - /** - * - * @param delObj - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @DELETE - @Path("/{keyspace}/tables/{tablename}/rows") - @ApiOperation(value = "Delete From table", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response deleteFromTable( - @ApiParam(value = "Major Version", - required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonDelete delObj, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename, - @Context UriInfo info) { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = null; - try { - authMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "deleteFromTable"); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - if(delObj == null) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGDATA ,ErrorSeverity.WARN, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Required HTTP Request body is missing.").toMap()).build(); - } - PreparedQueryObject queryObject = new PreparedQueryObject(); - StringBuilder columnString = new StringBuilder(); - - int counter = 0; - ArrayList<String> columnList = delObj.getColumns(); - if (columnList != null) { - for (String column : columnList) { - columnString.append(column); - if (counter != columnList.size() - 1) - columnString.append(","); - counter = counter + 1; - } - } - - // get the row specifier - RowIdentifier rowId = null; - try { - rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - String rowSpec = rowId.rowIdString.toString(); - - if ((columnList != null) && (!rowSpec.isEmpty())) { - queryObject.appendQueryString("DELETE " + columnString + " FROM " + keyspace + "." - + tablename + " WHERE " + rowSpec + ";"); - } - - if ((columnList == null) && (!rowSpec.isEmpty())) { - queryObject.appendQueryString("DELETE FROM " + keyspace + "." + tablename + " WHERE " - + rowSpec + ";"); - } - - if ((columnList != null) && (rowSpec.isEmpty())) { - queryObject.appendQueryString( - "DELETE " + columnString + " FROM " + keyspace + "." + rowSpec + ";"); - } - // get the conditional, if any - Condition conditionInfo; - if (delObj.getConditions() == null) - conditionInfo = null; - else {// to avoid parsing repeatedly, just send the select query to - // obtain row - PreparedQueryObject selectQuery = new PreparedQueryObject(); - selectQuery.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " WHERE " - + rowId.rowIdString + ";"); - selectQuery.addValue(rowId.primarKeyValue); - conditionInfo = new MusicCore.Condition(delObj.getConditions(), selectQuery); - } - - String consistency = delObj.getConsistencyInfo().get("type"); - - ReturnType operationResult = null; - try { - if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) - operationResult = MusicCore.eventualPut(queryObject); - else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - String lockId = delObj.getConsistencyInfo().get("lockId"); - if(lockId == null) { - logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or" - + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " - + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); - } - operationResult = MusicCore.criticalPut(keyspace, tablename, rowId.primarKeyValue, - queryObject, lockId, conditionInfo); - } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); - } - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); - } - } catch (MusicLockingException e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Unable to perform Delete operation. Exception from music").toMap()).build(); - } - if (operationResult==null) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Null result - Please Contact admin").toMap()).build(); - } - if (operationResult.getResult().equals(ResultType.SUCCESS)) { - return response.status(Status.OK).entity(new JsonResponse(operationResult.getResult()).setMessage(operationResult.getMessage()).toMap()).build(); - } else { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(operationResult.getMessage()).toMap()).build(); - } - } - - /** - * - * @param tabObj - * @param keyspace - * @param tablename - * @throws Exception - */ - @DELETE - @Path("/{keyspace}/tables/{tablename}") - @ApiOperation(value = "Drop Table", response = String.class) - @Produces(MediaType.APPLICATION_JSON) - public Response dropTable( - @ApiParam(value = "Major Version", - required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = - MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "dropTable"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - String consistency = "eventual";// for now this needs only eventual - // consistency - PreparedQueryObject query = new PreparedQueryObject(); - query.appendQueryString("DROP TABLE " + keyspace + "." + tablename + ";"); - try { - return response.status(Status.OK).entity(new JsonResponse(MusicCore.nonKeyRelatedPut(query, consistency)).toMap()).build(); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - - } - - /** - * - * @param selObj - * @param keyspace - * @param tablename - * @param info - * @return - */ - @PUT - @Path("/{keyspace}/tables/{tablename}/rows/criticalget") - @ApiOperation(value = "Select Critical", response = Map.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response selectCritical( - @ApiParam(value = "Major Version", - required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonInsert selObj, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename, - @Context UriInfo info) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = MusicCore.autheticateUser(ns, userId, password, keyspace,aid, "selectCritical"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"Error while authentication... ", AppMessages.MISSINGINFO ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - String lockId = selObj.getConsistencyInfo().get("lockId"); - - PreparedQueryObject queryObject = new PreparedQueryObject(); - - RowIdentifier rowId = null; - try { - rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - queryObject.appendQueryString( - "SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowId.rowIdString + ";"); - - ResultSet results = null; - - String consistency = selObj.getConsistencyInfo().get("type"); - - if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - if(lockId == null) { - logger.error(EELFLoggerDelegate.errorLogger,"LockId cannot be null. Create lock reference or" - + " use ATOMIC instead of CRITICAL", ErrorSeverity.FATAL, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("LockId cannot be null. Create lock " - + "and acquire lock or use ATOMIC instead of CRITICAL").toMap()).build(); - } - results = MusicCore.criticalGet(keyspace, tablename, rowId.primarKeyValue, queryObject, - lockId); - } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - results = MusicCore.atomicGet(keyspace, tablename, rowId.primarKeyValue, queryObject); - } - - else if (consistency.equalsIgnoreCase(MusicUtil.ATOMICDELETELOCK)) { - results = MusicCore.atomicGetWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, queryObject); - } - if(results!=null && results.getAvailableWithoutFetching() >0) { - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setError("No data found").toMap()).build(); - - - } - - /** - * - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @GET - @Path("/{keyspace}/tables/{tablename}/rows") - @ApiOperation(value = "Select All or Select Specific", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response select( - @ApiParam(value = "Major Version", - required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam(NS) String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Keyspace Name", - required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", - required = true) @PathParam("tablename") String tablename, - @Context UriInfo info) throws Exception { - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - Map<String, Object> authMap = - MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "select"); - if (authMap.containsKey("aid")) - authMap.remove("aid"); - if (!authMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE).setError(String.valueOf(authMap.get("Exception"))).toMap()).build(); - } - PreparedQueryObject queryObject = new PreparedQueryObject(); - - if (info.getQueryParameters().isEmpty())// select all - queryObject.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + ";"); - else { - int limit = -1; // do not limit the number of results - try { - queryObject = selectSpecificQuery(VERSION, minorVersion, patchVersion, aid, ns, - userId, password, keyspace, tablename, info, limit); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - } - - try { - ResultSet results = MusicCore.get(queryObject); - if(results.getAvailableWithoutFetching() >0) { - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).setError("No data found").toMap()).build(); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()).build(); - } - - } - - /** - * - * @param keyspace - * @param tablename - * @param info - * @param limit - * @return - * @throws MusicServiceException - */ - public PreparedQueryObject selectSpecificQuery(String version, String minorVersion, - String patchVersion, String aid, String ns, String userId, String password, - String keyspace, String tablename, UriInfo info, int limit) - throws MusicServiceException { - - PreparedQueryObject queryObject = new PreparedQueryObject(); - StringBuilder rowIdString = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), - queryObject).rowIdString; - - queryObject.appendQueryString( - "SELECT * FROM " + keyspace + "." + tablename + " WHERE " + rowIdString); - - if (limit != -1) { - queryObject.appendQueryString(" LIMIT " + limit); - } - - queryObject.appendQueryString(";"); - return queryObject; - - } - - /** - * - * @param keyspace - * @param tablename - * @param rowParams - * @param queryObject - * @return - * @throws MusicServiceException - */ - private RowIdentifier getRowIdentifier(String keyspace, String tablename, - MultivaluedMap<String, String> rowParams, PreparedQueryObject queryObject) - throws MusicServiceException { - StringBuilder rowSpec = new StringBuilder(); - int counter = 0; - TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); - if (tableInfo == null) { - logger.error(EELFLoggerDelegate.errorLogger, - "Table information not found. Please check input for table name= " - + keyspace + "." + tablename); - throw new MusicServiceException( - "Table information not found. Please check input for table name= " - + keyspace + "." + tablename); - } - StringBuilder primaryKey = new StringBuilder(); - for (MultivaluedMap.Entry<String, List<String>> entry : rowParams.entrySet()) { - String keyName = entry.getKey(); - List<String> valueList = entry.getValue(); - String indValue = valueList.get(0); - DataType colType = null; - Object formattedValue = null; - try { - colType = tableInfo.getColumn(entry.getKey()).getType(); - formattedValue = MusicUtil.convertToActualDataType(colType, indValue); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); - } - if(tableInfo.getPrimaryKey().get(0).getName().equals(entry.getKey())) - primaryKey.append(indValue); - rowSpec.append(keyName + "= ?"); - queryObject.addValue(formattedValue); - if (counter != rowParams.size() - 1) - rowSpec.append(" AND "); - counter = counter + 1; - } - return new RowIdentifier(primaryKey.toString(), rowSpec, queryObject); - } -} diff --git a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java b/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java deleted file mode 100644 index 44b4a514..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicHealthCheckAPI.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.HashMap; -/** - * @author inam - * - */ -import java.util.Map; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.Consumes; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.core.Response.Status; - - -import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.eelf.healthcheck.MusicHealthCheck; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; - -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; -import com.datastax.driver.core.ConsistencyLevel; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; - - - - -@Path("/v{version: [0-9]+}/service") -@Api(value="Healthcheck Api") -public class RestMusicHealthCheckAPI { - - - private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class); - - - @GET - @Path("/pingCassandra/{consistency}") - @ApiOperation(value = "Get Health Status", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response cassandraStatus(@Context HttpServletResponse response, @ApiParam(value = "Consistency level", - required = true) @PathParam("consistency") String consistency) { - logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Cassandra"); - - Map<String, Object> resultMap = new HashMap<>(); - if(ConsistencyLevel.valueOf(consistency) == null) { - resultMap.put("INVALID", "Consistency level is invalid..."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - MusicHealthCheck cassHealthCheck = new MusicHealthCheck(); - String status = cassHealthCheck.getCassandraStatus(consistency); - if(status.equals("ACTIVE")) { - resultMap.put("ACTIVE", "Cassandra Running and Listening to requests"); - return Response.status(Status.OK).entity(resultMap).build(); - } else { - resultMap.put("INACTIVE", "One or more nodes in the Cluster is/are down or not responding."); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - - - } - - @GET - @Path("/pingZookeeper") - @ApiOperation(value = "Get Health Status", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response ZKStatus(@Context HttpServletResponse response) { - logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for MUSIC Health Check status for Zookeeper"); - Map<String, Object> resultMap = new HashMap<>(); - MusicHealthCheck ZKHealthCheck = new MusicHealthCheck(); - String status = ZKHealthCheck.getZookeeperStatus(); - if(status.equals("ACTIVE")) { - resultMap.put("ACTIVE", "Zookeeper is Active and Running"); - return Response.status(Status.OK).entity(resultMap).build(); - }else { - resultMap.put("INACTIVE", "Zookeeper is not responding"); - return Response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - } - - @GET - @Path("/musicHealthCheck") - @ApiOperation(value = "Get Health Status", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response musicHealthCheck() { - logger.info(EELFLoggerDelegate.applicationLogger,"Replying to request for Health Check status for MUSIC"); - Map<String, Object> resultMap = new HashMap<>(); - MusicHealthCheck healthCheck = new MusicHealthCheck(); - String status = healthCheck.getZookeeperStatus(); - if(status.equals("ACTIVE")) { - resultMap.put("ZooKeeper", "Active"); - }else { - resultMap.put("ZooKeeper", "Inactive"); - } - status = healthCheck.getCassandraStatus(ConsistencyLevel.ANY.toString()); - if(status.equals("ACTIVE")) { - resultMap.put("Cassandra", "Active"); - } else { - resultMap.put("Cassandra", "Inactive"); - } - resultMap.put("MUSIC", "Active"); - return Response.status(Status.OK).entity(resultMap).build(); - } - -} diff --git a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java b/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java deleted file mode 100644 index 70583baa..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicLocksAPI.java +++ /dev/null @@ -1,424 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.Map; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.core.Response.Status; -import org.onap.music.datastore.jsonobjects.JsonLeasedLock; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.onap.music.lockingservice.MusicLockState; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.main.ReturnType; -import org.onap.music.response.jsonobjects.JsonResponse; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; - - -@Path("/v2/locks/") -@Api(value="Lock Api") -public class RestMusicLocksAPI { - - private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicLocksAPI.class); - private static final String XMINORVERSION = "X-minorVersion"; - private static final String XPATCHVERSION = "X-patchVersion"; - private static final String VERSION = "v2"; - - /** - * Puts the requesting process in the q for this lock. The corresponding - * node will be created in zookeeper if it did not already exist - * - * @param lockName - * @return - * @throws Exception - */ - @POST - @Path("/create/{lockname}") - @ApiOperation(value = "Create Lock", - notes = "Puts the requesting process in the q for this lock." + - " The corresponding node will be created in zookeeper if it did not already exist." + - " Lock Name is the \"key\" of the form keyspaceName.tableName.rowId", - response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response createLockReference( - @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockName); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "createLockReference"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.AUTHENTICATIONERROR); - return response.status(Status.UNAUTHORIZED).entity(resultMap).build(); - } - ResultType status = ResultType.SUCCESS; - String lockId = MusicCore.createLockReference(lockName); - - if (lockId == null) { - status = ResultType.FAILURE; - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError("Lock Id is null").toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(status).setLock(lockId).toMap()).build(); - } - - /** - * - * Checks if the node is in the top of the queue and hence acquires the lock - * - * @param lockId - * @return - * @throws Exception - */ - @GET - @Path("/acquire/{lockreference}") - @ApiOperation(value = "Aquire Lock", - notes = "Checks if the node is in the top of the queue and hence acquires the lock", - response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response accquireLock( - @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockId); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "accquireLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - try { - String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); - ReturnType lockStatus = MusicCore.acquireLock(lockName,lockId); - if ( lockStatus.getResult().equals(ResultType.SUCCESS)) { - response.status(Status.OK); - } else { - response.status(Status.BAD_REQUEST); - } - return response.entity(new JsonResponse(lockStatus.getResult()).setLock(lockId).setMessage(lockStatus.getMessage()).toMap()).build(); - } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger,AppMessages.INVALIDLOCK + lockId, ErrorSeverity.CRITICAL, ErrorTypes.LOCKINGERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError("Unable to aquire lock").toMap()).build(); - } - } - - - - - @POST - @Path("/acquire-with-lease/{lockreference}") - @ApiOperation(value = "Aquire Lock with Lease", response = Map.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response accquireLockWithLease(JsonLeasedLock lockObj, - @ApiParam(value="Lock Reference",required=true) @PathParam("lockreference") String lockId, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockId); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "accquireLockWithLease"); - - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - String lockName = lockId.substring(lockId.indexOf('$')+1, lockId.lastIndexOf('$')); - ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, lockObj.getLeasePeriod()); - if ( lockLeaseStatus.getResult().equals(ResultType.SUCCESS)) { - response.status(Status.OK); - } else { - response.status(Status.BAD_REQUEST); - } - return response.entity(new JsonResponse(lockLeaseStatus.getResult()).setLock(lockName) - .setMessage(lockLeaseStatus.getMessage()) - .setLockLease(String.valueOf(lockObj.getLeasePeriod())).toMap()).build(); - } - - - @GET - @Path("/enquire/{lockname}") - @ApiOperation(value = "Get Lock Holder", - notes = "Gets the current Lock Holder", - response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response currentLockHolder( - @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockName); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "currentLockHolder"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - String who = MusicCore.whoseTurnIsIt(lockName); - ResultType status = ResultType.SUCCESS; - String error = ""; - if ( who == null ) { - status = ResultType.FAILURE; - error = "There was a problem getting the lock holder"; - logger.error(EELFLoggerDelegate.errorLogger,"There was a problem getting the lock holder", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(status).setError(error).setLock(lockName).setLockHolder(who).toMap()).build(); - } - - @GET - @Path("/{lockname}") - @ApiOperation(value = "Lock State", - notes = "Returns current Lock State and Holder.", - response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response currentLockState( - @ApiParam(value="Lock Name",required=true) @PathParam("lockname") String lockName, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "userId", - required = true) @HeaderParam("userId") String userId, - @ApiParam(value = "Password", - required = true) @HeaderParam("password") String password) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockName); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "currentLockState"); - - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - - MusicLockState mls = MusicCore.getMusicLockState(lockName); - Map<String,Object> returnMap = null; - JsonResponse jsonResponse = new JsonResponse(ResultType.FAILURE).setLock(lockName); - if(mls == null) { - jsonResponse.setError(""); - jsonResponse.setMessage("No lock object created yet.."); - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(jsonResponse.toMap()).build(); - } else { - jsonResponse.setStatus(ResultType.SUCCESS); - jsonResponse.setLockStatus(mls.getLockStatus()); - jsonResponse.setLockHolder(mls.getLockHolder()); - return response.status(Status.OK).entity(jsonResponse.toMap()).build(); - } - } - - /** - * - * deletes the process from the zk queue - * - * @param lockId - * @throws Exception - */ - @DELETE - @Path("/release/{lockreference}") - @ApiOperation(value = "Release Lock", - notes = "deletes the process from the zk queue", - response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response unLock(@PathParam("lockreference") String lockId, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockId); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "unLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - boolean voluntaryRelease = true; - MusicLockState mls = MusicCore.releaseLock(lockId,voluntaryRelease); - if(mls.getErrorMessage() != null) { - resultMap.put(ResultType.EXCEPTION.getResult(), mls.getErrorMessage()); - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.INCORRECTDATA ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,Object> returnMap = null; - if (mls.getLockStatus() == MusicLockState.LockStatus.UNLOCKED) { - returnMap = new JsonResponse(ResultType.SUCCESS).setLock(lockId) - .setLockStatus(mls.getLockStatus()).toMap(); - response.status(Status.OK); - } - if (mls.getLockStatus() == MusicLockState.LockStatus.LOCKED) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.LOCKINGERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - returnMap = new JsonResponse(ResultType.FAILURE).setLock(lockId) - .setLockStatus(mls.getLockStatus()).toMap(); - response.status(Status.BAD_REQUEST); - } - return response.entity(returnMap).build(); - } - - /** - * - * @param lockName - * @throws Exception - */ - @DELETE - @Path("/delete/{lockname}") - @ApiOperation(value = "Delete Lock", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Response deleteLock(@PathParam("lockname") String lockName, - @ApiParam(value = "Minor Version",required = false) @HeaderParam(XMINORVERSION) String minorVersion, - @ApiParam(value = "Patch Version",required = false) @HeaderParam(XPATCHVERSION) String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Application namespace", - required = true) @HeaderParam("ns") String ns) throws Exception{ - ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); - Map<String, Object> resultMap = MusicCore.validateLock(lockName); - if (resultMap.containsKey("Exception")) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - Map<String,String> userCredentials = MusicUtil.extractBasicAuthentication(authorization); - String userId = userCredentials.get(MusicUtil.USERID); - String password = userCredentials.get(MusicUtil.PASSWORD); - String keyspaceName = (String) resultMap.get("keyspace"); - resultMap.remove("keyspace"); - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "deleteLock"); - if (resultMap.containsKey("aid")) - resultMap.remove("aid"); - if (!resultMap.isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.CRITICAL, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST).entity(resultMap).build(); - } - try{ - MusicCore.deleteLock(lockName); - }catch (Exception e) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE).setError(e.getMessage()).toMap()).build(); - } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).toMap()).build(); - } - -} diff --git a/src/main/java/org/onap/music/rest/RestMusicQAPI.java b/src/main/java/org/onap/music/rest/RestMusicQAPI.java deleted file mode 100755 index 1f6ec24f..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicQAPI.java +++ /dev/null @@ -1,492 +0,0 @@ -/* - * ============LICENSE_START========================================== org.onap.music - * =================================================================== Copyright (c) 2017 AT&T - * Intellectual Property =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.HashMap; -import java.util.Map; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.HeaderParam; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.UriInfo; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; -import javax.ws.rs.core.Response.Status; -import org.onap.music.datastore.jsonobjects.JsonDelete; -import org.onap.music.datastore.jsonobjects.JsonInsert; -import org.onap.music.datastore.jsonobjects.JsonTable; -import org.onap.music.datastore.jsonobjects.JsonUpdate; -import org.onap.music.eelf.logging.EELFLoggerDelegate; - -import org.onap.music.eelf.logging.format.AppMessages; -import org.onap.music.eelf.logging.format.ErrorSeverity; -import org.onap.music.eelf.logging.format.ErrorTypes; -import org.apache.commons.lang3.StringUtils; -import org.onap.music.datastore.PreparedQueryObject; -import com.datastax.driver.core.ResultSet; -import org.onap.music.exceptions.MusicServiceException; -import org.onap.music.main.MusicCore; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; -import org.onap.music.response.jsonobjects.JsonResponse; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import io.swagger.annotations.ApiParam; - - -// @Path("/v{version: [0-9]+}/priorityq/") -@Path("{version}/priorityq/") -@Api(value = "Q Api") -public class RestMusicQAPI { - - private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicQAPI.class); - /* - * private static final String XMINORVERSION = "X-minorVersion"; private static final String - * XPATCHVERSION = "X-patchVersion"; private static final String NS = "ns"; private static final - * String USERID = "userId"; private static final String PASSWORD = "password"; - * */ - // private static final String VERSION = "v2"; - - - /** - * - * @param tableObj - * @param keyspace - * @param tablename - * @throws Exception - */ - - @POST - @Path("/keyspaces/{keyspace}/{qname}") // is it same as tablename?down - @ApiOperation(value = "Create Q", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - - /* old - @POST - @Path("/keyspaces/{keyspace}/{qname}") - @ApiOperation(value = "", response = Void.class) - @Consumes(MediaType.APPLICATION_JSON) - */ - public Response createQ( - // public Map<String,Object> createQ( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonTable tableObj, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { - - - ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); - - Map<String, String> fields = tableObj.getFields(); - if (fields == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ/Required table fields are empty or not set").toMap()) - .build(); - } - - String primaryKey = tableObj.getPrimaryKey(); - String partitionKey = tableObj.getPartitionKey(); - String clusteringKey = tableObj.getClusteringKey(); - String filteringKey = tableObj.getFilteringKey(); - String clusteringOrder = tableObj.getClusteringOrder(); - - if(primaryKey == null) { - primaryKey = tableObj.getFields().get("PRIMARY KEY"); - } - - if ((primaryKey == null) && (partitionKey == null)) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Partition key cannot be empty").toMap()) - .build(); - } - - if ((primaryKey == null) && (clusteringKey == null)) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Clustering key cannot be empty").toMap()) - .build(); - } - - if (clusteringOrder == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Clustering Order cannot be empty").toMap()) - .build(); - } - - if ((primaryKey!=null) && (partitionKey == null)) { - primaryKey.trim(); - int count1 = StringUtils.countMatches(primaryKey, ')'); - int count2 = StringUtils.countMatches(primaryKey, '('); - if (count1 != count2) { - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ Error: primary key '(' and ')' do not match, primary key=" + primaryKey) - .toMap()).build(); - } - - if ( primaryKey.indexOf('(') == -1 || ( count2 == 1 && (primaryKey.lastIndexOf(")") +1) == primaryKey.length() ) ) - { - if (primaryKey.contains(",") ) { - partitionKey= primaryKey.substring(0,primaryKey.indexOf(",")); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusteringKey=primaryKey.substring(primaryKey.indexOf(',')+1); // make sure index - clusteringKey=clusteringKey.replaceAll("[)]+", ""); - } else { - partitionKey=primaryKey; - partitionKey=partitionKey.replaceAll("[\\)]+",""); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - clusteringKey=""; - } - } else { - partitionKey= primaryKey.substring(0,primaryKey.indexOf(')')); - partitionKey=partitionKey.replaceAll("[\\(]+",""); - partitionKey.trim(); - clusteringKey= primaryKey.substring(primaryKey.indexOf(')')); - clusteringKey=clusteringKey.replaceAll("[\\(]+",""); - clusteringKey=clusteringKey.replaceAll("[\\)]+",""); - clusteringKey.trim(); - if (clusteringKey.indexOf(",") == 0) { - clusteringKey=clusteringKey.substring(1); - } - clusteringKey.trim(); - if (",".equals(clusteringKey) ) { - clusteringKey=""; // print error if needed ( ... ),) - } - } - } - - if (partitionKey.trim().isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Partition key cannot be empty").toMap()) - .build(); - } - - if (clusteringKey.trim().isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Clustering key cannot be empty").toMap()) - .build(); - } - - if((filteringKey != null) && (filteringKey.equalsIgnoreCase(partitionKey))) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE) - .setError("CreateQ: Filtering key cannot be same as Partition Key").toMap()) - .build(); - } - - return new RestMusicDataAPI().createTable(version, minorVersion, patchVersion, aid, ns, authorization, tableObj, keyspace, tablename); - } - - /** - * - * @param insObj - * @param keyspace - * @param tablename - * @throws Exception - */ - @POST - @Path("/keyspaces/{keyspace}/{qname}/rows") - @ApiOperation(value = "", response = Void.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - // public Map<String, Object> insertIntoQ( - public Response insertIntoQ( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonInsert insObj, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { - - // check valuesMap.isEmpty and proceed - // response.addHeader(xLatestVersion, MusicUtil.getVersion()); - ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); - if (insObj.getValues().isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("Required HTTP Request body is missing.").toMap()).build(); - } - return new RestMusicDataAPI().insertIntoTable(version, minorVersion, patchVersion, aid, ns, - authorization, insObj, keyspace, tablename); - } - - /** - * - * @param updateObj - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @PUT - @Path("/keyspaces/{keyspace}/{qname}/rows") - @ApiOperation(value = "updateQ", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response updateQ( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - JsonUpdate updateObj, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) { - - //logger.info(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, ErrorSeverity.CRITICAL, - ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); - if (updateObj.getValues().isEmpty()) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE).setError( - "Required HTTP Request body is missing. JsonUpdate updateObj.getValues() is empty. ") - .toMap()) - .build(); - - - } - return new RestMusicDataAPI().updateTable(version, minorVersion, patchVersion, aid, ns, - authorization,updateObj, keyspace, tablename, info); - } - - /** - * - * @param delObj - * @param keyspace - * @param tablename - * @param info - * - * @return - * @throws Exception - */ - - @DELETE - @Path("/keyspaces/{keyspace}/{qname}/rows") - @ApiOperation(value = "deleteQ", response = String.class) - @Consumes(MediaType.APPLICATION_JSON) - @Produces(MediaType.APPLICATION_JSON) - public Response deleteFromQ( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId, - // @ApiParam(value = "Password", required = true) @HeaderParam("password") String password, - JsonDelete delObj, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) { - // added checking as per RestMusicDataAPI - ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); - if (delObj == null) { - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGDATA, - ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); - return response.status(Status.BAD_REQUEST).entity(new JsonResponse(ResultType.FAILURE) - .setError("deleteFromQ JsonDelete delObjis empty").toMap()).build(); - } - - return new RestMusicDataAPI().deleteFromTable(version, minorVersion, patchVersion, aid, ns, - authorization, delObj, keyspace, tablename, info); - } - - /** - * - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @GET - @Path("/keyspaces/{keyspace}/{qname}/peek") - @ApiOperation(value = "", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - //public Map<String, HashMap<String, Object>> peek( - public Response peek( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) throws Exception { - int limit =1; //peek must return just the top row - Map<String ,String> auth = new HashMap<>(); - String userId =auth.get(MusicUtil.USERID); - String password =auth.get(MusicUtil.PASSWORD); - ResponseBuilder response = MusicUtil.buildVersionResponse(version, minorVersion, patchVersion); - - PreparedQueryObject queryObject = new PreparedQueryObject(); - if (info.getQueryParameters() == null ) //|| info.getQueryParameters().isEmpty()) - queryObject.appendQueryString( - "SELECT * FROM " + keyspace + "." + tablename + " LIMIT " + limit + ";"); - else { - - try { - queryObject = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion, - patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger, "MusicServiceException occured in peek"+ ex); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR, - ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()) - .build(); - } - } - - try { - ResultSet results = MusicCore.get(queryObject); - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS) - .setDataResult(MusicCore.marshallResults(results)).toMap()).build(); - } catch (MusicServiceException ex) { - logger.error(EELFLoggerDelegate.errorLogger, "MusicServiceException occured in peek"+ ex); - logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.UNKNOWNERROR, - ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); - return response.status(Status.BAD_REQUEST) - .entity(new JsonResponse(ResultType.FAILURE).setError(ex.getMessage()).toMap()) - .build(); - } - } - - /** - * - * - * @param keyspace - * @param tablename - * @param info - * @return - * @throws Exception - */ - @GET - @Path("/keyspaces/{keyspace}/{qname}/filter") - @ApiOperation(value = "filter", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - // public Map<String, HashMap<String, Object>> filter( - public Response filter( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId, - //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename, - @Context UriInfo info) throws Exception { - /* - * PreparedQueryObject query = new RestMusicDataAPI().selectSpecificQuery(version, minorVersion, - * patchVersion, aid, ns, userId, password, keyspace, tablename, info, limit); ResultSet results - */ - /* Map<String ,String> auth = new HashMap<>(); - String userId =auth.get(MusicUtil.USERID); - String password =auth.get(MusicUtil.PASSWORD); - */ - return new RestMusicDataAPI().select(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename, info);// , limit) - - } - - /** - * - * @param tabObj - * @param keyspace - * @param tablename - * @throws Exception - */ - @DELETE - @ApiOperation(value = "DropQ", response = String.class) - @Path("/keyspaces/{keyspace}/{qname}") - @Produces(MediaType.APPLICATION_JSON) - public Response dropQ( - @ApiParam(value = "Major Version", required = true) @PathParam("version") String version, - @ApiParam(value = "Minor Version", - required = false) @HeaderParam("X-minorVersion") String minorVersion, - @ApiParam(value = "Patch Version", - required = false) @HeaderParam("X-patchVersion") String patchVersion, - @ApiParam(value = "AID", required = true) @HeaderParam("aid") String aid, - @ApiParam(value = "Application namespace", required = true) @HeaderParam("ns") String ns, - @ApiParam(value = "Authorization", required = true) @HeaderParam(MusicUtil.AUTHORIZATION) String authorization, - // @ApiParam(value = "userId", required = true) @HeaderParam("userId") String userId, - //@ApiParam(value = "Password", required = true) @HeaderParam("password") String password, - // cjc JsonTable tabObj, - @ApiParam(value = "Key Space", required = true) @PathParam("keyspace") String keyspace, - @ApiParam(value = "Table Name", required = true) @PathParam("qname") String tablename) - throws Exception { - - - return new RestMusicDataAPI().dropTable(version, minorVersion, patchVersion, aid, ns, authorization, keyspace, tablename); - } -} diff --git a/src/main/java/org/onap/music/rest/RestMusicTestAPI.java b/src/main/java/org/onap/music/rest/RestMusicTestAPI.java deleted file mode 100644 index 287fa176..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicTestAPI.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; - -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicUtil; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; - - -@Path("/v{version: [0-9]+}/test") -@Api(value="Test Api") -public class RestMusicTestAPI { - - @SuppressWarnings("unused") - private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicTestAPI.class); - - /** - * Returns a test JSON. This will confirm that REST is working. - * @return - */ - @GET - @ApiOperation(value = "Get Test", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Map<String, HashMap<String, String>> simpleTests( - @Context HttpServletResponse response) { - response.addHeader("X-latestVersion",MusicUtil.getVersion()); - Map<String, HashMap<String, String>> testMap = new HashMap<>(); - for(int i=0; i < 3; i++){ - HashMap<String, String> innerMap = new HashMap<>(); - innerMap.put(i+"", i+1+""); - innerMap.put(i+1+"", i+2+""); - testMap.put(i+"", innerMap); - } - return testMap; - } -} diff --git a/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java b/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java deleted file mode 100644 index b9754f61..00000000 --- a/src/main/java/org/onap/music/rest/RestMusicVersionAPI.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * ============LICENSE_START========================================== - * org.onap.music - * =================================================================== - * Copyright (c) 2017 AT&T Intellectual Property - * =================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * ============LICENSE_END============================================= - * ==================================================================== - */ -package org.onap.music.rest; - -import java.util.Map; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; - -import org.onap.music.response.jsonobjects.JsonResponse; -import org.onap.music.eelf.logging.EELFLoggerDelegate; -import org.onap.music.main.MusicUtil; -import org.onap.music.main.ResultType; - -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; - - -@Path("/v{version: [0-9]+}/version") -@Api(value="Version Api") -public class RestMusicVersionAPI { - - private EELFLoggerDelegate logger =EELFLoggerDelegate.getLogger(RestMusicVersionAPI.class); - - /** - * Get the version of MUSIC - * @return - */ - @GET - @ApiOperation(value = "Get Version", response = Map.class) - @Produces(MediaType.APPLICATION_JSON) - public Map<String,Object> version(@Context HttpServletResponse response) { - logger.info("Replying to request for MUSIC version with MUSIC:" + MusicUtil.getVersion()); - response.addHeader("X-latestVersion",MusicUtil.getVersion()); - return new JsonResponse(ResultType.SUCCESS).setMusicVersion("MUSIC:" + MusicUtil.getVersion()).toMap(); - } -}
\ No newline at end of file |