From 03ade18ac83ab02ee1ce0d6d43725b96513b4577 Mon Sep 17 00:00:00 2001 From: "Thomas Nelson Jr (arthurdent3) tn1381@att.com" Date: Tue, 5 Jun 2018 14:09:02 -0400 Subject: Update Master to include jar folder. This will put jar folder form separate branch into this where it belongs and we can build properly for beijing. Cassablanca will have a parent pom approach. Change-Id: Ieae3fce5ab42faddf4964285533fdc59d8b963de Issue-ID: MUSIC-79 Signed-off-by: Thomas Nelson Jr (arthurdent3) tn1381@att.com --- .../conductor/conditionals/JsonConditional.java | 89 +++++ .../conductor/conditionals/MusicContional.java | 357 +++++++++++++++++++++ .../conditionals/RestMusicConditonalAPI.java | 218 +++++++++++++ .../org/onap/music/datastore/MusicDataStore.java | 2 + .../music/lockingservice/MusicLockingService.java | 9 +- src/main/java/org/onap/music/main/CachingUtil.java | 27 +- src/main/java/org/onap/music/main/MusicCore.java | 2 + src/main/java/org/onap/music/main/MusicUtil.java | 16 +- .../java/org/onap/music/rest/RestMusicDataAPI.java | 11 +- 9 files changed, 712 insertions(+), 19 deletions(-) create mode 100644 src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java create mode 100644 src/main/java/org/onap/music/conductor/conditionals/MusicContional.java create mode 100644 src/main/java/org/onap/music/conductor/conditionals/RestMusicConditonalAPI.java (limited to 'src/main/java') diff --git a/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java b/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java new file mode 100644 index 00000000..0e971eb6 --- /dev/null +++ b/src/main/java/org/onap/music/conductor/conditionals/JsonConditional.java @@ -0,0 +1,89 @@ +/* + * ============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.conductor.conditionals; + +import java.io.Serializable; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import io.swagger.annotations.ApiModel; + +@ApiModel(value = "JsonConditional", description = "Json model for insert or update into table based on some conditions") +@JsonIgnoreProperties(ignoreUnknown = true) +public class JsonConditional implements Serializable { + + private String primaryKey; + private String primaryKeyValue; + private String casscadeColumnName; + private Map tableValues; + private Map casscadeColumnData; + private Map> conditions; + + public Map getTableValues() { + return tableValues; + } + public void setTableValues(Map tableValues) { + this.tableValues = tableValues; + } + + public String getPrimaryKey() { + return primaryKey; + } + public String getPrimaryKeyValue() { + return primaryKeyValue; + } + public String getCasscadeColumnName() { + return casscadeColumnName; + } + + public Map getCasscadeColumnData() { + return casscadeColumnData; + } + + + + public void setPrimaryKey(String primaryKey) { + this.primaryKey = primaryKey; + } + public void setPrimaryKeyValue(String primaryKeyValue) { + this.primaryKeyValue = primaryKeyValue; + } + public Map> getConditions() { + return conditions; + } + public void setConditions(Map> conditions) { + this.conditions = conditions; + } + public void setCasscadeColumnName(String casscadeColumnName) { + this.casscadeColumnName = casscadeColumnName; + } + + public void setCasscadeColumnData(Map casscadeColumnData) { + this.casscadeColumnData = casscadeColumnData; + } + + + + + +} diff --git a/src/main/java/org/onap/music/conductor/conditionals/MusicContional.java b/src/main/java/org/onap/music/conductor/conditionals/MusicContional.java new file mode 100644 index 00000000..93e34721 --- /dev/null +++ b/src/main/java/org/onap/music/conductor/conditionals/MusicContional.java @@ -0,0 +1,357 @@ +/* + * ============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.conductor.conditionals; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; + +import javax.ws.rs.core.Response.Status; + +import org.codehaus.jettison.json.JSONObject; +import org.onap.music.datastore.PreparedQueryObject; +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 org.onap.music.rest.RestMusicDataAPI; + +import com.datastax.driver.core.ColumnDefinitions; +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.Row; +import com.datastax.driver.core.TableMetadata; + +public class MusicContional { + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicDataAPI.class); + + public static ReturnType conditionalInsert(String keyspace, String tablename, String casscadeColumnName, + Map casscadeColumnData, String primaryKey, Map valuesMap, + Map status) throws Exception { + + Map queryBank = new HashMap<>(); + TableMetadata tableInfo = null; + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType(); + String primaryId = tableInfo.getPrimaryKey().get(0).getName(); + DataType casscadeColumnType = tableInfo.getColumn(casscadeColumnName).getType(); + String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + + PreparedQueryObject select = new PreparedQueryObject(); + select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?"); + select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey)); + queryBank.put(MusicUtil.SELECT, select); + + PreparedQueryObject update = new PreparedQueryObject(); + Map updateColumnvalues = new HashMap<>(); //casscade column values + updateColumnvalues = getValues(true, casscadeColumnData, status); + Object formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, updateColumnvalues); + update.appendQueryString("UPDATE " + keyspace + "." + tablename + " SET " + casscadeColumnName + " =" + + casscadeColumnName + " + ? , vector_ts = ?" + " WHERE " + primaryId + " = ? "); + update.addValue(formatedValues); + update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector)); + update.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKey)); + queryBank.put(MusicUtil.UPDATE, update); + + Map insertColumnvalues = new HashMap<>(); + insertColumnvalues = getValues(false, casscadeColumnData, status); + formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, insertColumnvalues); + PreparedQueryObject insert = extractQuery(valuesMap, tableInfo, tablename, keyspace, primaryId, primaryKey,casscadeColumnName,formatedValues); + queryBank.put(MusicUtil.INSERT, insert); + + + String key = keyspace + "." + tablename + "." + primaryKey; + String lockId = MusicCore.createLockReference(key); + long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); + ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod); + + try { + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + ReturnType criticalPutResult = conditionalInsertAtomic(lockId, keyspace, tablename, primaryKey, + queryBank); + MusicCore.destroyLockRef(lockId); + if (criticalPutResult.getMessage().contains("insert")) + criticalPutResult + .setMessage("Insert values: "); + else if (criticalPutResult.getMessage().contains("update")) + criticalPutResult + .setMessage("Update values: " + updateColumnvalues); + return criticalPutResult; + + } else { + MusicCore.destroyLockRef(lockId); + return lockAcqResult; + } + } catch (Exception e) { + MusicCore.destroyLockRef(lockId); + return new ReturnType(ResultType.FAILURE, e.getMessage()); + } + + } + + public static ReturnType conditionalInsertAtomic(String lockId, String keyspace, String tableName, + String primaryKey, Map queryBank) { + + ResultSet results = null; + + try { + + MusicLockState mls = MusicCore.getLockingServiceHandle() + .getLockState(keyspace + "." + tableName + "." + primaryKey); + if (mls.getLockHolder().equals(lockId) == true) { + try { + results = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)); + } catch (Exception e) { + return new ReturnType(ResultType.FAILURE, e.getMessage()); + } + if (results.all().isEmpty()) { + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.INSERT), "critical"); + return new ReturnType(ResultType.SUCCESS, "insert"); + } else { + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPDATE), "critical"); + return new ReturnType(ResultType.SUCCESS, "update"); + } + } else { + return new ReturnType(ResultType.FAILURE, + "Cannot perform operation since you are the not the lock holder"); + } + + } catch (Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + String exceptionAsString = sw.toString(); + return new ReturnType(ResultType.FAILURE, + "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" + + exceptionAsString); + } + + } + + public static ReturnType update(Map queryBank, String keyspace, String tableName, String primaryKey,String primaryKeyValue,String planId,String cascadeColumnName,Map cascadeColumnValues) { + + String key = keyspace + "." + tableName + "." + primaryKeyValue; + String lockId = MusicCore.createLockReference(key); + long leasePeriod = MusicUtil.getDefaultLockLeasePeriod(); + ReturnType lockAcqResult = MusicCore.acquireLockWithLease(key, lockId, leasePeriod); + + try { + + if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) { + return updateAtomic(lockId, keyspace, tableName, primaryKey,primaryKeyValue, queryBank,planId,cascadeColumnValues,cascadeColumnName); + + } else { + MusicCore.destroyLockRef(lockId); + return lockAcqResult; + } + + } catch (Exception e) { + MusicCore.destroyLockRef(lockId); + return new ReturnType(ResultType.FAILURE, e.getMessage()); + + } + } + + public static ReturnType updateAtomic(String lockId, String keyspace, String tableName, String primaryKey,String primaryKeyValue, + Map queryBank,String planId,Map cascadeColumnValues,String casscadeColumnName) { + try { + + MusicLockState mls = MusicCore.getLockingServiceHandle() + .getLockState(keyspace + "." + tableName + "." + primaryKeyValue); + if (mls.getLockHolder().equals(lockId) == true) { + Row row = MusicCore.getDSHandle().executeCriticalGet(queryBank.get(MusicUtil.SELECT)).one(); + + if(row != null) { + Map updatedValues = cascadeColumnUpdateSpecific(row, cascadeColumnValues, casscadeColumnName, planId); + JSONObject json = new JSONObject(updatedValues); + PreparedQueryObject update = new PreparedQueryObject(); + String vector_ts = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + update.appendQueryString("UPDATE " + keyspace + "." + tableName + " SET " + casscadeColumnName + "['" + planId + + "'] = ?, vector_ts = ? WHERE " + primaryKey + " = ?"); + update.addValue(MusicUtil.convertToActualDataType(DataType.text(), json.toString())); + update.addValue(MusicUtil.convertToActualDataType(DataType.text(), vector_ts)); + update.addValue(MusicUtil.convertToActualDataType(DataType.text(), primaryKeyValue)); + try { + MusicCore.getDSHandle().executePut(update, "critical"); + } catch (Exception ex) { + return new ReturnType(ResultType.FAILURE, ex.getMessage()); + } + }else { + return new ReturnType(ResultType.FAILURE,"Cannot find data related to key: "+primaryKey); + } + + MusicCore.getDSHandle().executePut(queryBank.get(MusicUtil.UPSERT), "critical"); + return new ReturnType(ResultType.SUCCESS, "update success"); + + } else { + return new ReturnType(ResultType.FAILURE, + "Cannot perform operation since you are the not the lock holder"); + } + + } catch (Exception e) { + StringWriter sw = new StringWriter(); + e.printStackTrace(new PrintWriter(sw)); + String exceptionAsString = sw.toString(); + return new ReturnType(ResultType.FAILURE, + "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" + + exceptionAsString); + } + + } + + @SuppressWarnings("unchecked") + public static Map getValues(boolean isExists, Map casscadeColumnData, + Map status) { + + Map value = new HashMap<>(); + Map returnMap = new HashMap<>(); + Object key = casscadeColumnData.get("key"); + String setStatus = ""; + value = (Map) casscadeColumnData.get("value"); + + if (isExists) + setStatus = status.get("exists"); + else + setStatus = status.get("nonexists"); + + value.put("status", setStatus); + JSONObject valueJson = new JSONObject(value); + returnMap.put(key.toString(), valueJson.toString()); + return returnMap; + + } + + + public static PreparedQueryObject extractQuery(Map valuesMap, TableMetadata tableInfo, String tableName, + String keySpaceName,String primaryKeyName,String primaryKey,String casscadeColumn,Object casscadeColumnValues) throws Exception { + + PreparedQueryObject queryObject = new PreparedQueryObject(); + StringBuilder fieldsString = new StringBuilder("(vector_ts"+","); + StringBuilder valueString = new StringBuilder("(" + "?" + ","); + String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis()); + queryObject.addValue(vector); + if(casscadeColumn!=null && casscadeColumnValues!=null) { + fieldsString.append("" +casscadeColumn+" ," ); + valueString.append("?,"); + queryObject.addValue(casscadeColumnValues); + } + + int counter = 0; + for (Map.Entry 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); + + } + + 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; + } + queryObject.appendQueryString("INSERT INTO " + keySpaceName + "." + tableName + " " + + fieldsString + " VALUES " + valueString); + return queryObject; + } + + public static Object getColValue(Row row, String colName, DataType colType) { + switch (colType.getName()) { + case VARCHAR: + return row.getString(colName); + case UUID: + return row.getUUID(colName); + case VARINT: + return row.getVarint(colName); + case BIGINT: + return row.getLong(colName); + case INT: + return row.getInt(colName); + case FLOAT: + return row.getFloat(colName); + case DOUBLE: + return row.getDouble(colName); + case BOOLEAN: + return row.getBool(colName); + case MAP: + return row.getMap(colName, String.class, String.class); + default: + return null; + } + } + + @SuppressWarnings("unchecked") + public static Map cascadeColumnUpdateSpecific(Row row, Map changeOfStatus, + String cascadeColumnName, String planId) { + + ColumnDefinitions colInfo = row.getColumnDefinitions(); + DataType colType = colInfo.getType(cascadeColumnName); + Map values = new HashMap<>(); + Object columnValue = getColValue(row, cascadeColumnName, colType); + + Map finalValues = new HashMap<>(); + values = (Map) columnValue; + if (values.keySet().contains(planId)) { + String valueString = values.get(planId); + String tempValueString = valueString.replaceAll("\\{", "").replaceAll("\"", "").replaceAll("\\}", ""); + String[] elements = tempValueString.split(","); + for (String str : elements) { + String[] keyValue = str.split(":"); + if ((changeOfStatus.keySet().contains(keyValue[0].replaceAll("\\s", "")))) + keyValue[1] = changeOfStatus.get(keyValue[0].replaceAll("\\s", "")); + finalValues.put(keyValue[0], keyValue[1]); + } + } + return finalValues; + + } + +} diff --git a/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditonalAPI.java b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditonalAPI.java new file mode 100644 index 00000000..c13dd621 --- /dev/null +++ b/src/main/java/org/onap/music/conductor/conditionals/RestMusicConditonalAPI.java @@ -0,0 +1,218 @@ +/* + * ============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.conductor.conditionals; + +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import javax.ws.rs.Consumes; +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.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import javax.ws.rs.core.Response.Status; + +import org.codehaus.jettison.json.JSONObject; +import org.onap.music.datastore.PreparedQueryObject; +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.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 org.onap.music.rest.RestMusicAdminAPI; + +import com.datastax.driver.core.DataType; +import com.datastax.driver.core.TableMetadata; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiParam; + +@Path("/v2/conditional") +@Api(value = "Conditional Api", hidden = true) +public class RestMusicConditonalAPI { + private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RestMusicAdminAPI.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"; + + @POST + @Path("/insert/keyspaces/{keyspace}/tables/{tablename}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response insertConditional( + @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 = "userId", required = true) @HeaderParam(USERID) String userId, + @ApiParam(value = "Password", required = true) @HeaderParam(PASSWORD) String password, + @ApiParam(value = "Major Version", required = true) @PathParam("keyspace") String keyspace, + @ApiParam(value = "Major Version", required = true) @PathParam("tablename") String tablename, + JsonConditional jsonObj) throws Exception { + ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); + String primaryKey = jsonObj.getPrimaryKey(); + String primaryKeyValue = jsonObj.getPrimaryKeyValue(); + String casscadeColumnName = jsonObj.getCasscadeColumnName(); + Map tableValues = jsonObj.getTableValues(); + Map casscadeColumnData = jsonObj.getCasscadeColumnData(); + Map> conditions = jsonObj.getConditions(); + + if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null || tableValues.isEmpty() + || casscadeColumnData.isEmpty() || conditions.isEmpty()) { + logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE) + .setError(String.valueOf("One or more input values missing")).toMap()).build(); + + } + + Map 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 valuesMap = new LinkedHashMap<>(); + for (Map.Entry entry : tableValues.entrySet()) { + valuesMap.put(entry.getKey(), entry.getValue()); + } + + Map status = new HashMap<>(); + status.put("exists", conditions.get("exists").get("status").toString()); + status.put("nonexists", conditions.get("nonexists").get("status").toString()); + ReturnType out = null; + + out = MusicContional.conditionalInsert(keyspace, tablename, casscadeColumnName, casscadeColumnData, + primaryKeyValue, valuesMap, status); + return response.status(Status.OK).entity(new JsonResponse(out.getResult()).setMessage(out.getMessage()).toMap()) + .build(); + + } + + @SuppressWarnings("unchecked") + @PUT + @Path("/update/keyspaces/{keyspace}/tables/{tablename}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response updateConditional( + @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 = "userId", required = true) @HeaderParam(USERID) String userId, + @ApiParam(value = "Password", required = true) @HeaderParam(PASSWORD) String password, + @ApiParam(value = "Major Version", required = true) @PathParam("keyspace") String keyspace, + @ApiParam(value = "Major Version", required = true) @PathParam("tablename") String tablename, + JsonConditional upObj) throws Exception { + ResponseBuilder response = MusicUtil.buildVersionResponse(VERSION, minorVersion, patchVersion); + + String primaryKey = upObj.getPrimaryKey(); + String primaryKeyValue = upObj.getPrimaryKeyValue(); + String casscadeColumnName = upObj.getCasscadeColumnName(); + Map casscadeColumnData = upObj.getCasscadeColumnData(); + Map tableValues = upObj.getTableValues(); + + if (primaryKey == null || primaryKeyValue == null || casscadeColumnName == null + || casscadeColumnData.isEmpty()) { + logger.error(EELFLoggerDelegate.errorLogger, "", AppMessages.MISSINGINFO, ErrorSeverity.CRITICAL, + ErrorTypes.AUTHENTICATIONERROR); + return response.status(Status.UNAUTHORIZED).entity(new JsonResponse(ResultType.FAILURE) + .setError(String.valueOf("One or more input values missing")).toMap()).build(); + + } + + Map authMap = null; + try { + authMap = MusicCore.autheticateUser(ns, userId, password, keyspace, aid, "updateTable"); + } 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(); + } + + String planId = casscadeColumnData.get("key").toString(); + Map casscadeColumnValueMap = (Map) casscadeColumnData.get("value"); + TableMetadata tableInfo = null; + tableInfo = MusicCore.returnColumnMetadata(keyspace, tablename); + DataType primaryIdType = tableInfo.getPrimaryKey().get(0).getType(); + String primaryId = tableInfo.getPrimaryKey().get(0).getName(); + + PreparedQueryObject upsert = MusicContional.extractQuery(tableValues, tableInfo, tablename, keyspace, primaryKey, primaryKeyValue, null, null); + + PreparedQueryObject select = new PreparedQueryObject(); + select.appendQueryString("SELECT * FROM " + keyspace + "." + tablename + " where " + primaryId + " = ?"); + select.addValue(MusicUtil.convertToActualDataType(primaryIdType, primaryKeyValue)); + + Map queryBank = new HashMap<>(); + //queryBank.put(MusicUtil.UPDATE, update); + queryBank.put(MusicUtil.UPSERT, upsert); + queryBank.put(MusicUtil.SELECT, select); + ReturnType result = MusicContional.update(queryBank, keyspace, tablename, primaryKey,primaryKeyValue,planId,casscadeColumnName,casscadeColumnValueMap); + if (result.getResult() == ResultType.SUCCESS) { + return response.status(Status.OK) + .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build(); + + } + return response.status(Status.BAD_REQUEST) + .entity(new JsonResponse(result.getResult()).setMessage(result.getMessage()).toMap()).build(); + + } + +} diff --git a/src/main/java/org/onap/music/datastore/MusicDataStore.java b/src/main/java/org/onap/music/datastore/MusicDataStore.java index 278cf768..e9356f9d 100644 --- a/src/main/java/org/onap/music/datastore/MusicDataStore.java +++ b/src/main/java/org/onap/music/datastore/MusicDataStore.java @@ -253,6 +253,8 @@ public class MusicDataStore { return row.getBool(colName); case MAP: return row.getMap(colName, String.class, String.class); + case LIST: + return row.getList(colName, String.class); default: return null; } diff --git a/src/main/java/org/onap/music/lockingservice/MusicLockingService.java b/src/main/java/org/onap/music/lockingservice/MusicLockingService.java index ae026903..d0c33000 100644 --- a/src/main/java/org/onap/music/lockingservice/MusicLockingService.java +++ b/src/main/java/org/onap/music/lockingservice/MusicLockingService.java @@ -19,6 +19,8 @@ package org.onap.music.lockingservice; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.StringTokenizer; import java.util.concurrent.CountDownLatch; @@ -97,7 +99,12 @@ public class MusicLockingService implements Watcher { try{ data = zkLockHandle.getNodeData(lockName); }catch (Exception ex){ - logger.error(EELFLoggerDelegate.errorLogger, ex.getMessage(),AppMessages.UNKNOWNERROR, ErrorSeverity.ERROR, ErrorTypes.LOCKINGERROR); + StringWriter sw = new StringWriter(); + ex.printStackTrace(); + ex.printStackTrace(new PrintWriter(sw)); + String exceptionAsString = sw.toString(); + logger.error(EELFLoggerDelegate.errorLogger,exceptionAsString); + throw new MusicLockingException(exceptionAsString); } if(data !=null) return MusicLockState.deSerialize(data); diff --git a/src/main/java/org/onap/music/main/CachingUtil.java b/src/main/java/org/onap/music/main/CachingUtil.java index b59e81b3..26491302 100755 --- a/src/main/java/org/onap/music/main/CachingUtil.java +++ b/src/main/java/org/onap/music/main/CachingUtil.java @@ -101,8 +101,8 @@ public class CachingUtil implements Runnable { String keySpace = row.getString("application_name"); try { userAttempts.put(nameSpace, 0); - AAFResponse responseObj = triggerAAF(nameSpace, userId, password); - if (responseObj.getNs().size() > 0) { + boolean aafRresponse = triggerAAF(nameSpace, userId, password); + if (aafRresponse) { map = new HashMap<>(); map.put(userId, password); aafCache.put(nameSpace, map); @@ -163,20 +163,21 @@ public class CachingUtil implements Runnable { } } - AAFResponse responseObj = triggerAAF(nameSpace, userId, password); - if (responseObj.getNs().size() > 0) { - if (responseObj.getNs().get(0).getAdmin().contains(userId)) { + boolean aafRresponse = triggerAAF(nameSpace, userId, password); + if (aafRresponse) { + //TODO + //if (responseObj.getNs().get(0).getAdmin().contains(userId)) { //Map map = new HashMap<>(); //map.put(userId, password); //aafCache.put(nameSpace, map); return true; - } + //} } logger.info(EELFLoggerDelegate.applicationLogger,"Invalid user. Cache not updated"); - return false; + return aafRresponse; } - private static AAFResponse triggerAAF(String nameSpace, String userId, String password) + private static boolean triggerAAF(String nameSpace, String userId, String password) throws Exception { if (MusicUtil.getAafEndpointUrl() == null) { logger.error(EELFLoggerDelegate.errorLogger,"",AppMessages.UNKNOWNERROR,ErrorSeverity.WARN, ErrorTypes.GENERALSERVICEERROR); @@ -194,7 +195,9 @@ public class CachingUtil implements Runnable { ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON) .header("Authorization", "Basic " + base64Creds) .header("content-type", "application/json").get(ClientResponse.class); - if (response.getStatus() != 200) { + if(response.getStatus() == 200) + return true; + else if (response.getStatus() != 200) { if (userAttempts.get(nameSpace) == null) userAttempts.put(nameSpace, 0); if ((Integer) userAttempts.get(nameSpace) >= 2) { @@ -209,14 +212,14 @@ public class CachingUtil implements Runnable { // TODO Allow for 2-3 times and forbid any attempt to trigger AAF with invalid values // for specific time. } - response.getHeaders().put(HttpHeaders.CONTENT_TYPE, + /*response.getHeaders().put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON)); // AAFResponse output = response.getEntity(AAFResponse.class); response.bufferEntity(); String x = response.getEntity(String.class); - AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class); + AAFResponse responseObj = new ObjectMapper().readValue(x, AAFResponse.class);*/ - return responseObj; + return false; } public static void updateMusicCache(String keyspace, String nameSpace) { diff --git a/src/main/java/org/onap/music/main/MusicCore.java b/src/main/java/org/onap/music/main/MusicCore.java index dfc93ccc..cc147d57 100644 --- a/src/main/java/org/onap/music/main/MusicCore.java +++ b/src/main/java/org/onap/music/main/MusicCore.java @@ -22,6 +22,7 @@ package org.onap.music.main; +import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; @@ -664,6 +665,7 @@ public class MusicCore { "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n" + e.getMessage()); }catch(MusicLockingException ex){ + logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage()); return new ReturnType(ResultType.FAILURE,ex.getMessage()); } diff --git a/src/main/java/org/onap/music/main/MusicUtil.java b/src/main/java/org/onap/music/main/MusicUtil.java index 5b442a79..20bb0a48 100755 --- a/src/main/java/org/onap/music/main/MusicUtil.java +++ b/src/main/java/org/onap/music/main/MusicUtil.java @@ -54,6 +54,10 @@ public class MusicUtil { private static final String XLATESTVERSION = "X-latestVersion"; private static final String XMINORVERSION = "X-minorVersion"; private static final String XPATCHVERSION = "X-patchVersion"; + public static final String SELECT = "select"; + public static final String INSERT = "insert"; + public static final String UPDATE = "update"; + public static final String UPSERT = "upsert"; private static final String LOCALHOST = "localhost"; private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties"; @@ -343,7 +347,8 @@ public class MusicUtil { try { Scanner fileScanner = new Scanner(new File("")); testType = fileScanner.next();// ignore the my id line - String batchSize = fileScanner.next();// ignore the my public ip + @SuppressWarnings("unused") + String batchSize = fileScanner.next();// ignore the my public ip // line fileScanner.close(); } catch (FileNotFoundException e) { @@ -411,7 +416,8 @@ public class MusicUtil { value = "'" + valueString + "'"; break; case MAP: { - Map otMap = (Map) valueObj; + @SuppressWarnings("unchecked") + Map otMap = (Map) valueObj; value = "{" + jsonMaptoSqlString(otMap, ",") + "}"; break; } @@ -430,7 +436,8 @@ public class MusicUtil { * @throws MusicTypeConversionException * @throws Exception */ - public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception { + @SuppressWarnings("unchecked") + public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception { String valueObjString = valueObj + ""; switch (colType.getName()) { case UUID: @@ -449,6 +456,8 @@ public class MusicUtil { return Boolean.parseBoolean(valueObjString); case MAP: return (Map) valueObj; + case LIST: + return (List)valueObj; default: return valueObjString; } @@ -480,7 +489,6 @@ public class MusicUtil { return sqlString.toString(); } - @SuppressWarnings("unused") public static String buildVersion(String major, String minor, String patch) { if (minor != null) { major += "." + minor; diff --git a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java index 93b8f82a..39d5a890 100755 --- a/src/main/java/org/onap/music/rest/RestMusicDataAPI.java +++ b/src/main/java/org/onap/music/rest/RestMusicDataAPI.java @@ -1139,7 +1139,10 @@ public class RestMusicDataAPI { results = MusicCore.atomicGetWithDeleteLock(keyspace, tablename, rowId.primarKeyValue, queryObject); } - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); + 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").setDataResult(MusicCore.marshallResults(results)).toMap()).build(); } /** @@ -1200,7 +1203,10 @@ public class RestMusicDataAPI { try { ResultSet results = MusicCore.get(queryObject); - return response.status(Status.OK).entity(new JsonResponse(ResultType.SUCCESS).setDataResult(MusicCore.marshallResults(results)).toMap()).build(); + 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").setDataResult(MusicCore.marshallResults(results)).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(); @@ -1274,6 +1280,7 @@ public class RestMusicDataAPI { } 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); -- cgit 1.2.3-korg