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 * ====================================================================
23 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 javax.ws.rs.core.Response;
34 import javax.ws.rs.core.Response.ResponseBuilder;
35 import org.onap.music.datastore.PreparedQueryObject;
36 import org.onap.music.eelf.logging.EELFLoggerDelegate;
37 import com.datastax.driver.core.DataType;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.Properties;
48 * Properties This will take Properties and load them into MusicUtil.
49 * This is a hack for now. Eventually it would bebest to do this in
53 public class MusicUtil {
54 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
56 public static final String ATOMIC = "atomic";
57 public static final String EVENTUAL = "eventual";
58 public static final String CRITICAL = "critical";
59 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
60 public static final String DEFAULTKEYSPACENAME = "TBD";
61 private static final String XLATESTVERSION = "X-latestVersion";
62 private static final String XMINORVERSION = "X-minorVersion";
63 private static final String XPATCHVERSION = "X-patchVersion";
65 private static final String LOCALHOST = "localhost";
66 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
68 private static int myId = 0;
69 private static ArrayList<String> allIds = new ArrayList<>();
70 private static String publicIp = "";
71 private static ArrayList<String> allPublicIps = new ArrayList<>();
72 private static String myZkHost = LOCALHOST;
73 private static String myCassaHost = LOCALHOST;
74 private static String defaultMusicIp = LOCALHOST;
75 private static boolean debug = true;
76 private static String version = "2.3.0";
77 private static String musicRestIp = LOCALHOST;
78 private static String musicPropertiesFilePath = PROPERTIES_FILE;
79 private static long defaultLockLeasePeriod = 6000;
80 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
81 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
82 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
84 private static String cassName = "cassandra";
85 private static String cassPwd = "cassandra";
86 private static String aafEndpointUrl = null;
89 throw new IllegalStateException("Utility Class");
94 * @return the cassName
96 public static String getCassName() {
101 * @return the cassPwd
103 public static String getCassPwd() {
108 * @return the aafEndpointUrl
110 public static String getAafEndpointUrl() {
111 return aafEndpointUrl;
116 * @param aafEndpointUrl
118 public static void setAafEndpointUrl(String aafEndpointUrl) {
119 MusicUtil.aafEndpointUrl = aafEndpointUrl;
126 public static int getMyId() {
134 public static void setMyId(int myId) {
135 MusicUtil.myId = myId;
142 public static List<String> getAllIds() {
150 public static void setAllIds(List<String> allIds) {
151 MusicUtil.allIds = (ArrayList<String>) allIds;
158 public static String getPublicIp() {
166 public static void setPublicIp(String publicIp) {
167 MusicUtil.publicIp = publicIp;
174 public static List<String> getAllPublicIps() {
180 * @param allPublicIps
182 public static void setAllPublicIps(List<String> allPublicIps) {
183 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
187 * Returns An array of property names that should be in the Properties
192 public static String[] getPropkeys() {
197 * Get MusicRestIp - default = localhost property file value - music.rest.ip
201 public static String getMusicRestIp() {
210 public static void setMusicRestIp(String musicRestIp) {
211 MusicUtil.musicRestIp = musicRestIp;
215 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
216 * property file value - music.properties
220 public static String getMusicPropertiesFilePath() {
221 return musicPropertiesFilePath;
225 * Set MusicPropertiesFilePath
227 * @param musicPropertiesFilePath
229 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
230 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
234 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
239 public static long getDefaultLockLeasePeriod() {
240 return defaultLockLeasePeriod;
244 * Set DefaultLockLeasePeriod
246 * @param defaultLockLeasePeriod
248 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
249 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
257 public static void setDebug(boolean debug) {
258 MusicUtil.debug = debug;
262 * Is Debug - Default = true property file value - debug
266 public static boolean isDebug() {
275 public static void setVersion(String version) {
276 MusicUtil.version = version;
280 * Return the version property file value - version
284 public static String getVersion() {
289 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
290 * value - zookeeper.host
294 public static String getMyZkHost() {
299 * Set MyZkHost - Zookeeper Hostname
303 public static void setMyZkHost(String myZkHost) {
304 MusicUtil.myZkHost = myZkHost;
308 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
309 * value - cassandra.host
313 public static String getMyCassaHost() {
318 * Set MyCassHost - Cassandra Hostname
322 public static void setMyCassaHost(String myCassaHost) {
323 MusicUtil.myCassaHost = myCassaHost;
327 * Get DefaultMusicIp - Default = localhost property file value - music.ip
331 public static String getDefaultMusicIp() {
332 return defaultMusicIp;
338 * @param defaultMusicIp
340 public static void setDefaultMusicIp(String defaultMusicIp) {
341 MusicUtil.defaultMusicIp = defaultMusicIp;
348 public static String getTestType() {
349 String testType = "";
351 Scanner fileScanner = new Scanner(new File(""));
352 testType = fileScanner.next();// ignore the my id line
353 String batchSize = fileScanner.next();// ignore the my public ip
356 } catch (FileNotFoundException e) {
357 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
367 public static void sleep(long time) {
370 } catch (InterruptedException e) {
371 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
372 Thread.currentThread().interrupt();
377 * Utility function to check if the query object is valid.
383 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
385 int noOfValues = queryObject.getValues().size();
387 char[] temp = queryObject.getQuery().toCharArray();
388 for (int i = 0; i < temp.length; i++) {
392 return (noOfValues == noOfParams);
394 return !queryObject.getQuery().isEmpty();
399 public static void setCassName(String cassName) {
400 MusicUtil.cassName = cassName;
403 public static void setCassPwd(String cassPwd) {
404 MusicUtil.cassPwd = cassPwd;
407 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
410 switch (type.getName()) {
412 value = valueObj + "";
416 String valueString = valueObj + "";
417 valueString = valueString.replace("'", "''");
418 value = "'" + valueString + "'";
421 Map<String, Object> otMap = (Map<String, Object>) valueObj;
422 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
426 value = valueObj + "";
437 * @throws MusicTypeConversionException
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 valueObjString;
466 * Utility function to parse json map into sql like string
469 * @param lineDelimiter
473 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
474 StringBuilder sqlString = new StringBuilder();
476 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
477 Object ot = entry.getValue();
478 String value = ot + "";
479 if (ot instanceof String) {
480 value = "'" + value.replace("'", "''") + "'";
482 sqlString.append("'" + entry.getKey() + "':" + value);
483 if (counter != jMap.size() - 1)
484 sqlString.append(lineDelimiter);
485 counter = counter + 1;
487 return sqlString.toString();
490 @SuppressWarnings("unused")
491 public static String buildVersion(String major, String minor, String patch) {
493 major += "." + minor;
495 major += "." + patch;
502 * Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
503 * X-latestVerstion will be equal to the latest full version.
504 * X-minorVersion - will be equal to the latest minor version.
505 * X-pathVersion - will be equal to the latest patch version.
506 * Future plans will change this.
513 public static ResponseBuilder buildVersionResponse(String major, String minor, String patch) {
514 ResponseBuilder response = Response.noContent();
515 String versionIn = buildVersion(major,minor,patch);
516 String version = MusicUtil.getVersion();
517 String[] verArray = version.split("\\.",3);
518 if ( minor != null ) {
519 response.header(XMINORVERSION,minor);
521 response.header(XMINORVERSION,verArray[1]);
523 if ( patch != null ) {
524 response.header(XPATCHVERSION,patch);
526 response.header(XPATCHVERSION,verArray[2]);
528 response.header(XLATESTVERSION,version);
529 logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
534 public static void loadProperties() throws Exception {
535 CipherUtil.readAndSetKeyString();
536 Properties prop = new Properties();
537 InputStream input = null;
539 // load the properties file
540 input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
542 } catch (Exception ex) {
543 logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
544 throw new Exception();
549 } catch (IOException e) {
554 String cassPwd = prop.getProperty("cassandra.password");
555 String isEncrypted = prop.getProperty("cassandra.password.isencrypted");
556 logger.info(EELFLoggerDelegate.applicationLogger,"cassandra.password:" + cassPwd);
557 logger.info(EELFLoggerDelegate.applicationLogger,"cassandra.password.isencrypted:" + isEncrypted);
558 if("true".equals(isEncrypted)) {
559 logger.info(EELFLoggerDelegate.applicationLogger,"Decrypting....");
560 cassPwd = CipherUtil.decryptPKC(cassPwd);
561 logger.info(EELFLoggerDelegate.applicationLogger,"Decrypted password: "+cassPwd);
562 MusicUtil.setCassPwd(cassPwd);
564 MusicUtil.setCassPwd(cassPwd);
565 // get the property value and return it
566 MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host"));
567 String zkHosts = prop.getProperty("zookeeper.host");
568 MusicUtil.setMyZkHost(zkHosts);
569 MusicUtil.setCassName(prop.getProperty("cassandra.user"));