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;
26 import java.io.FileNotFoundException;
27 import java.math.BigInteger;
28 import java.util.ArrayList;
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 com.datastax.driver.core.DataType;
40 * Properties This will take Properties and load them into MusicUtil. This is a hack for
41 * now. Eventually it would bebest to do this in another way.
44 public class MusicUtil {
45 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
47 private static int myId = 0;
48 private static ArrayList<String> allIds = new ArrayList<String>();
49 private static String publicIp = "";
50 private static ArrayList<String> allPublicIps = new ArrayList<String>();
51 private static String myZkHost = "localhost";
52 private static String myCassaHost = "localhost";
53 private static String defaultMusicIp = "localhost";
54 private static boolean debug = true;
55 private static String version = "2.3.0";
56 public static String musicRestIp = "localhost";
57 private static String musicPropertiesFilePath = "/opt/app/music/etc/music.properties";
58 private static long defaultLockLeasePeriod = 6000;
59 private static final String[] propKeys = new String[] {"zookeeper.host", "cassandra.host",
60 "music.ip", "debug", "version", "music.rest.ip", "music.properties",
61 "lock.lease.period", "id", "all.ids", "public.ip", "all.pubic.ips",
62 "cassandra.user", "cassandra.password", "aaf.endpoint.url"};
64 public static final String ATOMIC = "atomic";
65 public static final String EVENTUAL = "eventual";
66 public static final String CRITICAL = "critical";
67 public static final String DEFAULTKEYSPACENAME = "TBD";
68 private static String cassName = "cassandra";
69 private static String cassPwd = "cassandra";
70 private static String aafEndpointUrl = null;
73 * @return the cassName
75 public static String getCassName() {
82 public static String getCassPwd() {
87 * @return the aafEndpointUrl
89 public static String getAafEndpointUrl() {
90 return aafEndpointUrl;
95 * @param aafEndpointUrl
97 public static void setAafEndpointUrl(String aafEndpointUrl) {
98 MusicUtil.aafEndpointUrl = aafEndpointUrl;
105 public static int getMyId() {
113 public static void setMyId(int myId) {
114 MusicUtil.myId = myId;
122 public static ArrayList<String> getAllIds() {
130 public static void setAllIds(List<String> allIds) {
131 MusicUtil.allIds = (ArrayList<String>) allIds;
138 public static String getPublicIp() {
146 public static void setPublicIp(String publicIp) {
147 MusicUtil.publicIp = publicIp;
154 public static ArrayList<String> getAllPublicIps() {
160 * @param allPublicIps
162 public static void setAllPublicIps(List<String> allPublicIps) {
163 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
167 * Returns An array of property names that should be in the Properties files.
171 public static String[] getPropkeys() {
176 * Get MusicRestIp - default = localhost property file value - music.rest.ip
180 public static String getMusicRestIp() {
189 public static void setMusicRestIp(String musicRestIp) {
190 MusicUtil.musicRestIp = musicRestIp;
194 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties property file value -
199 public static String getMusicPropertiesFilePath() {
200 return musicPropertiesFilePath;
204 * Set MusicPropertiesFilePath
206 * @param musicPropertiesFilePath
208 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
209 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
213 * Get DefaultLockLeasePeriod - Default = 6000 property file value - lock.lease.period
217 public static long getDefaultLockLeasePeriod() {
218 return defaultLockLeasePeriod;
222 * Set DefaultLockLeasePeriod
224 * @param defaultLockLeasePeriod
226 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
227 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
235 public static void setDebug(boolean debug) {
236 MusicUtil.debug = debug;
240 * Is Debug - Default = true property file value - debug
244 public static boolean isDebug() {
253 public static void setVersion(String version) {
254 MusicUtil.version = version;
258 * Return the version property file value - version
262 public static String getVersion() {
267 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file value - zookeeper.host
271 public static String getMyZkHost() {
276 * Set MyZkHost - Zookeeper Hostname
280 public static void setMyZkHost(String myZkHost) {
281 MusicUtil.myZkHost = myZkHost;
285 * Get MyCassHost - Cassandra Hostname - Default = localhost property file value -
290 public static String getMyCassaHost() {
295 * Set MyCassHost - Cassandra Hostname
299 public static void setMyCassaHost(String myCassaHost) {
300 MusicUtil.myCassaHost = myCassaHost;
304 * Get DefaultMusicIp - Default = localhost property file value - music.ip
308 public static String getDefaultMusicIp() {
309 return defaultMusicIp;
315 * @param defaultMusicIp
317 public static void setDefaultMusicIp(String defaultMusicIp) {
318 MusicUtil.defaultMusicIp = defaultMusicIp;
325 public static String getTestType() {
326 String testType = "";
328 Scanner fileScanner = new Scanner(new File(""));
329 testType = fileScanner.next();// ignore the my id line
330 String batchSize = fileScanner.next();// ignore the my public ip line
332 } catch (FileNotFoundException e) {
333 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
343 public static void sleep(long time) {
346 } catch (InterruptedException e) {
347 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
352 * Utility function to check if the query object is valid.
358 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
360 int noOfValues = queryObject.getValues().size();
362 char[] temp = queryObject.getQuery().toCharArray();
363 for (int i = 0; i < temp.length; i++) {
367 return (noOfValues == noOfParams);
369 return !queryObject.getQuery().isEmpty();
374 public static void setCassName(String cassName) {
375 MusicUtil.cassName = cassName;
378 public static void setCassPwd(String cassPwd) {
379 MusicUtil.cassPwd = cassPwd;
382 public static String convertToCQLDataType(DataType type, Object valueObj) {
385 switch (type.getName()) {
387 value = valueObj + "";
391 String valueString = valueObj + "";
392 valueString = valueString.replace("'", "''");
393 value = "'" + valueString + "'";
396 Map<String, Object> otMap = (Map<String, Object>) valueObj;
397 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
401 value = valueObj + "";
415 public static Object convertToActualDataType(DataType colType, Object valueObj) {
416 String valueObjString = valueObj + "";
417 switch (colType.getName()) {
419 return UUID.fromString(valueObjString);
421 return BigInteger.valueOf(Long.parseLong(valueObjString));
423 return Long.parseLong(valueObjString);
425 return Integer.parseInt(valueObjString);
427 return Float.parseFloat(valueObjString);
429 return Double.parseDouble(valueObjString);
431 return Boolean.parseBoolean(valueObjString);
433 return (Map<String, Object>) valueObj;
435 return valueObjString;
442 * Utility function to parse json map into sql like string
445 * @param lineDelimiter
449 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) {
450 StringBuilder sqlString = new StringBuilder();
452 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
453 Object ot = entry.getValue();
454 String value = ot + "";
455 if (ot instanceof String) {
456 value = "'" + value.replace("'", "''") + "'";
458 sqlString.append("'" + entry.getKey() + "':" + value);
459 if (counter != jMap.size() - 1)
460 sqlString.append(lineDelimiter);
461 counter = counter + 1;
463 return sqlString.toString();