* ===================================================================
* Copyright (c) 2017 AT&T Intellectual Property
* ===================================================================
- * Modifications Copyright (c) 2018 IBM.
+ * Modifications Copyright (c) 2019 IBM.
* Modifications Copyright (c) 2019 Samsung.
* ===================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
import com.datastax.driver.core.Row;
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.exceptions.MusicServiceException;
import org.onap.music.service.MusicCoreService;
import org.onap.music.service.impl.MusicCassaCore;
-import org.onap.music.service.impl.MusicZKCore;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.DataType;
-import com.sun.jersey.core.util.Base64;
/**
* @author nelson24
public class MusicUtil {
private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
+ // Consistancy Constants
public static final String ATOMIC = "atomic";
public static final String EVENTUAL = "eventual";
public static final String CRITICAL = "critical";
public static final String QUORUM = "quorum";
public static final String ONE = "one";
public static final String ATOMICDELETELOCK = "atomic_delete_lock";
- public static final String DEFAULTKEYSPACENAME = "TBD";
+
+ // Header Constants
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 AUTHORIZATION = "Authorization";
+
+ // CQL Constants
public static final String SELECT = "select";
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 PASSWORD = "";
public static final String CASSANDRA = "cassandra";
- public static final String ZOOKEEPER = "zookeeper";
-
- public static final String AUTHORIZATION = "Authorization";
private static final String LOCALHOST = "localhost";
private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
+ public static final String DEFAULTKEYSPACENAME = "TBD";
- private static int myId = 0;
- private static ArrayList<String> allIds = new ArrayList<>();
- private static String publicIp = "";
- private static ArrayList<String> allPublicIps = new ArrayList<>();
- 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 int cacheObjectMaxLife = -1;
+ private static long defaultLockLeasePeriod = 6000;
+ // Amount of times to retry to delete a lock in atomic.
+ private static int retryCount = 3;
private static String lockUsing = MusicUtil.CASSANDRA;
+ // Cadi OnOff
private static boolean isCadi = false;
-
+ // Keyspace Creation on/off
+ private static boolean isKeyspaceActive = false;
private static boolean debug = true;
- private static String version = "2.3.0";
- private static String musicRestIp = LOCALHOST;
+ private static String version = "0.0.0";
+ private static String build = "";
+
private static String musicPropertiesFilePath = PROPERTIES_FILE;
- 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","admin.username","admin.password","aaf.admin.url",
- "music.namespace","admin.aaf.role","cassandra.port","lock.using"};
+ // private static final String[] propKeys = new String[] { MusicUtil.class.getDeclaredMethod(arg0, )"build","cassandra.host", "debug",
+ // "version", "music.properties", "lock.lease.period", "cassandra.user",
+ // "cassandra.password", "aaf.endpoint.url","admin.username","admin.password",
+ // "music.namespace","admin.aaf.role","cassandra.port","lock.using","retry.count",
+ // "transId.header.required","conversation.header.required","clientId.header.required",
+ // "messageId.header.required","transId.header.prefix","conversation.header.prefix",
+ // "clientId.header.prefix","messageId.header.prefix"};
+ // Consistency Constants and variables.
private static final String[] cosistencyLevel = new String[] {
- "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO","THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"};
+ "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO",
+ "THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"};
private static final Map<String,ConsistencyLevel> consistencyName = new HashMap<>();
static {
consistencyName.put("ONE",ConsistencyLevel.ONE);
consistencyName.put("LOCAL_SERIAL",ConsistencyLevel.LOCAL_SERIAL);
}
+ // Cassandra Values
private static String cassName = "cassandra";
private static String cassPwd;
- private static String aafEndpointUrl = null;
- public static ConcurrentMap<String, Long> zkNodeMap = new ConcurrentHashMap<>();
- private static String adminId = "username";
- private static String adminPass= "password";
- private static String aafAdminUrl= null;
- private static String musicNamespace= "com.att.music.api";
- private static String adminAafRole= "com.att.music.api.admin_api";
-
- public static final long MusicEternityEpochMillis = 1533081600000L; // Wednesday, August 1, 2018 12:00:00 AM
+ private static String myCassaHost = LOCALHOST;
+ private static int cassandraPort = 9042;
+
+ // AAF
+ private static String musicAafNs = "org.onap.music.cadi";
+ // Locking
+ public static final long MusicEternityEpochMillis = 1533081600000L; // Wednesday, August 1, 2018 12:00:00 AM
public static final long MaxLockReferenceTimePart = 1000000000000L; // millis after eternity (eq sometime in 2050)
-
public static final long MaxCriticalSectionDurationMillis = 1L * 24 * 60 * 60 * 1000; // 1 day
-
+ // Response/Request tracking headers
+ private static String transIdPrefix = "false";
+ private static String conversationIdPrefix = "false";
+ private static String clientIdPrefix = "false";
+ private static String messageIdPrefix = "false";
+ private static Boolean transIdRequired = false;
+ private static Boolean conversationIdRequired = false;
+ private static Boolean clientIdRequired = false;
+ private static Boolean messageIdRequired = false;
+ private static String cipherEncKey = "";
+
+ public MusicUtil() {
+ throw new IllegalStateException("Utility Class");
+ }
+
public static String getLockUsing() {
return lockUsing;
}
-
public static void setLockUsing(String lockUsing) {
MusicUtil.lockUsing = lockUsing;
}
-
- public static String getAafAdminUrl() {
- return aafAdminUrl;
- }
-
-
- public static void setAafAdminUrl(String aafAdminUrl) {
- MusicUtil.aafAdminUrl = aafAdminUrl;
- }
-
- public static String getMusicNamespace() {
- return musicNamespace;
- }
-
-
- public static void setMusicNamespace(String musicNamespace) {
- MusicUtil.musicNamespace = musicNamespace;
- }
-
-
- public static String getAdminAafRole() {
- return adminAafRole;
- }
-
-
- public static void setAdminAafRole(String adminAafRole) {
- MusicUtil.adminAafRole = adminAafRole;
- }
-
-
-
- public static String getAdminId() {
- return adminId;
- }
-
-
- public static void setAdminId(String adminId) {
- MusicUtil.adminId = adminId;
- }
-
-
- public static String getAdminPass() {
- return adminPass;
- }
-
- public static void setAdminPass(String adminPass) {
- MusicUtil.adminPass = adminPass;
- }
-
-
- private MusicUtil() {
- throw new IllegalStateException("Utility Class");
- }
/**
*
* @return cassandra port
return cassPwd;
}
- /**
- * @return the aafEndpointUrl
- */
- public static String getAafEndpointUrl() {
- return aafEndpointUrl;
- }
-
- /**
- *
- * @param aafEndpointUrl
- */
- public static void setAafEndpointUrl(String aafEndpointUrl) {
- MusicUtil.aafEndpointUrl = aafEndpointUrl;
- }
-
- /**
- *
- * @return
- */
- public static int getMyId() {
- return myId;
- }
-
- /**
- *
- * @param myId
- */
- public static void setMyId(int myId) {
- MusicUtil.myId = myId;
- }
-
- /**
- *
- * @return
- */
- public static List<String> getAllIds() {
- return allIds;
- }
-
- /**
- *
- * @param allIds
- */
- public static void setAllIds(List<String> allIds) {
- MusicUtil.allIds = (ArrayList<String>) allIds;
- }
-
- /**
- *
- * @return
- */
- public static String getPublicIp() {
- return publicIp;
- }
-
- /**
- *
- * @param publicIp
- */
- public static void setPublicIp(String publicIp) {
- MusicUtil.publicIp = publicIp;
- }
-
- /**
- *
- * @return
- */
- public static List<String> getAllPublicIps() {
- return allPublicIps;
- }
-
- /**
- *
- * @param allPublicIps
- */
- public static void setAllPublicIps(List<String> allPublicIps) {
- MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
- }
-
/**
* Returns An array of property names that should be in the Properties
* files.
*
- * @return
- */
- public static String[] getPropkeys() {
- return propKeys;
- }
-
- /**
- * Get MusicRestIp - default = localhost property file value - music.rest.ip
- *
- * @return
- */
- public static String getMusicRestIp() {
- return musicRestIp;
- }
-
- /**
- * Set MusicRestIp
- *
- * @param musicRestIp
- */
- public static void setMusicRestIp(String musicRestIp) {
- MusicUtil.musicRestIp = musicRestIp;
- }
+// * @return
+// */
+// public static String[] getPropkeys() {
+// return propKeys.clone();
+// }
/**
* Get MusicPropertiesFilePath - Default = /opt/music/music.properties
}
/**
- * Return the version property file value - version
+ * Return the version property file value - version.
*
* @return
*/
}
/**
- * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
- * value - zookeeper.host
- *
- * @return
+ * Set the build of project which is a combination of the
+ * version and the date.
+ *
+ * @param build - version-date.
*/
- public static String getMyZkHost() {
- return myZkHost;
+ public static void setBuild(String build) {
+ MusicUtil.build = build;
}
/**
- * Set MyZkHost - Zookeeper Hostname
- *
- * @param myZkHost
+ * Return the build version-date.
*/
- public static void setMyZkHost(String myZkHost) {
- MusicUtil.myZkHost = myZkHost;
+ public static String getBuild() {
+ return build;
}
/**
/**
* Set MyCassHost - Cassandra Hostname
*
- * @param myCassaHost
+ * @param myCassaHost .
*/
public static void setMyCassaHost(String myCassaHost) {
MusicUtil.myCassaHost = myCassaHost;
}
-
+
/**
- * Get DefaultMusicIp - Default = localhost property file value - music.ip
- *
+ * Gey default retry count
* @return
*/
- public static String getDefaultMusicIp() {
- return defaultMusicIp;
+ public static int getRetryCount() {
+ return retryCount;
}
/**
- * Set DefaultMusicIp
- *
- * @param defaultMusicIp
+ * Set retry count
+ * @param retryCount .
*/
- public static void setDefaultMusicIp(String defaultMusicIp) {
- MusicUtil.defaultMusicIp = defaultMusicIp;
+ public static void setRetryCount(int retryCount) {
+ MusicUtil.retryCount = retryCount;
}
+
/**
- *
- * @return
+ * This is used to turn keyspace creation api on/off.
+ *
+ */
+ public static void setKeyspaceActive(Boolean keyspaceActive) {
+ MusicUtil.isKeyspaceActive = keyspaceActive;
+ }
+
+ /**
+ * This is used to turn keyspace creation api on/off.
+ * @return boolean isKeyspaceActive
*/
+ public static boolean isKeyspaceActive() {
+ return isKeyspaceActive;
+ }
+
+ /**
+ * This method depricated as its not used or needed.
+ *
+ * @return String
+ */
+ @Deprecated
public static String getTestType() {
String testType = "";
try {
Scanner fileScanner = new Scanner(new File(""));
testType = fileScanner.next();// ignore the my id line
@SuppressWarnings("unused")
- 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());
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
}
return testType;
}
/**
- *
+ * Method to do a Thread Sleep.
+ * Used for adding a delay.
+ *
* @param time
*/
public static void sleep(long time) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
- logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
+ logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
Thread.currentThread().interrupt();
}
}
}
public static ByteBuffer convertToActualDataType(DataType colType, byte[] valueObj) {
- ByteBuffer buffer = ByteBuffer.wrap(valueObj);
- return buffer;
+ ByteBuffer buffer = ByteBuffer.wrap(valueObj);
+ return buffer;
}
/**
response.header(XPATCHVERSION,verArray[2]);
}
response.header(XLATESTVERSION,version);
- logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
+ logger.info(EELFLoggerDelegate.auditLogger,"Version In:" + versionIn);
return response;
}
-
- public static Map<String,String> extractBasicAuthentication(String authorization){
- Map<String,String> authValues = new HashMap<>();
- if(authorization == null) {
- authValues.put("ERROR", "Authorization cannot be null");
- return authValues;
- }
- authorization = authorization.replaceFirst("Basic", "");
- String decoded = Base64.base64Decode(authorization);
- StringTokenizer token = new StringTokenizer(decoded, ":");
- authValues.put(MusicUtil.USERID, token.nextToken());
- authValues.put(MusicUtil.PASSWORD,token.nextToken());
- return authValues;
-
- }
-
public static boolean isValidConsistency(String consistency) {
for (String string : cosistencyLevel) {
if (string.equalsIgnoreCase(consistency))
public static ConsistencyLevel getConsistencyLevel(String consistency) {
return consistencyName.get(consistency.toUpperCase());
- }
-
- 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) {
- e.printStackTrace();
- }
- }
- }
- // 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")));
- MusicUtil.setCacheObjectMaxLife(Integer.parseInt(prop.getProperty("cacheobject.maxlife")));
}
- 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;
- }
-
- public static int getCacheObjectMaxLife() {
- return MusicUtil.cacheObjectMaxLife;
- }
-
- public static void setCacheObjectMaxLife(int cacheObjectMaxLife) {
- MusicUtil.cacheObjectMaxLife = cacheObjectMaxLife;
- }
-
/**
* Given the time of write for an update in a critical section, this method provides a transformed timestamp
* that ensures that a previous lock holder who is still alive can never corrupt a later critical section.
public static MusicCoreService getMusicCoreService() {
if(getLockUsing().equals(MusicUtil.CASSANDRA))
return MusicCassaCore.getInstance();
- else if (getLockUsing().equals(MusicUtil.ZOOKEEPER))
- return MusicZKCore.getInstance();
else
return MusicCassaCore.getInstance();
}
MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical");
} catch (MusicServiceException | MusicQueryException e) {
logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,
- ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR);
+ ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR, e);
}
}
return uuid;
}
+ private static String checkPrefix(String prefix){
+ if (prefix == null || "".equals(prefix) || prefix.endsWith("-")) {
+ return prefix;
+ } else {
+ return prefix + "-";
+ }
+ }
+
+ /**
+ * @return the transIdPrefix
+ */
+ public static String getTransIdPrefix() {
+ return transIdPrefix;
+ }
+
+ /**
+ * @param transIdPrefix the transIdPrefix to set
+ */
+ public static void setTransIdPrefix(String transIdPrefix) {
+ MusicUtil.transIdPrefix = checkPrefix(transIdPrefix);
+ }
+
+ /**
+ * @return the conversationIdPrefix
+ */
+ public static String getConversationIdPrefix() {
+ return conversationIdPrefix;
+ }
+
+ /**
+ * @param conversationIdPrefix the conversationIdPrefix to set
+ */
+ public static void setConversationIdPrefix(String conversationIdPrefix) {
+ MusicUtil.conversationIdPrefix = checkPrefix(conversationIdPrefix);
+ }
+
+ /**
+ * @return the clientIdPrefix
+ */
+ public static String getClientIdPrefix() {
+ return clientIdPrefix;
+ }
+
+ /**
+ * @param clientIdPrefix the clientIdPrefix to set
+ */
+ public static void setClientIdPrefix(String clientIdPrefix) {
+ MusicUtil.clientIdPrefix = checkPrefix(clientIdPrefix);
+ }
+
+ /**
+ * @return the messageIdPrefix
+ */
+ public static String getMessageIdPrefix() {
+ return messageIdPrefix;
+ }
+
+ /**
+ * @param messageIdPrefix the messageIdPrefix to set
+ */
+ public static void setMessageIdPrefix(String messageIdPrefix) {
+ MusicUtil.messageIdPrefix = checkPrefix(messageIdPrefix);
+ }
+
+ /**
+ * @return the transIdRequired
+ */
+ public static Boolean getTransIdRequired() {
+ return transIdRequired;
+ }
+
+
+ /**
+ * @param transIdRequired the transIdRequired to set
+ */
+ public static void setTransIdRequired(Boolean transIdRequired) {
+ MusicUtil.transIdRequired = transIdRequired;
+ }
+
+
+ /**
+ * @return the conversationIdRequired
+ */
+ public static Boolean getConversationIdRequired() {
+ return conversationIdRequired;
+ }
+
+
+ /**
+ * @param conversationIdRequired the conversationIdRequired to set
+ */
+ public static void setConversationIdRequired(Boolean conversationIdRequired) {
+ MusicUtil.conversationIdRequired = conversationIdRequired;
+ }
+
+
+ /**
+ * @return the clientIdRequired
+ */
+ public static Boolean getClientIdRequired() {
+ return clientIdRequired;
+ }
+
+
+ /**
+ * @param clientIdRequired the clientIdRequired to set
+ */
+ public static void setClientIdRequired(Boolean clientIdRequired) {
+ MusicUtil.clientIdRequired = clientIdRequired;
+ }
+
+
+ /**
+ * @return the messageIdRequired
+ */
+ public static Boolean getMessageIdRequired() {
+ return messageIdRequired;
+ }
+
+ /**
+ * @param messageIdRequired the messageIdRequired to set
+ */
+ public static void setMessageIdRequired(Boolean messageIdRequired) {
+ MusicUtil.messageIdRequired = messageIdRequired;
+ }
+
+
+ public static String getCipherEncKey() {
+ return MusicUtil.cipherEncKey;
+ }
+
+
+ public static void setCipherEncKey(String cipherEncKey) {
+ MusicUtil.cipherEncKey = cipherEncKey;
+ if ( null == cipherEncKey || cipherEncKey.equals("") ||
+ cipherEncKey.equals("nothing to see here")) {
+ logger.error(EELFLoggerDelegate.errorLogger, "Missing Cipher Encryption Key.");
+ }
+ }
+
+ public static String getMusicAafNs() {
+ return MusicUtil.musicAafNs;
+ }
+
+
+ public static void setMusicAafNs(String musicAafNs) {
+ MusicUtil.musicAafNs = musicAafNs;
+ }
+
+
+
}