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.HashMap;
29 import java.util.List;
31 import java.util.Scanner;
32 import java.util.UUID;
33 import org.onap.music.datastore.PreparedQueryObject;
34 import org.onap.music.eelf.logging.EELFLoggerDelegate;
35 import org.onap.music.exceptions.MusicServiceException;
37 import com.datastax.driver.core.DataType;
42 * Properties This will take Properties and load them into MusicUtil.
43 * This is a hack for now. Eventually it would bebest to do this in
47 public class MusicUtil {
48 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
50 public static final String ATOMIC = "atomic";
51 public static final String EVENTUAL = "eventual";
52 public static final String CRITICAL = "critical";
53 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
55 public static final String DEFAULTKEYSPACENAME = "TBD";
58 private static int myId = 0;
59 private static ArrayList<String> allIds = new ArrayList<>();
60 private static String publicIp = "";
61 private static ArrayList<String> allPublicIps = new ArrayList<>();
62 private static String myZkHost = "localhost";
63 private static String myCassaHost = "localhost";
64 private static String defaultMusicIp = "localhost";
65 private static boolean debug = true;
66 private static String version = "2.3.0";
67 private static String musicRestIp = "localhost";
68 private static String musicPropertiesFilePath = "/opt/app/music/etc/music.properties";
69 private static long defaultLockLeasePeriod = 6000;
70 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
71 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
72 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
74 private static String cassName = "cassandra";
75 private static String cassPwd;
76 private static String aafEndpointUrl = null;
79 throw new IllegalStateException("Utility Class");
84 * @return the cassName
86 public static String getCassName() {
93 public static String getCassPwd() {
98 * @return the aafEndpointUrl
100 public static String getAafEndpointUrl() {
101 return aafEndpointUrl;
106 * @param aafEndpointUrl
108 public static void setAafEndpointUrl(String aafEndpointUrl) {
109 MusicUtil.aafEndpointUrl = aafEndpointUrl;
116 public static int getMyId() {
124 public static void setMyId(int myId) {
125 MusicUtil.myId = myId;
132 public static List<String> getAllIds() {
140 public static void setAllIds(List<String> allIds) {
141 MusicUtil.allIds = (ArrayList<String>) allIds;
148 public static String getPublicIp() {
156 public static void setPublicIp(String publicIp) {
157 MusicUtil.publicIp = publicIp;
164 public static List<String> getAllPublicIps() {
170 * @param allPublicIps
172 public static void setAllPublicIps(List<String> allPublicIps) {
173 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
177 * Returns An array of property names that should be in the Properties
182 public static String[] getPropkeys() {
187 * Get MusicRestIp - default = localhost property file value - music.rest.ip
191 public static String getMusicRestIp() {
200 public static void setMusicRestIp(String musicRestIp) {
201 MusicUtil.musicRestIp = musicRestIp;
205 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
206 * property file value - music.properties
210 public static String getMusicPropertiesFilePath() {
211 return musicPropertiesFilePath;
215 * Set MusicPropertiesFilePath
217 * @param musicPropertiesFilePath
219 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
220 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
224 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
229 public static long getDefaultLockLeasePeriod() {
230 return defaultLockLeasePeriod;
234 * Set DefaultLockLeasePeriod
236 * @param defaultLockLeasePeriod
238 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
239 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
247 public static void setDebug(boolean debug) {
248 MusicUtil.debug = debug;
252 * Is Debug - Default = true property file value - debug
256 public static boolean isDebug() {
265 public static void setVersion(String version) {
266 MusicUtil.version = version;
270 * Return the version property file value - version
274 public static String getVersion() {
279 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
280 * value - zookeeper.host
284 public static String getMyZkHost() {
289 * Set MyZkHost - Zookeeper Hostname
293 public static void setMyZkHost(String myZkHost) {
294 MusicUtil.myZkHost = myZkHost;
298 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
299 * value - cassandra.host
303 public static String getMyCassaHost() {
308 * Set MyCassHost - Cassandra Hostname
312 public static void setMyCassaHost(String myCassaHost) {
313 MusicUtil.myCassaHost = myCassaHost;
317 * Get DefaultMusicIp - Default = localhost property file value - music.ip
321 public static String getDefaultMusicIp() {
322 return defaultMusicIp;
328 * @param defaultMusicIp
330 public static void setDefaultMusicIp(String defaultMusicIp) {
331 MusicUtil.defaultMusicIp = defaultMusicIp;
338 public static String getTestType() {
339 String testType = "";
341 Scanner fileScanner = new Scanner(new File(""));
342 testType = fileScanner.next();// ignore the my id line
343 String batchSize = fileScanner.next();// ignore the my public ip
346 } catch (FileNotFoundException e) {
347 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
357 public static void sleep(long time) {
360 } catch (InterruptedException e) {
361 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
362 Thread.currentThread().interrupt();
367 * Utility function to check if the query object is valid.
373 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
375 int noOfValues = queryObject.getValues().size();
377 char[] temp = queryObject.getQuery().toCharArray();
378 for (int i = 0; i < temp.length; i++) {
382 return (noOfValues == noOfParams);
384 return !queryObject.getQuery().isEmpty();
389 public static void setCassName(String cassName) {
390 MusicUtil.cassName = cassName;
393 public static void setCassPwd(String cassPwd) {
394 MusicUtil.cassPwd = cassPwd;
397 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
400 switch (type.getName()) {
402 value = valueObj + "";
406 String valueString = valueObj + "";
407 valueString = valueString.replace("'", "''");
408 value = "'" + valueString + "'";
411 Map<String, Object> otMap = (Map<String, Object>) valueObj;
412 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
416 value = valueObj + "";
429 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception{
430 String valueObjString = valueObj + "";
431 switch (colType.getName()) {
433 return UUID.fromString(valueObjString);
435 return BigInteger.valueOf(Long.parseLong(valueObjString));
437 return Long.parseLong(valueObjString);
439 return Integer.parseInt(valueObjString);
441 return Float.parseFloat(valueObjString);
443 return Double.parseDouble(valueObjString);
445 return Boolean.parseBoolean(valueObjString);
447 return (Map<String, Object>) valueObj;
449 return valueObjString;
455 * Utility function to parse json map into sql like string
458 * @param lineDelimiter
462 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
463 StringBuilder sqlString = new StringBuilder();
465 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
466 Object ot = entry.getValue();
467 String value = ot + "";
468 if (ot instanceof String) {
469 value = "'" + value.replace("'", "''") + "'";
471 sqlString.append("'" + entry.getKey() + "':" + value);
472 if (counter != jMap.size() - 1)
473 sqlString.append(lineDelimiter);
474 counter = counter + 1;
476 return sqlString.toString();