2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (c) 2017 AT&T Intellectual Property
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.main;
25 import java.io.FileNotFoundException;
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.List;
30 import java.util.Scanner;
31 import java.util.UUID;
32 import org.onap.music.datastore.PreparedQueryObject;
33 import org.onap.music.eelf.logging.EELFLoggerDelegate;
34 import com.datastax.driver.core.DataType;
39 * Properties This will take Properties and load them into MusicUtil.
40 * This is a hack for now. Eventually it would bebest to do this in
44 public class MusicUtil {
45 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
47 public static final String ATOMIC = "atomic";
48 public static final String EVENTUAL = "eventual";
49 public static final String CRITICAL = "critical";
50 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
51 public static final String DEFAULTKEYSPACENAME = "TBD";
53 private static final String LOCALHOST = "localhost";
54 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
56 private static int myId = 0;
57 private static ArrayList<String> allIds = new ArrayList<>();
58 private static String publicIp = "";
59 private static ArrayList<String> allPublicIps = new ArrayList<>();
60 private static String myZkHost = LOCALHOST;
61 private static String myCassaHost = LOCALHOST;
62 private static String defaultMusicIp = LOCALHOST;
63 private static boolean debug = true;
64 private static String version = "2.3.0";
65 private static String musicRestIp = LOCALHOST;
66 private static String musicPropertiesFilePath = PROPERTIES_FILE;
67 private static long defaultLockLeasePeriod = 6000;
68 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
69 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
70 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
72 private static String cassName = "cassandra";
73 private static String cassPwd;
74 private static String aafEndpointUrl = null;
77 throw new IllegalStateException("Utility Class");
82 * @return the cassName
84 public static String getCassName() {
91 public static String getCassPwd() {
96 * @return the aafEndpointUrl
98 public static String getAafEndpointUrl() {
99 return aafEndpointUrl;
104 * @param aafEndpointUrl
106 public static void setAafEndpointUrl(String aafEndpointUrl) {
107 MusicUtil.aafEndpointUrl = aafEndpointUrl;
114 public static int getMyId() {
122 public static void setMyId(int myId) {
123 MusicUtil.myId = myId;
130 public static List<String> getAllIds() {
138 public static void setAllIds(List<String> allIds) {
139 MusicUtil.allIds = (ArrayList<String>) allIds;
146 public static String getPublicIp() {
154 public static void setPublicIp(String publicIp) {
155 MusicUtil.publicIp = publicIp;
162 public static List<String> getAllPublicIps() {
168 * @param allPublicIps
170 public static void setAllPublicIps(List<String> allPublicIps) {
171 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
175 * Returns An array of property names that should be in the Properties
180 public static String[] getPropkeys() {
185 * Get MusicRestIp - default = localhost property file value - music.rest.ip
189 public static String getMusicRestIp() {
198 public static void setMusicRestIp(String musicRestIp) {
199 MusicUtil.musicRestIp = musicRestIp;
203 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
204 * property file value - music.properties
208 public static String getMusicPropertiesFilePath() {
209 return musicPropertiesFilePath;
213 * Set MusicPropertiesFilePath
215 * @param musicPropertiesFilePath
217 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
218 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
222 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
227 public static long getDefaultLockLeasePeriod() {
228 return defaultLockLeasePeriod;
232 * Set DefaultLockLeasePeriod
234 * @param defaultLockLeasePeriod
236 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
237 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
245 public static void setDebug(boolean debug) {
246 MusicUtil.debug = debug;
250 * Is Debug - Default = true property file value - debug
254 public static boolean isDebug() {
263 public static void setVersion(String version) {
264 MusicUtil.version = version;
268 * Return the version property file value - version
272 public static String getVersion() {
277 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
278 * value - zookeeper.host
282 public static String getMyZkHost() {
287 * Set MyZkHost - Zookeeper Hostname
291 public static void setMyZkHost(String myZkHost) {
292 MusicUtil.myZkHost = myZkHost;
296 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
297 * value - cassandra.host
301 public static String getMyCassaHost() {
306 * Set MyCassHost - Cassandra Hostname
310 public static void setMyCassaHost(String myCassaHost) {
311 MusicUtil.myCassaHost = myCassaHost;
315 * Get DefaultMusicIp - Default = localhost property file value - music.ip
319 public static String getDefaultMusicIp() {
320 return defaultMusicIp;
326 * @param defaultMusicIp
328 public static void setDefaultMusicIp(String defaultMusicIp) {
329 MusicUtil.defaultMusicIp = defaultMusicIp;
336 public static String getTestType() {
337 String testType = "";
339 Scanner fileScanner = new Scanner(new File(""));
340 testType = fileScanner.next();// ignore the my id line
341 String batchSize = fileScanner.next();// ignore the my public ip
344 } catch (FileNotFoundException e) {
345 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
355 public static void sleep(long time) {
358 } catch (InterruptedException e) {
359 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
360 Thread.currentThread().interrupt();
365 * Utility function to check if the query object is valid.
371 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
373 int noOfValues = queryObject.getValues().size();
375 char[] temp = queryObject.getQuery().toCharArray();
376 for (int i = 0; i < temp.length; i++) {
380 return (noOfValues == noOfParams);
382 return !queryObject.getQuery().isEmpty();
387 public static void setCassName(String cassName) {
388 MusicUtil.cassName = cassName;
391 public static void setCassPwd(String cassPwd) {
392 MusicUtil.cassPwd = cassPwd;
395 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
398 switch (type.getName()) {
400 value = valueObj + "";
404 String valueString = valueObj + "";
405 valueString = valueString.replace("'", "''");
406 value = "'" + valueString + "'";
409 Map<String, Object> otMap = (Map<String, Object>) valueObj;
410 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
414 value = valueObj + "";
425 * @throws MusicTypeConversionException
428 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
429 String valueObjString = valueObj + "";
430 switch (colType.getName()) {
432 return UUID.fromString(valueObjString);
434 return BigInteger.valueOf(Long.parseLong(valueObjString));
436 return Long.parseLong(valueObjString);
438 return Integer.parseInt(valueObjString);
440 return Float.parseFloat(valueObjString);
442 return Double.parseDouble(valueObjString);
444 return Boolean.parseBoolean(valueObjString);
446 return (Map<String, Object>) valueObj;
448 return valueObjString;
454 * Utility function to parse json map into sql like string
457 * @param lineDelimiter
461 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
462 StringBuilder sqlString = new StringBuilder();
464 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
465 Object ot = entry.getValue();
466 String value = ot + "";
467 if (ot instanceof String) {
468 value = "'" + value.replace("'", "''") + "'";
470 sqlString.append("'" + entry.getKey() + "':" + value);
471 if (counter != jMap.size() - 1)
472 sqlString.append(lineDelimiter);
473 counter = counter + 1;
475 return sqlString.toString();