Update distribution files to support helm charts
[music.git] / src / main / java / org / onap / music / main / MusicUtil.java
index 20bb0a4..cfad845 100755 (executable)
@@ -23,17 +23,29 @@ package org.onap.music.main;
 
 import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigInteger;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Properties;
 import java.util.Scanner;
+import java.util.StringTokenizer;
 import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.ResponseBuilder;
+
 import org.onap.music.datastore.PreparedQueryObject;
 import org.onap.music.eelf.logging.EELFLoggerDelegate;
+
 import com.datastax.driver.core.DataType;
+import com.sun.jersey.core.util.Base64;
 
 /**
  * @author nelson24
@@ -58,6 +70,10 @@ public class MusicUtil {
     public static final String INSERT = "insert";
     public static final String UPDATE = "update";
     public static final String UPSERT = "upsert";
+    public static final String USERID = "userId";
+    public static final String PASSWORD = "password";
+
+       public static final String AUTHORIZATION = "Authorization";
 
     private static final String LOCALHOST = "localhost";
     private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
@@ -69,6 +85,10 @@ public class MusicUtil {
     private static String myZkHost = LOCALHOST;
     private static String myCassaHost = LOCALHOST;
     private static String defaultMusicIp = LOCALHOST;
+    private static int cassandraPort = 9042;
+    private static int notifytimeout = 30000;
+    private static int notifyinterval = 5000;
+    
     private static boolean debug = true;
     private static String version = "2.3.0";
     private static String musicRestIp = LOCALHOST;
@@ -76,17 +96,31 @@ public class MusicUtil {
     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" };
+            "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url","cassandra.port", "notify.timeout", "notify.interval" };
 
     private static String cassName = "cassandra";
     private static String cassPwd;
     private static String aafEndpointUrl = null;
+    public static final ConcurrentMap<String, Long> zkNodeMap = new ConcurrentHashMap<>();
 
     private MusicUtil() {
         throw new IllegalStateException("Utility Class");
     }
-    
-    
+    /**
+     * 
+     * @return cassandra port
+     */
+    public static int getCassandraPort() {
+               return cassandraPort;
+       }
+
+    /**
+     * set cassandra port
+     * @param cassandraPort
+     */
+       public static void setCassandraPort(int cassandraPort) {
+               MusicUtil.cassandraPort = cassandraPort;
+       }
     /**
      * @return the cassName
      */
@@ -402,7 +436,8 @@ public class MusicUtil {
         MusicUtil.cassPwd = cassPwd;
     }
 
-    public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
+    @SuppressWarnings("unchecked")
+       public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
 
         String value = "";
         switch (type.getName()) {
@@ -416,8 +451,7 @@ public class MusicUtil {
             value = "'" + valueString + "'";
             break;
         case MAP: {
-            @SuppressWarnings("unchecked")
-                       Map<String, Object> otMap = (Map<String, Object>) valueObj;
+            Map<String, Object> otMap = (Map<String, Object>) valueObj;
             value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
             break;
         }
@@ -440,29 +474,34 @@ public class MusicUtil {
        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;
-        case LIST:
-               return (List<String>)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;
+            case BLOB:
+               
+            default:
+                return valueObjString;
         }
     }
 
+    public static ByteBuffer convertToActualDataType(DataType colType, byte[] valueObj) {
+         ByteBuffer buffer = ByteBuffer.wrap(valueObj);
+         return buffer;
+    }
     /**
      *
      * Utility function to parse json map into sql like string
@@ -489,6 +528,7 @@ public class MusicUtil {
         return sqlString.toString();
     }
 
+    @SuppressWarnings("unused")
     public static String buildVersion(String major, String minor, String patch) {
         if (minor != null) {
             major += "." + minor;
@@ -530,8 +570,64 @@ public class MusicUtil {
         logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
         return response;
     }
-
     
     
+    public static Map<String,String> extractBasicAuthentication(String authorization){
+               
+       Map<String,String> authValues = new HashMap<>();
+       authorization = authorization.replaceFirst("Basic", "");
+       String decoded = Base64.base64Decode(authorization);
+       StringTokenizer token = new StringTokenizer(decoded, ":");
+       authValues.put(MusicUtil.USERID, token.nextToken().toString());
+       authValues.put(MusicUtil.PASSWORD,token.nextToken());
+       return authValues;
+       
+    }
+    
+    public static void loadProperties() throws Exception {
+               Properties prop = new Properties();
+           InputStream input = null;
+           try {
+               // load the properties file
+               input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
+               prop.load(input);
+           } catch (Exception ex) {
+               logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
+               throw new Exception();
+           } finally {
+               if (input != null) {
+                   try {
+                       input.close();
+                   } catch (IOException e) {
+                       logger.error(EELFLoggerDelegate.applicationLogger,"Load properties failed "+e.getMessage(),e);
+                   }
+               }
+           }
+           // get the property value and return it
+               MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host"));
+               String zkHosts = prop.getProperty("zookeeper.host");
+               MusicUtil.setMyZkHost(zkHosts);
+               MusicUtil.setCassName(prop.getProperty("cassandra.user"));
+               MusicUtil.setCassPwd(prop.getProperty("cassandra.password"));
+               MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty("cassandra.port")));
+               MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty("notify.timeout")));
+               MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty("notify.interval")));
+               
+       }
     
+       public static void setNotifyInterval(int notifyinterval) {
+               MusicUtil.notifyinterval = notifyinterval;
+       }
+       public static void setNotifyTimeOut(int notifytimeout) {
+               MusicUtil.notifytimeout = notifytimeout;
+       }
+
+       public static int getNotifyInterval() {
+               return MusicUtil.notifyinterval;
+       }
+       
+       public static int getNotifyTimeout() {
+               return MusicUtil.notifytimeout;
+               
+       }
 }