Log stack traces with logger
[music.git] / src / main / java / org / onap / music / conductor / conditionals / MusicConditional.java
index 58409b7..69ccd10 100644 (file)
@@ -4,6 +4,9 @@
  * ===================================================================
  *  Copyright (c) 2017 AT&T Intellectual Property
  * ===================================================================
+ *  Modifications Copyright (C) 2019 IBM.
+ *  Modifications Copyright (c) 2019 Samsung
+ * ===================================================================
  *  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
@@ -37,13 +40,11 @@ import org.onap.music.eelf.logging.format.ErrorTypes;
 import org.onap.music.exceptions.MusicLockingException;
 import org.onap.music.exceptions.MusicQueryException;
 import org.onap.music.exceptions.MusicServiceException;
-import org.onap.music.lockingservice.cassandra.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.rest.RestMusicDataAPI;
-import org.onap.music.service.impl.MusicZKCore;
 
 import com.datastax.driver.core.ColumnDefinitions;
 import com.datastax.driver.core.DataType;
@@ -72,8 +73,8 @@ public class MusicConditional {
         queryBank.put(MusicUtil.SELECT, select);
 
         PreparedQueryObject update = new PreparedQueryObject();
-        Map<String, String> updateColumnvalues = new HashMap<>(); //casscade column values
-        updateColumnvalues = getValues(true, casscadeColumnData, status);
+        //casscade column values
+        Map<String, String> updateColumnvalues = getValues(true, casscadeColumnData, status);
         Object formatedValues = MusicUtil.convertToActualDataType(casscadeColumnType, updateColumnvalues);
         update.appendQueryString("UPDATE " + keyspace + "." + tablename + " SET " + casscadeColumnName + " ="
                 + casscadeColumnName + " + ? , vector_ts = ?" + " WHERE " + primaryId + " = ? ");
@@ -83,8 +84,8 @@ public class MusicConditional {
         queryBank.put(MusicUtil.UPDATE, update);
 
 
-        Map<String, String> insertColumnvalues = new HashMap<>();//casscade column values
-        insertColumnvalues = getValues(false, casscadeColumnData, status);
+        //casscade column values
+        Map<String, String> 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);
@@ -113,6 +114,7 @@ public class MusicConditional {
                 return lockAcqResult;
             }
         } catch (Exception e) {
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             MusicCore.destroyLockRef(lockId);
             return new ReturnType(ResultType.FAILURE, e.getMessage());
         }
@@ -127,12 +129,11 @@ public class MusicConditional {
         try {
             String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKey;
             ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId);
-            //MusicLockState mls = MusicZKCore.getLockingServiceHandle()
-                    //.getLockState(keyspace + "." + tableName + "." + primaryKey);
             if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
                 try {
                     results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT));
                 } catch (Exception e) {
+                    logger.error(EELFLoggerDelegate.applicationLogger, e);
                     return new ReturnType(ResultType.FAILURE, e.getMessage());
                 }
                 if (results.all().isEmpty()) {
@@ -151,6 +152,7 @@ public class MusicConditional {
             StringWriter sw = new StringWriter();
             e.printStackTrace(new PrintWriter(sw));
             String exceptionAsString = sw.toString();
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE,
                     "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
                             + exceptionAsString);
@@ -178,6 +180,7 @@ public class MusicConditional {
 
         } catch (Exception e) {
             MusicCore.destroyLockRef(lockId);
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE, e.getMessage());
 
         }
