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 private static int myId = 0;
51 private static ArrayList<String> allIds = new ArrayList<String>();
52 private static String publicIp = "";
53 private static ArrayList<String> allPublicIps = new ArrayList<String>();
54 private static String myZkHost = "localhost";
55 private static String myCassaHost = "localhost";
56 private static String defaultMusicIp = "localhost";
57 private static boolean debug = true;
58 private static String version = "2.3.0";
59 public static String musicRestIp = "localhost";
60 private static String musicPropertiesFilePath = "/opt/app/music/etc/music.properties";
61 private static long defaultLockLeasePeriod = 6000;
62 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
63 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
64 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
66 public static final String ATOMIC = "atomic";
67 public static final String EVENTUAL = "eventual";
68 public static final String CRITICAL = "critical";
69 public static final String DEFAULTKEYSPACENAME = "TBD";
70 private static String cassName = "cassandra";
71 private static String cassPwd = "cassandra";
72 private static String aafEndpointUrl = null;
75 * @return the cassName
77 public static String getCassName() {
84 public static String getCassPwd() {
89 * @return the aafEndpointUrl
91 public static String getAafEndpointUrl() {
92 return aafEndpointUrl;
97 * @param aafEndpointUrl
99 public static void setAafEndpointUrl(String aafEndpointUrl) {
100 MusicUtil.aafEndpointUrl = aafEndpointUrl;
107 public static int getMyId() {
115 public static void setMyId(int myId) {
116 MusicUtil.myId = myId;
123 public static ArrayList<String> getAllIds() {
131 public static void setAllIds(List<String> allIds) {
132 MusicUtil.allIds = (ArrayList<String>) allIds;
139 public static String getPublicIp() {
147 public static void setPublicIp(String publicIp) {
148 MusicUtil.publicIp = publicIp;
155 public static ArrayList<String> getAllPublicIps() {
161 * @param allPublicIps
163 public static void setAllPublicIps(List<String> allPublicIps) {
164 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
168 * Returns An array of property names that should be in the Properties
173 public static String[] getPropkeys() {
178 * Get MusicRestIp - default = localhost property file value - music.rest.ip
182 public static String getMusicRestIp() {
191 public static void setMusicRestIp(String musicRestIp) {
192 MusicUtil.musicRestIp = musicRestIp;
196 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
197 * property file value - music.properties
201 public static String getMusicPropertiesFilePath() {
202 return musicPropertiesFilePath;
206 * Set MusicPropertiesFilePath
208 * @param musicPropertiesFilePath
210 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
211 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
215 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
220 public static long getDefaultLockLeasePeriod() {
221 return defaultLockLeasePeriod;
225 * Set DefaultLockLeasePeriod
227 * @param defaultLockLeasePeriod
229 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
230 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
238 public static void setDebug(boolean debug) {
239 MusicUtil.debug = debug;
243 * Is Debug - Default = true property file value - debug
247 public static boolean isDebug() {
256 public static void setVersion(String version) {
257 MusicUtil.version = version;
261 * Return the version property file value - version
265 public static String getVersion() {
270 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
271 * value - zookeeper.host
275 public static String getMyZkHost() {
280 * Set MyZkHost - Zookeeper Hostname
284 public static void setMyZkHost(String myZkHost) {
285 MusicUtil.myZkHost = myZkHost;
289 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
290 * value - cassandra.host
294 public static String getMyCassaHost() {
299 * Set MyCassHost - Cassandra Hostname
303 public static void setMyCassaHost(String myCassaHost) {
304 MusicUtil.myCassaHost = myCassaHost;
308 * Get DefaultMusicIp - Default = localhost property file value - music.ip
312 public static String getDefaultMusicIp() {
313 return defaultMusicIp;
319 * @param defaultMusicIp
321 public static void setDefaultMusicIp(String defaultMusicIp) {
322 MusicUtil.defaultMusicIp = defaultMusicIp;
329 public static String getTestType() {
330 String testType = "";
332 Scanner fileScanner = new Scanner(new File(""));
333 testType = fileScanner.next();// ignore the my id line
334 String batchSize = fileScanner.next();// ignore the my public ip
337 } catch (FileNotFoundException e) {
338 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
348 public static void sleep(long time) {
351 } catch (InterruptedException e) {
352 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
357 * Utility function to check if the query object is valid.
363 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
365 int noOfValues = queryObject.getValues().size();
367 char[] temp = queryObject.getQuery().toCharArray();
368 for (int i = 0; i < temp.length; i++) {
372 return (noOfValues == noOfParams);
374 return !queryObject.getQuery().isEmpty();
379 public static void setCassName(String cassName) {
380 MusicUtil.cassName = cassName;
383 public static void setCassPwd(String cassPwd) {
384 MusicUtil.cassPwd = cassPwd;
387 public static String convertToCQLDataType(DataType type, Object valueObj) {
390 switch (type.getName()) {
392 value = valueObj + "";
396 String valueString = valueObj + "";
397 valueString = valueString.replace("'", "''");
398 value = "'" + valueString + "'";
401 Map<String, Object> otMap = (Map<String, Object>) valueObj;
402 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
406 value = valueObj + "";
419 public static Object convertToActualDataType(DataType colType, Object valueObj) {
420 String valueObjString = valueObj + "";
421 switch (colType.getName()) {
423 return UUID.fromString(valueObjString);
425 return BigInteger.valueOf(Long.parseLong(valueObjString));
427 return Long.parseLong(valueObjString);
429 return Integer.parseInt(valueObjString);
431 return Float.parseFloat(valueObjString);
433 return Double.parseDouble(valueObjString);
435 return Boolean.parseBoolean(valueObjString);
437 return (Map<String, Object>) valueObj;
439 return valueObjString;
445 * Utility function to parse json map into sql like string
448 * @param lineDelimiter
452 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) {
453 StringBuilder sqlString = new StringBuilder();
455 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
456 Object ot = entry.getValue();
457 String value = ot + "";
458 if (ot instanceof String) {
459 value = "'" + value.replace("'", "''") + "'";
461 sqlString.append("'" + entry.getKey() + "':" + value);
462 if (counter != jMap.size() - 1)
463 sqlString.append(lineDelimiter);
464 counter = counter + 1;
466 return sqlString.toString();
469 public static Map<String, HashMap<String, Object>> setErrorResponse(MusicServiceException ex) {
470 Map<String, HashMap<String, Object>> results = new HashMap<>();
471 HashMap<String, Object> tempMap = new HashMap<>();
472 Map<String, Object> result = new HashMap<>();
473 result.put("Error Description", ex.getMessage());
474 tempMap.put("Error", result);
475 results.put("Result", tempMap);