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 private 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 ATOMICDELETELOCK = "atomic_delete_lock";
71 public static final String DEFAULTKEYSPACENAME = "TBD";
72 private static String cassName = "cassandra";
73 private static String cassPwd;
74 private static String aafEndpointUrl = null;
77 * @return the cassName
79 public static String getCassName() {
86 public static String getCassPwd() {
91 * @return the aafEndpointUrl
93 public static String getAafEndpointUrl() {
94 return aafEndpointUrl;
99 * @param aafEndpointUrl
101 public static void setAafEndpointUrl(String aafEndpointUrl) {
102 MusicUtil.aafEndpointUrl = aafEndpointUrl;
109 public static int getMyId() {
117 public static void setMyId(int myId) {
118 MusicUtil.myId = myId;
125 public static ArrayList<String> getAllIds() {
133 public static void setAllIds(List<String> allIds) {
134 MusicUtil.allIds = (ArrayList<String>) allIds;
141 public static String getPublicIp() {
149 public static void setPublicIp(String publicIp) {
150 MusicUtil.publicIp = publicIp;
157 public static ArrayList<String> getAllPublicIps() {
163 * @param allPublicIps
165 public static void setAllPublicIps(List<String> allPublicIps) {
166 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
170 * Returns An array of property names that should be in the Properties
175 public static String[] getPropkeys() {
180 * Get MusicRestIp - default = localhost property file value - music.rest.ip
184 public static String getMusicRestIp() {
193 public static void setMusicRestIp(String musicRestIp) {
194 MusicUtil.musicRestIp = musicRestIp;
198 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
199 * property file value - music.properties
203 public static String getMusicPropertiesFilePath() {
204 return musicPropertiesFilePath;
208 * Set MusicPropertiesFilePath
210 * @param musicPropertiesFilePath
212 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
213 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
217 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
222 public static long getDefaultLockLeasePeriod() {
223 return defaultLockLeasePeriod;
227 * Set DefaultLockLeasePeriod
229 * @param defaultLockLeasePeriod
231 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
232 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
240 public static void setDebug(boolean debug) {
241 MusicUtil.debug = debug;
245 * Is Debug - Default = true property file value - debug
249 public static boolean isDebug() {
258 public static void setVersion(String version) {
259 MusicUtil.version = version;
263 * Return the version property file value - version
267 public static String getVersion() {
272 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
273 * value - zookeeper.host
277 public static String getMyZkHost() {
282 * Set MyZkHost - Zookeeper Hostname
286 public static void setMyZkHost(String myZkHost) {
287 MusicUtil.myZkHost = myZkHost;
291 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
292 * value - cassandra.host
296 public static String getMyCassaHost() {
301 * Set MyCassHost - Cassandra Hostname
305 public static void setMyCassaHost(String myCassaHost) {
306 MusicUtil.myCassaHost = myCassaHost;
310 * Get DefaultMusicIp - Default = localhost property file value - music.ip
314 public static String getDefaultMusicIp() {
315 return defaultMusicIp;
321 * @param defaultMusicIp
323 public static void setDefaultMusicIp(String defaultMusicIp) {
324 MusicUtil.defaultMusicIp = defaultMusicIp;
331 public static String getTestType() {
332 String testType = "";
334 Scanner fileScanner = new Scanner(new File(""));
335 testType = fileScanner.next();// ignore the my id line
336 String batchSize = fileScanner.next();// ignore the my public ip
339 } catch (FileNotFoundException e) {
340 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
350 public static void sleep(long time) {
353 } catch (InterruptedException e) {
354 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
359 * Utility function to check if the query object is valid.
365 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
367 int noOfValues = queryObject.getValues().size();
369 char[] temp = queryObject.getQuery().toCharArray();
370 for (int i = 0; i < temp.length; i++) {
374 return (noOfValues == noOfParams);
376 return !queryObject.getQuery().isEmpty();
381 public static void setCassName(String cassName) {
382 MusicUtil.cassName = cassName;
385 public static void setCassPwd(String cassPwd) {
386 MusicUtil.cassPwd = cassPwd;
389 public static String convertToCQLDataType(DataType type, Object valueObj) {
392 switch (type.getName()) {
394 value = valueObj + "";
398 String valueString = valueObj + "";
399 valueString = valueString.replace("'", "''");
400 value = "'" + valueString + "'";
403 Map<String, Object> otMap = (Map<String, Object>) valueObj;
404 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
408 value = valueObj + "";
421 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception{
422 String valueObjString = valueObj + "";
423 switch (colType.getName()) {
425 return UUID.fromString(valueObjString);
427 return BigInteger.valueOf(Long.parseLong(valueObjString));
429 return Long.parseLong(valueObjString);
431 return Integer.parseInt(valueObjString);
433 return Float.parseFloat(valueObjString);
435 return Double.parseDouble(valueObjString);
437 return Boolean.parseBoolean(valueObjString);
439 return (Map<String, Object>) valueObj;
441 return valueObjString;
447 * Utility function to parse json map into sql like string
450 * @param lineDelimiter
454 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) {
455 StringBuilder sqlString = new StringBuilder();
457 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
458 Object ot = entry.getValue();
459 String value = ot + "";
460 if (ot instanceof String) {
461 value = "'" + value.replace("'", "''") + "'";
463 sqlString.append("'" + entry.getKey() + "':" + value);
464 if (counter != jMap.size() - 1)
465 sqlString.append(lineDelimiter);
466 counter = counter + 1;
468 return sqlString.toString();
471 public static Map<String, HashMap<String, Object>> setErrorResponse(MusicServiceException ex) {
472 Map<String, HashMap<String, Object>> results = new HashMap<>();
473 HashMap<String, Object> tempMap = new HashMap<>();
474 Map<String, Object> result = new HashMap<>();
475 result.put("error-description", ex.getMessage());
476 tempMap.put("error", result);
477 results.put("result", tempMap);