@@ -188,8 +191,7 @@ public class MusicConditional {
         try {
             String fullyQualifiedKey = keyspace + "." + tableName + "." + primaryKeyValue;
             ReturnType lockAcqResult = MusicCore.acquireLock(fullyQualifiedKey, lockId);
-            //MusicLockState mls = MusicZKCore.getLockingServiceHandle()
-                    //.getLockState(keyspace + "." + tableName + "." + primaryKeyValue);
+
             if (lockAcqResult.getResult().equals(ResultType.SUCCESS)) {
                 Row row  = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(queryBank.get(MusicUtil.SELECT)).one();
                 
@@ -206,6 +208,7 @@ public class MusicConditional {
                     try {
                         MusicDataStoreHandle.getDSHandle().executePut(update, "critical");
                     } catch (Exception ex) {
+                        logger.error(EELFLoggerDelegate.applicationLogger, ex);
                         return new ReturnType(ResultType.FAILURE, ex.getMessage());
                     }
                 }else {
@@ -223,6 +226,7 @@ public class MusicConditional {
             StringWriter sw = new StringWriter();
             e.printStackTrace(new PrintWriter(sw));
             String exceptionAsString = sw.toString();
+            logger.error(EELFLoggerDelegate.applicationLogger, e);
             return new ReturnType(ResultType.FAILURE,
                     "Exception thrown while doing the critical put, check sanctity of the row/conditions:\n"
                             + exceptionAsString);
@@ -234,11 +238,10 @@ public class MusicConditional {
     public static Map<String, String> getValues(boolean isExists, Map<String, Object> casscadeColumnData,
             Map<String, String> status) {
 
-        Map<String, String> value = new HashMap<>();
         Map<String, String> returnMap = new HashMap<>();
         Object key = casscadeColumnData.get("key");
         String setStatus = "";
-        value = (Map<String, String>) casscadeColumnData.get("value");
+        Map<String, String> value = (Map<String, String>) casscadeColumnData.get("value");
 
         if (isExists)
             setStatus = status.get("exists");
@@ -259,9 +262,10 @@ public class MusicConditional {
         StringBuilder fieldsString = new StringBuilder("(vector_ts"+",");
         StringBuilder valueString = new StringBuilder("(" + "?" + ",");
         String vector = String.valueOf(Thread.currentThread().getId() + System.currentTimeMillis());
+        String localPrimaryKey;
         queryObject.addValue(vector);
         if(casscadeColumn!=null && casscadeColumnValues!=null) {
-            fieldsString.append("" +casscadeColumn+" ," );
+            fieldsString.append(casscadeColumn).append(" ,");
           valueString.append("?,");
           queryObject.addValue(casscadeColumnValues);
         }
@@ -269,17 +273,18 @@ public class MusicConditional {
         int counter = 0;
         for (Map.Entry<String, Object> entry : valuesMap.entrySet()) {
             
-            fieldsString.append("" + entry.getKey());
+            fieldsString.append(entry.getKey());
             Object valueObj = entry.getValue();
             if (primaryKeyName.equals(entry.getKey())) {
-                primaryKey = entry.getValue() + "";
-                primaryKey = primaryKey.replace("'", "''");
+              localPrimaryKey = entry.getValue() + "";
+              localPrimaryKey = localPrimaryKey.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);
+                logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() +" Invalid column name : "+entry.getKey
+                    (), AppMessages.INCORRECTDATA  ,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR, ex);
                
             }
 
@@ -287,7 +292,7 @@ public class MusicConditional {
             try {
               formattedValue = MusicUtil.convertToActualDataType(colType, valueObj);
             } catch (Exception e) {
-              logger.error(EELFLoggerDelegate.errorLogger,e.getMessage());
+              logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), e);
           }
             
             valueString.append("?");
@@ -339,11 +344,10 @@ public class MusicConditional {
 
         ColumnDefinitions colInfo = row.getColumnDefinitions();
         DataType colType = colInfo.getType(cascadeColumnName);
-        Map<String, String> values = new HashMap<>();
         Object columnValue = getColValue(row, cascadeColumnName, colType);
 
         Map<String, String> finalValues = new HashMap<>();
-        values = (Map<String, String>) columnValue;
+        Map<String, String> values = (Map<String, String>) columnValue;
         if (values != null && values.keySet().contains(planId)) {
             String valueString = values.get(planId);
             String tempValueString = valueString.replaceAll("\\{", "").replaceAll("\"", "").replaceAll("\\}", "");