diff options
Diffstat (limited to 'src/main/java/org/onap/music/rest')
-rwxr-xr-x | src/main/java/org/onap/music/rest/RestMusicAdminAPI.java | 20 | ||||
-rwxr-xr-x | src/main/java/org/onap/music/rest/RestMusicDataAPI.java | 228 |
2 files changed, 191 insertions, 57 deletions
diff --git a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java index 0265d039..cbe9ec9c 100755 --- a/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicAdminAPI.java @@ -39,6 +39,9 @@ import javax.ws.rs.core.MediaType; import org.onap.music.datastore.PreparedQueryObject; 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.CachingUtil; import org.onap.music.main.MusicCore; import org.onap.music.main.MusicUtil; @@ -72,6 +75,7 @@ public class RestMusicAdminAPI { String password = jsonObj.getPassword(); response.addHeader("X-latestVersion", MusicUtil.getVersion()); if (appName == null || userId == null || isAAF == null || password == null) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.MISSINGINFO ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR); resultMap.put("Exception", "Please check the request parameters. Some of the required values appName(ns), userId, password, isAAF are missing."); return resultMap; @@ -163,8 +167,14 @@ public class RestMusicAdminAPI { Row row = (Row) it.next(); resultMap.put( row.getUUID("uuid").toString(),row.getString("keyspace_name")); } - if (resultMap.isEmpty()) - resultMap.put("ERROR", "Application is not onboarded. Please contact admin."); + if (resultMap.isEmpty()) { + if(uuid != null) + resultMap.put("Exception", "Please make sure Aid is correct and application is onboarded."); + else { + resultMap.put("Exception", + "Application is not onboarded. Please make sure all the information is correct."); + } + } return resultMap; } @@ -195,7 +205,7 @@ public class RestMusicAdminAPI { String ks = row.getString("keyspace_name"); if (!ks.equals(MusicUtil.DEFAULTKEYSPACENAME)) { PreparedQueryObject queryObject = new PreparedQueryObject(); - queryObject.appendQueryString("DROP KEYSPACE " + ks + ";"); + queryObject.appendQueryString("DROP KEYSPACE IF EXISTS " + ks + ";"); MusicCore.nonKeyRelatedPut(queryObject, consistency); } } @@ -208,7 +218,7 @@ public class RestMusicAdminAPI { resultMap.put("Success", "Your application has been deleted successfully"); } else { resultMap.put("Exception", - "Oops. Spomething went wrong. Please make sure Aid is correct or Application is onboarded"); + "Oops. Something went wrong. Please make sure Aid is correct or Application is onboarded"); } return resultMap; } @@ -325,7 +335,7 @@ public class RestMusicAdminAPI { resultMap.put("Success", "Your application has been updated successfully"); } else { resultMap.put("Exception", - "Oops. Spomething went wrong. Please make sure Aid is correct and application is onboarded"); + "Oops. Something went wrong. Please make sure Aid is correct and application is onboarded"); } return resultMap; diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index f1d88efa..e7af8f9f 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -43,6 +43,10 @@ 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; @@ -51,6 +55,8 @@ 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.att.eelf.configuration.EELFLogger; import com.datastax.driver.core.DataType; import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.Row; @@ -144,15 +150,21 @@ public class RestMusicDataAPI { JsonKeySpace kspObject, @ApiParam(value = "Keyspace Name", required = true) @PathParam("name") String keyspaceName, - @Context HttpServletResponse response) throws Exception { + @Context HttpServletResponse response) { Map<String, Object> resultMap = CachingUtil.verifyOnboarding(ns, userId, password); response.addHeader(xLatestVersion, MusicUtil.getVersion()); if (!resultMap.isEmpty()) { return resultMap; } - resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, - "createKeySpace"); + try { + resultMap = MusicCore.autheticateUser(ns, userId, password, keyspaceName, aid, + "createKeySpace"); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.applicationLogger, + "Exception while authenting the user."); + return resultMap; + } String newAid = null; if (!resultMap.isEmpty()) { if (resultMap.containsKey("aid")) { @@ -169,7 +181,12 @@ public class RestMusicDataAPI { boolean result = false; long start = System.currentTimeMillis(); Map<String, Object> replicationInfo = kspObject.getReplicationInfo(); - String repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}"; + String repString = null; + try { + repString = "{" + MusicUtil.jsonMaptoSqlString(replicationInfo, ",") + "}"; + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + } queryObject.appendQueryString( "CREATE KEYSPACE " + keyspaceName + " WITH replication = " + repString); if (kspObject.getDurabilityOfWrites() != null) { @@ -200,7 +217,12 @@ public class RestMusicDataAPI { queryObject.appendQueryString( "DELETE FROM admin.keyspace_master where uuid = " + newAid); queryObject.appendQueryString(";"); - result = MusicCore.nonKeyRelatedPut(queryObject, consistency); + try { + result = MusicCore.nonKeyRelatedPut(queryObject, consistency); + } catch (MusicServiceException e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put("Exception", e.getMessage()); + } resultMap.remove("aid"); resultMap.remove("uuid"); return resultMap; @@ -209,10 +231,23 @@ public class RestMusicDataAPI { queryObject = new PreparedQueryObject(); queryObject.appendQueryString( "UPDATE admin.keyspace_master SET keyspace_name=? where uuid = ?;"); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), + try { + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.text(), MusicUtil.DEFAULTKEYSPACENAME)); - queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), newAid)); - result = MusicCore.nonKeyRelatedPut(queryObject, consistency); + queryObject.addValue(MusicUtil.convertToActualDataType(DataType.uuid(), newAid)); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put("Exception", "Unable to process input data. Invalid input data type. " + + "Please check keyspace_name and aid.. "); + } + try { + result = MusicCore.nonKeyRelatedPut(queryObject, consistency); + } catch (MusicServiceException e) { + e.printStackTrace(); + logger.error(EELFLoggerDelegate.errorLogger, "Unable to process operation. Error: "+e.getMessage()); + resultMap.put("Exception", "Unable to process operation. Error: "+e.getMessage()); + return resultMap; + } resultMap.remove("aid"); resultMap.remove("uuid"); return resultMap; @@ -230,11 +265,18 @@ public class RestMusicDataAPI { queryObject.appendQueryString(";"); result = MusicCore.nonKeyRelatedPut(queryObject, consistency); } catch (Exception e) { - logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); } resultMap.remove("uuid"); - if (CachingUtil.isAAFApplication(ns)) - resultMap.remove("aid"); + try { + if (CachingUtil.isAAFApplication(ns)) + resultMap.remove("aid"); + } catch (MusicServiceException e) { + e.printStackTrace(); + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put("Exception", e.getMessage()); + return resultMap; + } resultMap.put("Status", String.valueOf(result)); return resultMap; @@ -348,6 +390,7 @@ public class RestMusicDataAPI { @ApiParam(value = "Table Name", required = true) @PathParam("tablename") String tablename, @Context HttpServletResponse response) throws Exception { + Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "createTable"); response.addHeader(xLatestVersion, MusicUtil.getVersion()); @@ -366,11 +409,17 @@ public class RestMusicDataAPI { int counter = 0; String primaryKey; for (Map.Entry<String, String> entry : fields.entrySet()) { - fieldsString.append("" + entry.getKey() + " " + entry.getValue() + ""); + if (entry.getKey().equals("PRIMARY KEY")) { - primaryKey = entry.getValue().substring(entry.getValue().indexOf('(') + 1); - primaryKey = primaryKey.substring(0, primaryKey.indexOf(')')); - } + if(! entry.getValue().contains("(")) + primaryKey = entry.getValue(); + else { + primaryKey = entry.getValue().substring(entry.getValue().indexOf('(') + 1); + primaryKey = primaryKey.substring(0, primaryKey.indexOf(')')); + } + fieldsString.append("" + entry.getKey() + " (" + primaryKey + ")"); + } else + fieldsString.append("" + entry.getKey() + " " + entry.getValue() + ""); if (counter == fields.size() - 1) fieldsString.append(")"); else @@ -500,9 +549,15 @@ public class RestMusicDataAPI { required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("tablename") String tablename, - @Context HttpServletResponse response) throws Exception { - Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "insertIntoTable"); + @Context HttpServletResponse response) { + Map<String, Object> resultMap = null; + try { + resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, + aid, "insertIntoTable"); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } response.addHeader(xLatestVersion, MusicUtil.getVersion()); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -512,7 +567,17 @@ public class RestMusicDataAPI { ReturnType result = null; Map<String, Object> valuesMap = insObj.getValues(); PreparedQueryObject queryObject = new PreparedQueryObject(); - TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + TableMetadata tableInfo = null; + try { + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + if(tableInfo == null) + throw new MusicServiceException("Table name doesn't exists. Please check the table name."); + } catch (MusicServiceException e) { + e.printStackTrace(); + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put(ResultType.SYNTAXERROR.getResult(), e.getMessage()); + return resultMap; + } String primaryKeyName = tableInfo.getPrimaryKey().get(0).getName(); StringBuilder fieldsString = new StringBuilder("(vector_ts,"); String vectorTs = @@ -532,7 +597,13 @@ public class RestMusicDataAPI { DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object formattedValue = MusicUtil.convertToActualDataType(colType, valueObj); + Object formattedValue = null; + try { + formattedValue = MusicUtil.convertToActualDataType(colType, valueObj); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + //return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } valueString.append("?"); queryObject.addValue(formattedValue); @@ -593,7 +664,7 @@ public class RestMusicDataAPI { : new ReturnType(ResultType.FAILURE, "Null result - Please Contact admin").toMap(); } catch (Exception ex) { - logger.info(EELFLoggerDelegate.applicationLogger, ex.getMessage()); + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage(), AppMessages.UNKNOWNERROR ,ErrorSeverity.WARN, ErrorTypes.MUSICSERVICEERROR); return new ReturnType(ResultType.FAILURE, ex.getMessage()).toMap(); } } @@ -631,9 +702,15 @@ public class RestMusicDataAPI { required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("tablename") String tablename, - @Context UriInfo info, @Context HttpServletResponse response) throws Exception { - Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "updateTable"); + @Context UriInfo info, @Context HttpServletResponse response) { + Map<String, Object> resultMap; + try { + resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, + aid, "updateTable"); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } response.addHeader(xLatestVersion, MusicUtil.getVersion()); if (resultMap.containsKey("aid")) resultMap.remove("aid"); @@ -651,7 +728,15 @@ public class RestMusicDataAPI { PreparedQueryObject queryObject = new PreparedQueryObject(); Map<String, Object> valuesMap = updateObj.getValues(); - TableMetadata tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + TableMetadata tableInfo; + try { + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + } catch (MusicServiceException e) { + e.printStackTrace(); + logger.error(EELFLoggerDelegate.errorLogger, e.getMessage()); + resultMap.put("Exception", e.getMessage()); + return resultMap; + } if (tableInfo == null) { return new ReturnType(ResultType.FAILURE, "Table information not found. Please check input for table name= " @@ -665,7 +750,13 @@ public class RestMusicDataAPI { 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); + Object valueString = null; + try { + valueString = MusicUtil.convertToActualDataType(colType, valueObj); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + //return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } fieldValueString.append(entry.getKey() + "= ?"); queryObject.addValue(valueString); if (counter != valuesMap.size() - 1) @@ -699,7 +790,12 @@ public class RestMusicDataAPI { RowIdentifier rowId = null; try { rowId = getRowIdentifier(keyspace, tablename, info.getQueryParameters(), queryObject); + if(rowId == null || rowId.primarKeyValue.isEmpty()) { + resultMap.put(ResultType.SYNTAXERROR.getResult(), "Mandatory WHERE clause is missing. Please check the input request."); + return resultMap; + } } catch (MusicServiceException ex) { + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage()); return new ReturnType(ResultType.FAILURE, ex.getMessage()).toMap(); } @@ -730,11 +826,21 @@ public class RestMusicDataAPI { queryObject, lockId, conditionInfo); } else if (consistency.equalsIgnoreCase("atomic_delete_lock")) { // this function is mainly for the benchmarks - operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, - rowId.primarKeyValue, queryObject, conditionInfo); + try { + operationResult = MusicCore.atomicPutWithDeleteLock(keyspace, tablename, + rowId.primarKeyValue, queryObject, conditionInfo); + } catch (MusicLockingException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } } else if (consistency.equalsIgnoreCase(MusicUtil.ATOMIC)) { - operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, - queryObject, conditionInfo); + try { + operationResult = MusicCore.atomicPut(keyspace, tablename, rowId.primarKeyValue, + queryObject, conditionInfo); + } catch (MusicLockingException e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + return new ReturnType(ResultType.FAILURE, e.getMessage()).toMap(); + } } long actualUpdateCompletionTime = System.currentTimeMillis(); @@ -788,15 +894,25 @@ public class RestMusicDataAPI { required = true) @PathParam("keyspace") String keyspace, @ApiParam(value = "Table Name", required = true) @PathParam("tablename") String tablename, - @Context UriInfo info, @Context HttpServletResponse response) throws Exception { - Map<String, Object> resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, - aid, "deleteFromTable"); + @Context UriInfo info, @Context HttpServletResponse response) { + Map<String, Object> resultMap = null; + try { + resultMap = MusicCore.autheticateUser(ns, userId, password, keyspace, + aid, "deleteFromTable"); + } catch (Exception e) { + resultMap.put("Exception", e.getMessage()); + return resultMap; + } response.addHeader(xLatestVersion, MusicUtil.getVersion()); if (resultMap.containsKey("aid")) resultMap.remove("aid"); if (!resultMap.isEmpty()) { return resultMap; } + if(delObj == null) { + resultMap.put("Exception", "Request body is missing. Please check your input data and retry."); + return resultMap; + } PreparedQueryObject queryObject = new PreparedQueryObject(); StringBuilder columnString = new StringBuilder(); @@ -851,21 +967,25 @@ public class RestMusicDataAPI { String consistency = delObj.getConsistencyInfo().get("type"); ReturnType operationResult = null; - - if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) - operationResult = MusicCore.eventualPut(queryObject); - else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { - String lockId = delObj.getConsistencyInfo().get("lockId"); - 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); - } + try { + if (consistency.equalsIgnoreCase(MusicUtil.EVENTUAL)) + operationResult = MusicCore.eventualPut(queryObject); + else if (consistency.equalsIgnoreCase(MusicUtil.CRITICAL)) { + String lockId = delObj.getConsistencyInfo().get("lockId"); + 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) { + resultMap.put("Exception", "Unable to perform Delete operation. Exception from music: "+e.getMessage()); + return resultMap; + } try { return operationResult.toMap(); } catch (NullPointerException e) { @@ -1039,7 +1159,7 @@ public class RestMusicDataAPI { if (resultMap.containsKey("aid")) resultMap.remove("aid"); if (!resultMap.isEmpty()) { - logger.error("Error while authentication... "); + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.AUTHENTICATIONERROR ,ErrorSeverity.WARN, ErrorTypes.AUTHENTICATIONERROR); HashMap<String, Object> tempMap = new HashMap<>(); tempMap.putAll(resultMap); Map<String, HashMap<String, Object>> results = new HashMap<>(); @@ -1064,6 +1184,7 @@ public class RestMusicDataAPI { ResultSet results = MusicCore.get(queryObject); return MusicCore.marshallResults(results); } catch (MusicServiceException ex) { + logger.error(EELFLoggerDelegate.errorLogger,"", AppMessages.UNKNOWNERROR ,ErrorSeverity.ERROR, ErrorTypes.MUSICSERVICEERROR); return MusicUtil.setErrorResponse(ex); } @@ -1077,7 +1198,6 @@ public class RestMusicDataAPI { * @param limit * @return * @throws MusicServiceException - * @throws Exception */ public PreparedQueryObject selectSpecificQuery(String version, String minorVersion, String patchVersion, String aid, String ns, String userId, String password, @@ -1108,7 +1228,6 @@ public class RestMusicDataAPI { * @param queryObject * @return * @throws MusicServiceException - * @throws Exception */ private RowIdentifier getRowIdentifier(String keyspace, String tablename, MultivaluedMap<String, String> rowParams, PreparedQueryObject queryObject) @@ -1130,7 +1249,12 @@ public class RestMusicDataAPI { List<String> valueList = entry.getValue(); String indValue = valueList.get(0); DataType colType = tableInfo.getColumn(entry.getKey()).getType(); - Object formattedValue = MusicUtil.convertToActualDataType(colType, indValue); + Object formattedValue = null; + try { + formattedValue = MusicUtil.convertToActualDataType(colType, indValue); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger,e.getMessage()); + } primaryKey.append(indValue); rowSpec.append(keyName + "= ?"); queryObject.addValue(formattedValue); |