2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Modifications Copyright (c) 2018 IBM.
8 * Modifications Copyright (c) 2019 Samsung.
9 * ===================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 * ============LICENSE_END=============================================
23 * ====================================================================
26 package org.onap.music.main;
28 import com.datastax.driver.core.ColumnDefinitions;
29 import com.datastax.driver.core.ColumnDefinitions.Definition;
30 import com.datastax.driver.core.ResultSet;
31 import com.datastax.driver.core.Row;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.math.BigInteger;
37 import java.nio.ByteBuffer;
38 import java.util.ArrayList;
39 import java.util.HashMap;
40 import java.util.List;
42 import java.util.Properties;
43 import java.util.Scanner;
44 import java.util.StringTokenizer;
45 import java.util.UUID;
46 import java.util.concurrent.ConcurrentHashMap;
47 import java.util.concurrent.ConcurrentMap;
49 import javax.ws.rs.core.Response;
50 import javax.ws.rs.core.Response.ResponseBuilder;
51 import org.onap.music.datastore.MusicDataStoreHandle;
52 import org.onap.music.datastore.PreparedQueryObject;
53 import org.onap.music.eelf.logging.EELFLoggerDelegate;
54 import org.onap.music.eelf.logging.format.AppMessages;
55 import org.onap.music.eelf.logging.format.ErrorSeverity;
56 import org.onap.music.eelf.logging.format.ErrorTypes;
57 import org.onap.music.exceptions.MusicQueryException;
58 import org.onap.music.exceptions.MusicServiceException;
59 import org.onap.music.service.MusicCoreService;
60 import org.onap.music.service.impl.MusicCassaCore;
61 import org.onap.music.service.impl.MusicZKCore;
63 import com.datastax.driver.core.ConsistencyLevel;
64 import com.datastax.driver.core.DataType;
65 import com.sun.jersey.core.util.Base64;
70 * Properties This will take Properties and load them into MusicUtil.
71 * This is a hack for now. Eventually it would bebest to do this in
75 public class MusicUtil {
76 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
78 public static final String ATOMIC = "atomic";
79 public static final String EVENTUAL = "eventual";
80 public static final String CRITICAL = "critical";
81 public static final String EVENTUAL_NB = "eventual_nb";
82 public static final String ALL = "all";
83 public static final String QUORUM = "quorum";
84 public static final String ONE = "one";
85 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
86 public static final String DEFAULTKEYSPACENAME = "TBD";
87 private static final String XLATESTVERSION = "X-latestVersion";
88 private static final String XMINORVERSION = "X-minorVersion";
89 private static final String XPATCHVERSION = "X-patchVersion";
90 public static final String SELECT = "select";
91 public static final String INSERT = "insert";
92 public static final String UPDATE = "update";
93 public static final String UPSERT = "upsert";
94 public static final String USERID = "userId";
95 public static final String PASSWORD = "password";
96 public static final String CASSANDRA = "cassandra";
97 public static final String ZOOKEEPER = "zookeeper";
99 public static final String AUTHORIZATION = "Authorization";
101 private static final String LOCALHOST = "localhost";
102 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
104 private static int myId = 0;
105 private static ArrayList<String> allIds = new ArrayList<>();
106 private static String publicIp = "";
107 private static ArrayList<String> allPublicIps = new ArrayList<>();
108 private static String myZkHost = LOCALHOST;
109 private static String myCassaHost = LOCALHOST;
110 private static String defaultMusicIp = LOCALHOST;
111 private static int cassandraPort = 9042;
112 private static int notifytimeout = 30000;
113 private static int notifyinterval = 5000;
114 private static int cacheObjectMaxLife = -1;
115 private static String lockUsing = MusicUtil.CASSANDRA;
116 private static boolean isCadi = false;
118 private static boolean debug = true;
119 private static String version = "2.3.0";
120 private static String musicRestIp = LOCALHOST;
121 private static String musicPropertiesFilePath = PROPERTIES_FILE;
122 private static long defaultLockLeasePeriod = 6000;
123 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
124 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
125 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url","admin.username","admin.password","aaf.admin.url",
126 "music.namespace","admin.aaf.role","cassandra.port","lock.using"};
127 private static final String[] cosistencyLevel = new String[] {
128 "ALL","EACH_QUORUM","QUORUM","LOCAL_QUORUM","ONE","TWO","THREE","LOCAL_ONE","ANY","SERIAL","LOCAL_SERIAL"};
129 private static final Map<String,ConsistencyLevel> consistencyName = new HashMap<>();
131 consistencyName.put("ONE",ConsistencyLevel.ONE);
132 consistencyName.put("TWO",ConsistencyLevel.TWO);
133 consistencyName.put("THREE",ConsistencyLevel.THREE);
134 consistencyName.put("SERIAL",ConsistencyLevel.SERIAL);
135 consistencyName.put("ALL",ConsistencyLevel.ALL);
136 consistencyName.put("EACH_QUORUM",ConsistencyLevel.EACH_QUORUM);
137 consistencyName.put("QUORUM",ConsistencyLevel.QUORUM);
138 consistencyName.put("LOCAL_QUORUM",ConsistencyLevel.LOCAL_QUORUM);
139 consistencyName.put("LOCAL_ONE",ConsistencyLevel.LOCAL_ONE);
140 consistencyName.put("LOCAL_SERIAL",ConsistencyLevel.LOCAL_SERIAL);
143 private static String cassName = "cassandra";
144 private static String cassPwd;
145 private static String aafEndpointUrl = null;
146 public static ConcurrentMap<String, Long> zkNodeMap = new ConcurrentHashMap<>();
147 private static String adminId = "username";
148 private static String adminPass= "password";
149 private static String aafAdminUrl= null;
150 private static String musicNamespace= "com.att.music.api";
151 private static String adminAafRole= "com.att.music.api.admin_api";
153 public static final long MusicEternityEpochMillis = 1533081600000L; // Wednesday, August 1, 2018 12:00:00 AM
155 public static final long MaxLockReferenceTimePart = 1000000000000L; // millis after eternity (eq sometime in 2050)
157 public static final long MaxCriticalSectionDurationMillis = 1L * 24 * 60 * 60 * 1000; // 1 day
160 public static String getLockUsing() {
165 public static void setLockUsing(String lockUsing) {
166 MusicUtil.lockUsing = lockUsing;
169 public static String getAafAdminUrl() {
174 public static void setAafAdminUrl(String aafAdminUrl) {
175 MusicUtil.aafAdminUrl = aafAdminUrl;
179 public static String getMusicNamespace() {
180 return musicNamespace;
184 public static void setMusicNamespace(String musicNamespace) {
185 MusicUtil.musicNamespace = musicNamespace;
189 public static String getAdminAafRole() {
194 public static void setAdminAafRole(String adminAafRole) {
195 MusicUtil.adminAafRole = adminAafRole;
200 public static String getAdminId() {
205 public static void setAdminId(String adminId) {
206 MusicUtil.adminId = adminId;
210 public static String getAdminPass() {
214 public static void setAdminPass(String adminPass) {
215 MusicUtil.adminPass = adminPass;
219 private MusicUtil() {
220 throw new IllegalStateException("Utility Class");
224 * @return cassandra port
226 public static int getCassandraPort() {
227 return cassandraPort;
232 * @param cassandraPort
234 public static void setCassandraPort(int cassandraPort) {
235 MusicUtil.cassandraPort = cassandraPort;
238 * @return the cassName
240 public static String getCassName() {
245 * @return the cassPwd
247 public static String getCassPwd() {
252 * @return the aafEndpointUrl
254 public static String getAafEndpointUrl() {
255 return aafEndpointUrl;
260 * @param aafEndpointUrl
262 public static void setAafEndpointUrl(String aafEndpointUrl) {
263 MusicUtil.aafEndpointUrl = aafEndpointUrl;
270 public static int getMyId() {
278 public static void setMyId(int myId) {
279 MusicUtil.myId = myId;
286 public static List<String> getAllIds() {
294 public static void setAllIds(List<String> allIds) {
295 MusicUtil.allIds = (ArrayList<String>) allIds;
302 public static String getPublicIp() {
310 public static void setPublicIp(String publicIp) {
311 MusicUtil.publicIp = publicIp;
318 public static List<String> getAllPublicIps() {
324 * @param allPublicIps
326 public static void setAllPublicIps(List<String> allPublicIps) {
327 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
331 * Returns An array of property names that should be in the Properties
336 public static String[] getPropkeys() {
341 * Get MusicRestIp - default = localhost property file value - music.rest.ip
345 public static String getMusicRestIp() {
354 public static void setMusicRestIp(String musicRestIp) {
355 MusicUtil.musicRestIp = musicRestIp;
359 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
360 * property file value - music.properties
364 public static String getMusicPropertiesFilePath() {
365 return musicPropertiesFilePath;
369 * Set MusicPropertiesFilePath
371 * @param musicPropertiesFilePath
373 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
374 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
378 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
383 public static long getDefaultLockLeasePeriod() {
384 return defaultLockLeasePeriod;
388 * Set DefaultLockLeasePeriod
390 * @param defaultLockLeasePeriod
392 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
393 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
401 public static void setDebug(boolean debug) {
402 MusicUtil.debug = debug;
406 * Is Debug - Default = true property file value - debug
410 public static boolean isDebug() {
419 public static void setVersion(String version) {
420 MusicUtil.version = version;
424 * Return the version property file value - version
428 public static String getVersion() {
433 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
434 * value - zookeeper.host
438 public static String getMyZkHost() {
443 * Set MyZkHost - Zookeeper Hostname
447 public static void setMyZkHost(String myZkHost) {
448 MusicUtil.myZkHost = myZkHost;
452 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
453 * value - cassandra.host
457 public static String getMyCassaHost() {
462 * Set MyCassHost - Cassandra Hostname
466 public static void setMyCassaHost(String myCassaHost) {
467 MusicUtil.myCassaHost = myCassaHost;
471 * Get DefaultMusicIp - Default = localhost property file value - music.ip
475 public static String getDefaultMusicIp() {
476 return defaultMusicIp;
482 * @param defaultMusicIp
484 public static void setDefaultMusicIp(String defaultMusicIp) {
485 MusicUtil.defaultMusicIp = defaultMusicIp;
492 public static String getTestType() {
493 String testType = "";
495 Scanner fileScanner = new Scanner(new File(""));
496 testType = fileScanner.next();// ignore the my id line
497 @SuppressWarnings("unused")
498 String batchSize = fileScanner.next();// ignore the my public ip
501 } catch (FileNotFoundException e) {
502 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
512 public static void sleep(long time) {
515 } catch (InterruptedException e) {
516 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
517 Thread.currentThread().interrupt();
522 * Utility function to check if the query object is valid.
528 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
530 int noOfValues = queryObject.getValues().size();
532 char[] temp = queryObject.getQuery().toCharArray();
533 for (int i = 0; i < temp.length; i++) {
537 return (noOfValues == noOfParams);
539 return !queryObject.getQuery().isEmpty();
544 public static void setCassName(String cassName) {
545 MusicUtil.cassName = cassName;
548 public static void setCassPwd(String cassPwd) {
549 MusicUtil.cassPwd = cassPwd;
552 @SuppressWarnings("unchecked")
553 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
556 switch (type.getName()) {
558 value = valueObj + "";
562 String valueString = valueObj + "";
563 valueString = valueString.replace("'", "''");
564 value = "'" + valueString + "'";
567 Map<String, Object> otMap = (Map<String, Object>) valueObj;
568 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
572 value = valueObj + "";
583 * @throws MusicTypeConversionException
586 @SuppressWarnings("unchecked")
587 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
588 String valueObjString = valueObj + "";
589 switch (colType.getName()) {
591 return UUID.fromString(valueObjString);
593 return BigInteger.valueOf(Long.parseLong(valueObjString));
595 return Long.parseLong(valueObjString);
597 return Integer.parseInt(valueObjString);
599 return Float.parseFloat(valueObjString);
601 return Double.parseDouble(valueObjString);
603 return Boolean.parseBoolean(valueObjString);
605 return (Map<String, Object>) valueObj;
607 return (List<Object>)valueObj;
611 return valueObjString;
615 public static ByteBuffer convertToActualDataType(DataType colType, byte[] valueObj) {
616 ByteBuffer buffer = ByteBuffer.wrap(valueObj);
622 * Utility function to parse json map into sql like string
625 * @param lineDelimiter
629 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
630 StringBuilder sqlString = new StringBuilder();
632 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
633 Object ot = entry.getValue();
634 String value = ot + "";
635 if (ot instanceof String) {
636 value = "'" + value.replace("'", "''") + "'";
638 sqlString.append("'" + entry.getKey() + "':" + value);
639 if (counter != jMap.size() - 1)
640 sqlString.append(lineDelimiter);
641 counter = counter + 1;
643 return sqlString.toString();
646 @SuppressWarnings("unused")
647 public static String buildVersion(String major, String minor, String patch) {
649 major += "." + minor;
651 major += "." + patch;
658 * Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
659 * X-latestVerstion will be equal to the latest full version.
660 * X-minorVersion - will be equal to the latest minor version.
661 * X-pathVersion - will be equal to the latest patch version.
662 * Future plans will change this.
669 public static ResponseBuilder buildVersionResponse(String major, String minor, String patch) {
670 ResponseBuilder response = Response.noContent();
671 String versionIn = buildVersion(major,minor,patch);
672 String version = MusicUtil.getVersion();
673 String[] verArray = version.split("\\.",3);
674 if ( minor != null ) {
675 response.header(XMINORVERSION,minor);
677 response.header(XMINORVERSION,verArray[1]);
679 if ( patch != null ) {
680 response.header(XPATCHVERSION,patch);
682 response.header(XPATCHVERSION,verArray[2]);
684 response.header(XLATESTVERSION,version);
685 logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
690 public static Map<String,String> extractBasicAuthentication(String authorization){
691 Map<String,String> authValues = new HashMap<>();
692 if(authorization == null) {
693 authValues.put("ERROR", "Authorization cannot be null");
696 authorization = authorization.replaceFirst("Basic", "");
697 String decoded = Base64.base64Decode(authorization);
698 StringTokenizer token = new StringTokenizer(decoded, ":");
699 authValues.put(MusicUtil.USERID, token.nextToken());
700 authValues.put(MusicUtil.PASSWORD,token.nextToken());
705 public static boolean isValidConsistency(String consistency) {
706 for (String string : cosistencyLevel) {
707 if (string.equalsIgnoreCase(consistency))
714 public static ConsistencyLevel getConsistencyLevel(String consistency) {
715 return consistencyName.get(consistency.toUpperCase());
718 public static void loadProperties() throws Exception {
719 Properties prop = new Properties();
720 InputStream input = null;
722 // load the properties file
723 input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
725 } catch (Exception ex) {
726 logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
727 throw new Exception();
732 } catch (IOException e) {
737 // get the property value and return it
738 MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host"));
739 String zkHosts = prop.getProperty("zookeeper.host");
740 MusicUtil.setMyZkHost(zkHosts);
741 MusicUtil.setCassName(prop.getProperty("cassandra.user"));
742 MusicUtil.setCassPwd(prop.getProperty("cassandra.password"));
743 MusicUtil.setCassandraPort(Integer.parseInt(prop.getProperty("cassandra.port")));
744 MusicUtil.setNotifyTimeOut(Integer.parseInt(prop.getProperty("notify.timeout")));
745 MusicUtil.setNotifyInterval(Integer.parseInt(prop.getProperty("notify.interval")));
746 MusicUtil.setCacheObjectMaxLife(Integer.parseInt(prop.getProperty("cacheobject.maxlife")));
749 public static void setNotifyInterval(int notifyinterval) {
750 MusicUtil.notifyinterval = notifyinterval;
752 public static void setNotifyTimeOut(int notifytimeout) {
753 MusicUtil.notifytimeout = notifytimeout;
756 public static int getNotifyInterval() {
757 return MusicUtil.notifyinterval;
760 public static int getNotifyTimeout() {
761 return MusicUtil.notifytimeout;
764 public static int getCacheObjectMaxLife() {
765 return MusicUtil.cacheObjectMaxLife;
768 public static void setCacheObjectMaxLife(int cacheObjectMaxLife) {
769 MusicUtil.cacheObjectMaxLife = cacheObjectMaxLife;
773 * Given the time of write for an update in a critical section, this method provides a transformed timestamp
774 * that ensures that a previous lock holder who is still alive can never corrupt a later critical section.
775 * The main idea is to us the lock reference to clearly demarcate the timestamps across critical sections.
776 * @param the UUID lock reference associated with the write.
777 * @param the long timeOfWrite which is the actual time at which the write took place
778 * @throws MusicServiceException
779 * @throws MusicQueryException
781 public static long v2sTimeStampInMicroseconds(long ordinal, long timeOfWrite) throws MusicServiceException, MusicQueryException {
782 // TODO: use acquire time instead of music eternity epoch
783 long ts = ordinal * MaxLockReferenceTimePart + (timeOfWrite - MusicEternityEpochMillis);
788 public static MusicCoreService getMusicCoreService() {
789 if(getLockUsing().equals(MusicUtil.CASSANDRA))
790 return MusicCassaCore.getInstance();
791 else if (getLockUsing().equals(MusicUtil.ZOOKEEPER))
792 return MusicZKCore.getInstance();
794 return MusicCassaCore.getInstance();
801 public static Map<String, Object> validateLock(String lockName) {
802 Map<String, Object> resultMap = new HashMap<>();
803 String[] locks = lockName.split("\\.");
804 if(locks.length < 3) {
805 resultMap.put("Error", "Invalid lock. Please make sure lock is of the type keyspaceName.tableName.primaryKey");
808 String keyspace= locks[0];
809 if(keyspace.startsWith("$"))
810 keyspace = keyspace.substring(1);
811 resultMap.put("keyspace",keyspace);
816 public static void setIsCadi(boolean isCadi) {
817 MusicUtil.isCadi = isCadi;
820 public static void writeBackToQuorum(PreparedQueryObject selectQuery, String primaryKeyName,
821 PreparedQueryObject updateQuery, String keyspace, String table,
822 Object cqlFormattedPrimaryKeyValue)
825 ResultSet results = MusicDataStoreHandle.getDSHandle().executeQuorumConsistencyGet(selectQuery);
826 // write it back to a quorum
827 Row row = results.one();
828 ColumnDefinitions colInfo = row.getColumnDefinitions();
829 int totalColumns = colInfo.size();
831 StringBuilder fieldValueString = new StringBuilder("");
832 for (Definition definition : colInfo) {
833 String colName = definition.getName();
834 if (colName.equals(primaryKeyName))
836 DataType colType = definition.getType();
837 Object valueObj = MusicDataStoreHandle.getDSHandle().getColValue(row, colName, colType);
838 Object valueString = MusicUtil.convertToActualDataType(colType, valueObj);
839 fieldValueString.append(colName + " = ?");
840 updateQuery.addValue(valueString);
841 if (counter != (totalColumns - 1))
842 fieldValueString.append(",");
843 counter = counter + 1;
845 updateQuery.appendQueryString("UPDATE " + keyspace + "." + table + " SET "
846 + fieldValueString + " WHERE " + primaryKeyName + "= ? " + ";");
847 updateQuery.addValue(cqlFormattedPrimaryKeyValue);
849 MusicDataStoreHandle.getDSHandle().executePut(updateQuery, "critical");
850 } catch (MusicServiceException | MusicQueryException e) {
851 logger.error(EELFLoggerDelegate.errorLogger,e.getMessage(), AppMessages.QUERYERROR +""+updateQuery ,
852 ErrorSeverity.MAJOR, ErrorTypes.QUERYERROR);
856 public static boolean getIsCadi() {
857 return MusicUtil.isCadi;
862 * @return a random uuid
864 public static String generateUUID() {
865 String uuid = UUID.randomUUID().toString();
866 logger.info(EELFLoggerDelegate.applicationLogger,"New AID generated: "+uuid);