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 javax.ws.rs.core.Response;
33 import javax.ws.rs.core.Response.ResponseBuilder;
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.eelf.logging.EELFLoggerDelegate;
36 import com.datastax.driver.core.DataType;
41 * Properties This will take Properties and load them into MusicUtil.
42 * This is a hack for now. Eventually it would bebest to do this in
46 public class MusicUtil {
47 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
49 public static final String ATOMIC = "atomic";
50 public static final String EVENTUAL = "eventual";
51 public static final String CRITICAL = "critical";
52 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
53 public static final String DEFAULTKEYSPACENAME = "TBD";
54 private static final String XLATESTVERSION = "X-latestVersion";
55 private static final String XMINORVERSION = "X-minorVersion";
56 private static final String XPATCHVERSION = "X-patchVersion";
57 public static final String SELECT = "select";
58 public static final String INSERT = "insert";
59 public static final String UPDATE = "update";
60 public static final String UPSERT = "upsert";
62 private static final String LOCALHOST = "localhost";
63 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
65 private static int myId = 0;
66 private static ArrayList<String> allIds = new ArrayList<>();
67 private static String publicIp = "";
68 private static ArrayList<String> allPublicIps = new ArrayList<>();
69 private static String myZkHost = LOCALHOST;
70 private static String myCassaHost = LOCALHOST;
71 private static String defaultMusicIp = LOCALHOST;
72 private static boolean debug = true;
73 private static String version = "2.3.0";
74 private static String musicRestIp = LOCALHOST;
75 private static String musicPropertiesFilePath = PROPERTIES_FILE;
76 private static long defaultLockLeasePeriod = 6000;
77 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
78 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
79 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
81 private static String cassName = "cassandra";
82 private static String cassPwd;
83 private static String aafEndpointUrl = null;
86 throw new IllegalStateException("Utility Class");
91 * @return the cassName
93 public static String getCassName() {
100 public static String getCassPwd() {
105 * @return the aafEndpointUrl
107 public static String getAafEndpointUrl() {
108 return aafEndpointUrl;
113 * @param aafEndpointUrl
115 public static void setAafEndpointUrl(String aafEndpointUrl) {
116 MusicUtil.aafEndpointUrl = aafEndpointUrl;
123 public static int getMyId() {
131 public static void setMyId(int myId) {
132 MusicUtil.myId = myId;
139 public static List<String> getAllIds() {
147 public static void setAllIds(List<String> allIds) {
148 MusicUtil.allIds = (ArrayList<String>) allIds;
155 public static String getPublicIp() {
163 public static void setPublicIp(String publicIp) {
164 MusicUtil.publicIp = publicIp;
171 public static List<String> getAllPublicIps() {
177 * @param allPublicIps
179 public static void setAllPublicIps(List<String> allPublicIps) {
180 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
184 * Returns An array of property names that should be in the Properties
189 public static String[] getPropkeys() {
194 * Get MusicRestIp - default = localhost property file value - music.rest.ip
198 public static String getMusicRestIp() {
207 public static void setMusicRestIp(String musicRestIp) {
208 MusicUtil.musicRestIp = musicRestIp;
212 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
213 * property file value - music.properties
217 public static String getMusicPropertiesFilePath() {
218 return musicPropertiesFilePath;
222 * Set MusicPropertiesFilePath
224 * @param musicPropertiesFilePath
226 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
227 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
231 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
236 public static long getDefaultLockLeasePeriod() {
237 return defaultLockLeasePeriod;
241 * Set DefaultLockLeasePeriod
243 * @param defaultLockLeasePeriod
245 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
246 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
254 public static void setDebug(boolean debug) {
255 MusicUtil.debug = debug;
259 * Is Debug - Default = true property file value - debug
263 public static boolean isDebug() {
272 public static void setVersion(String version) {
273 MusicUtil.version = version;
277 * Return the version property file value - version
281 public static String getVersion() {
286 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
287 * value - zookeeper.host
291 public static String getMyZkHost() {
296 * Set MyZkHost - Zookeeper Hostname
300 public static void setMyZkHost(String myZkHost) {
301 MusicUtil.myZkHost = myZkHost;
305 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
306 * value - cassandra.host
310 public static String getMyCassaHost() {
315 * Set MyCassHost - Cassandra Hostname
319 public static void setMyCassaHost(String myCassaHost) {
320 MusicUtil.myCassaHost = myCassaHost;
324 * Get DefaultMusicIp - Default = localhost property file value - music.ip
328 public static String getDefaultMusicIp() {
329 return defaultMusicIp;
335 * @param defaultMusicIp
337 public static void setDefaultMusicIp(String defaultMusicIp) {
338 MusicUtil.defaultMusicIp = defaultMusicIp;
345 public static String getTestType() {
346 String testType = "";
348 Scanner fileScanner = new Scanner(new File(""));
349 testType = fileScanner.next();// ignore the my id line
350 @SuppressWarnings("unused")
351 String batchSize = fileScanner.next();// ignore the my public ip
354 } catch (FileNotFoundException e) {
355 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
365 public static void sleep(long time) {
368 } catch (InterruptedException e) {
369 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
370 Thread.currentThread().interrupt();
375 * Utility function to check if the query object is valid.
381 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
383 int noOfValues = queryObject.getValues().size();
385 char[] temp = queryObject.getQuery().toCharArray();
386 for (int i = 0; i < temp.length; i++) {
390 return (noOfValues == noOfParams);
392 return !queryObject.getQuery().isEmpty();
397 public static void setCassName(String cassName) {
398 MusicUtil.cassName = cassName;
401 public static void setCassPwd(String cassPwd) {
402 MusicUtil.cassPwd = cassPwd;
405 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
408 switch (type.getName()) {
410 value = valueObj + "";
414 String valueString = valueObj + "";
415 valueString = valueString.replace("'", "''");
416 value = "'" + valueString + "'";
419 @SuppressWarnings("unchecked")
420 Map<String, Object> otMap = (Map<String, Object>) valueObj;
421 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
425 value = valueObj + "";
436 * @throws MusicTypeConversionException
439 @SuppressWarnings("unchecked")
440 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
441 String valueObjString = valueObj + "";
442 switch (colType.getName()) {
444 return UUID.fromString(valueObjString);
446 return BigInteger.valueOf(Long.parseLong(valueObjString));
448 return Long.parseLong(valueObjString);
450 return Integer.parseInt(valueObjString);
452 return Float.parseFloat(valueObjString);
454 return Double.parseDouble(valueObjString);
456 return Boolean.parseBoolean(valueObjString);
458 return (Map<String, Object>) valueObj;
460 return (List<String>)valueObj;
462 return valueObjString;
468 * Utility function to parse json map into sql like string
471 * @param lineDelimiter
475 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
476 StringBuilder sqlString = new StringBuilder();
478 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
479 Object ot = entry.getValue();
480 String value = ot + "";
481 if (ot instanceof String) {
482 value = "'" + value.replace("'", "''") + "'";
484 sqlString.append("'" + entry.getKey() + "':" + value);
485 if (counter != jMap.size() - 1)
486 sqlString.append(lineDelimiter);
487 counter = counter + 1;
489 return sqlString.toString();
492 public static String buildVersion(String major, String minor, String patch) {
494 major += "." + minor;
496 major += "." + patch;
503 * Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
504 * X-latestVerstion will be equal to the latest full version.
505 * X-minorVersion - will be equal to the latest minor version.
506 * X-pathVersion - will be equal to the latest patch version.
507 * Future plans will change this.
514 public static ResponseBuilder buildVersionResponse(String major, String minor, String patch) {
515 ResponseBuilder response = Response.noContent();
516 String versionIn = buildVersion(major,minor,patch);
517 String version = MusicUtil.getVersion();
518 String[] verArray = version.split("\\.",3);
519 if ( minor != null ) {
520 response.header(XMINORVERSION,minor);
522 response.header(XMINORVERSION,verArray[1]);
524 if ( patch != null ) {
525 response.header(XPATCHVERSION,patch);
527 response.header(XPATCHVERSION,verArray[2]);
529 response.header(XLATESTVERSION,version);
530 logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);