Docker update and POM fix
[music.git] / src / main / java / org / onap / music / main / MusicUtil.java
index 61d428d..c0b968b 100755 (executable)
  */
 package org.onap.music.main;
 
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Scanner;
 import java.util.UUID;
 import org.onap.music.datastore.PreparedQueryObject;
 import org.onap.music.eelf.logging.EELFLoggerDelegate;
+import org.onap.music.exceptions.MusicServiceException;
+
 import com.datastax.driver.core.DataType;
 
 /**
  * @author nelson24
  * 
- *         Properties This will take Properties and load them into MusicUtil. This is a hack for
- *         now. Eventually it would bebest to do this in another way.
+ *         Properties This will take Properties and load them into MusicUtil.
+ *         This is a hack for now. Eventually it would bebest to do this in
+ *         another way.
  * 
  */
 public class MusicUtil {
     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
+    
+    public static final String ATOMIC = "atomic";
+    public static final String EVENTUAL = "eventual";
+    public static final String CRITICAL = "critical";
+    public static final String ATOMICDELETELOCK = "atomic_delete_lock";
+    
+    public static final String DEFAULTKEYSPACENAME = "TBD";
 
+    
     private static int myId = 0;
-    private static ArrayList<String> allIds = new ArrayList<String>();
+    private static ArrayList<String> allIds = new ArrayList<>();
     private static String publicIp = "";
-    private static ArrayList<String> allPublicIps = new ArrayList<String>();
+    private static ArrayList<String> allPublicIps = new ArrayList<>();
     private static String myZkHost = "localhost";
     private static String myCassaHost = "localhost";
     private static String defaultMusicIp = "localhost";
     private static boolean debug = true;
     private static String version = "2.3.0";
-    public static String musicRestIp = "localhost";
+    private static String musicRestIp = "localhost";
     private static String musicPropertiesFilePath = "/opt/app/music/etc/music.properties";
     private static long defaultLockLeasePeriod = 6000;
-    private static final String[] propKeys = new String[] {"zookeeper.host", "cassandra.host",
-                    "music.ip", "debug", "version", "music.rest.ip", "music.properties",
-                    "lock.lease.period", "id", "all.ids", "public.ip", "all.pubic.ips",
-                    "cassandra.user", "cassandra.password", "aaf.endpoint.url"};
+    private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
+            "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
+            "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
 
-    public static final String ATOMIC = "atomic";
-    public static final String EVENTUAL = "eventual";
-    public static final String CRITICAL = "critical";
-    public static final String DEFAULTKEYSPACENAME = "TBD";
     private static String cassName = "cassandra";
-    private static String cassPwd = "cassandra";
+    private static String cassPwd;
     private static String aafEndpointUrl = null;
 
+    private MusicUtil() {
+        throw new IllegalStateException("Utility Class");
+    }
+    
+    
     /**
      * @return the cassName
      */
@@ -114,12 +125,11 @@ public class MusicUtil {
         MusicUtil.myId = myId;
     }
 
-
     /**
      * 
      * @return
      */
-    public static ArrayList<String> getAllIds() {
+    public static List<String> getAllIds() {
         return allIds;
     }
 
@@ -151,7 +161,7 @@ public class MusicUtil {
      * 
      * @return
      */
-    public static ArrayList<String> getAllPublicIps() {
+    public static List<String> getAllPublicIps() {
         return allPublicIps;
     }
 
@@ -164,7 +174,8 @@ public class MusicUtil {
     }
 
     /**
-     * Returns An array of property names that should be in the Properties files.
+     * Returns An array of property names that should be in the Properties
+     * files.
      * 
      * @return
      */
@@ -191,8 +202,8 @@ public class MusicUtil {
     }
 
     /**
-     * Get MusicPropertiesFilePath - Default = /opt/music/music.properties property file value -
-     * music.properties
+     * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
+     * property file value - music.properties
      * 
      * @return
      */
@@ -210,7 +221,8 @@ public class MusicUtil {
     }
 
     /**
-     * Get DefaultLockLeasePeriod - Default = 6000 property file value - lock.lease.period
+     * Get DefaultLockLeasePeriod - Default = 6000 property file value -
+     * lock.lease.period
      * 
      * @return
      */
@@ -264,7 +276,8 @@ public class MusicUtil {
     }
 
     /**
-     * Get MyZkHost - Zookeeper Hostname - Default = localhost property file value - zookeeper.host
+     * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
+     * value - zookeeper.host
      * 
      * @return
      */
@@ -282,8 +295,8 @@ public class MusicUtil {
     }
 
     /**
-     * Get MyCassHost - Cassandra Hostname - Default = localhost property file value -
-     * cassandra.host
+     * Get MyCassHost - Cassandra Hostname - Default = localhost property file
+     * value - cassandra.host
      * 
      * @return
      */
@@ -327,7 +340,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 line
+            String batchSize = fileScanner.next();// ignore the my public ip
+                                                    // line
             fileScanner.close();
         } catch (FileNotFoundException e) {
             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
@@ -345,6 +359,7 @@ public class MusicUtil {
             Thread.sleep(time);
         } catch (InterruptedException e) {
             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
+            Thread.currentThread().interrupt();
         }
     }
 
@@ -379,32 +394,31 @@ public class MusicUtil {
         MusicUtil.cassPwd = cassPwd;
     }
 
-    public static String convertToCQLDataType(DataType type, Object valueObj) {
+    public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
 
         String value = "";
         switch (type.getName()) {
-            case UUID:
-                value = valueObj + "";
-                break;
-            case TEXT:
-            case VARCHAR:
-                String valueString = valueObj + "";
-                valueString = valueString.replace("'", "''");
-                value = "'" + valueString + "'";
-                break;
-            case MAP: {
-                Map<String, Object> otMap = (Map<String, Object>) valueObj;
-                value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
-                break;
-            }
-            default:
-                value = valueObj + "";
-                break;
+        case UUID:
+            value = valueObj + "";
+            break;
+        case TEXT:
+        case VARCHAR:
+            String valueString = valueObj + "";
+            valueString = valueString.replace("'", "''");
+            value = "'" + valueString + "'";
+            break;
+        case MAP: {
+            Map<String, Object> otMap = (Map<String, Object>) valueObj;
+            value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
+            break;
+        }
+        default:
+            value = valueObj + "";
+            break;
         }
         return value;
     }
 
-
     /**
      * 
      * @param colType
@@ -412,31 +426,30 @@ public class MusicUtil {
      * @return
      * @throws Exception
      */
-    public static Object convertToActualDataType(DataType colType, Object valueObj) {
+    public static Object convertToActualDataType(DataType colType, Object valueObj)  throws Exception{
         String valueObjString = valueObj + "";
         switch (colType.getName()) {
-            case UUID:
-                return UUID.fromString(valueObjString);
-            case VARINT:
-                return BigInteger.valueOf(Long.parseLong(valueObjString));
-            case BIGINT:
-                return Long.parseLong(valueObjString);
-            case INT:
-                return Integer.parseInt(valueObjString);
-            case FLOAT:
-                return Float.parseFloat(valueObjString);
-            case DOUBLE:
-                return Double.parseDouble(valueObjString);
-            case BOOLEAN:
-                return Boolean.parseBoolean(valueObjString);
-            case MAP:
-                return (Map<String, Object>) valueObj;
-            default:
-                return valueObjString;
+        case UUID:
+            return UUID.fromString(valueObjString);
+        case VARINT:
+            return BigInteger.valueOf(Long.parseLong(valueObjString));
+        case BIGINT:
+            return Long.parseLong(valueObjString);
+        case INT:
+            return Integer.parseInt(valueObjString);
+        case FLOAT:
+            return Float.parseFloat(valueObjString);
+        case DOUBLE:
+            return Double.parseDouble(valueObjString);
+        case BOOLEAN:
+            return Boolean.parseBoolean(valueObjString);
+        case MAP:
+            return (Map<String, Object>) valueObj;
+        default:
+            return valueObjString;
         }
     }
 
-
     /**
      *
      * Utility function to parse json map into sql like string
@@ -446,7 +459,7 @@ public class MusicUtil {
      * @return
      */
 
-    public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) {
+    public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
         StringBuilder sqlString = new StringBuilder();
         int counter = 0;
         for (Map.Entry<String, Object> entry : jMap.entrySet()) {