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;
38 import java.io.FileNotFoundException;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.util.Properties;
47 * Properties This will take Properties and load them into MusicUtil.
48 * This is a hack for now. Eventually it would bebest to do this in
52 public class MusicUtil {
53 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
55 public static final String ATOMIC = "atomic";
56 public static final String EVENTUAL = "eventual";
57 public static final String CRITICAL = "critical";
58 public static final String ATOMICDELETELOCK = "atomic_delete_lock";
59 public static final String DEFAULTKEYSPACENAME = "TBD";
60 private static final String XLATESTVERSION = "X-latestVersion";
61 private static final String XMINORVERSION = "X-minorVersion";
62 private static final String XPATCHVERSION = "X-patchVersion";
64 private static final String LOCALHOST = "localhost";
65 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
67 private static int myId = 0;
68 private static ArrayList<String> allIds = new ArrayList<>();
69 private static String publicIp = "";
70 private static ArrayList<String> allPublicIps = new ArrayList<>();
71 private static String myZkHost = LOCALHOST;
72 private static String myCassaHost = LOCALHOST;
73 private static String defaultMusicIp = LOCALHOST;
74 private static boolean debug = true;
75 private static String version = "2.3.0";
76 private static String musicRestIp = LOCALHOST;
77 private static String musicPropertiesFilePath = PROPERTIES_FILE;
78 private static long defaultLockLeasePeriod = 6000;
79 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
80 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
81 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
83 private static String cassName = "cassandra";
84 private static String cassPwd = "cassandra";
85 private static String aafEndpointUrl = null;
88 throw new IllegalStateException("Utility Class");
93 * @return the cassName
95 public static String getCassName() {
100 * @return the cassPwd
102 public static String getCassPwd() {
107 * @return the aafEndpointUrl
109 public static String getAafEndpointUrl() {
110 return aafEndpointUrl;
115 * @param aafEndpointUrl
117 public static void setAafEndpointUrl(String aafEndpointUrl) {
118 MusicUtil.aafEndpointUrl = aafEndpointUrl;
125 public static int getMyId() {
133 public static void setMyId(int myId) {
134 MusicUtil.myId = myId;
141 public static List<String> getAllIds() {
149 public static void setAllIds(List<String> allIds) {
150 MusicUtil.allIds = (ArrayList<String>) allIds;
157 public static String getPublicIp() {
165 public static void setPublicIp(String publicIp) {
166 MusicUtil.publicIp = publicIp;
173 public static List<String> getAllPublicIps() {
179 * @param allPublicIps
181 public static void setAllPublicIps(List<String> allPublicIps) {
182 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
186 * Returns An array of property names that should be in the Properties
191 public static String[] getPropkeys() {
196 * Get MusicRestIp - default = localhost property file value - music.rest.ip
200 public static String getMusicRestIp() {
209 public static void setMusicRestIp(String musicRestIp) {
210 MusicUtil.musicRestIp = musicRestIp;
214 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
215 * property file value - music.properties
219 public static String getMusicPropertiesFilePath() {
220 return musicPropertiesFilePath;
224 * Set MusicPropertiesFilePath
226 * @param musicPropertiesFilePath
228 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
229 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
233 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
238 public static long getDefaultLockLeasePeriod() {
239 return defaultLockLeasePeriod;
243 * Set DefaultLockLeasePeriod
245 * @param defaultLockLeasePeriod
247 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
248 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
256 public static void setDebug(boolean debug) {
257 MusicUtil.debug = debug;
261 * Is Debug - Default = true property file value - debug
265 public static boolean isDebug() {
274 public static void setVersion(String version) {
275 MusicUtil.version = version;
279 * Return the version property file value - version
283 public static String getVersion() {
288 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
289 * value - zookeeper.host
293 public static String getMyZkHost() {
298 * Set MyZkHost - Zookeeper Hostname
302 public static void setMyZkHost(String myZkHost) {
303 MusicUtil.myZkHost = myZkHost;
307 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
308 * value - cassandra.host
312 public static String getMyCassaHost() {
317 * Set MyCassHost - Cassandra Hostname
321 public static void setMyCassaHost(String myCassaHost) {
322 MusicUtil.myCassaHost = myCassaHost;
326 * Get DefaultMusicIp - Default = localhost property file value - music.ip
330 public static String getDefaultMusicIp() {
331 return defaultMusicIp;
337 * @param defaultMusicIp
339 public static void setDefaultMusicIp(String defaultMusicIp) {
340 MusicUtil.defaultMusicIp = defaultMusicIp;
347 public static String getTestType() {
348 String testType = "";
350 Scanner fileScanner = new Scanner(new File(""));
351 testType = fileScanner.next();// ignore the my id line
352 String batchSize = fileScanner.next();// ignore the my public ip
355 } catch (FileNotFoundException e) {
356 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
366 public static void sleep(long time) {
369 } catch (InterruptedException e) {
370 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
371 Thread.currentThread().interrupt();
376 * Utility function to check if the query object is valid.
382 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
384 int noOfValues = queryObject.getValues().size();
386 char[] temp = queryObject.getQuery().toCharArray();
387 for (int i = 0; i < temp.length; i++) {
391 return (noOfValues == noOfParams);
393 return !queryObject.getQuery().isEmpty();
398 public static void setCassName(String cassName) {
399 MusicUtil.cassName = cassName;
402 public static void setCassPwd(String cassPwd) {
403 MusicUtil.cassPwd = cassPwd;
406 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
409 switch (type.getName()) {
411 value = valueObj + "";
415 String valueString = valueObj + "";
416 valueString = valueString.replace("'", "''");
417 value = "'" + valueString + "'";
420 Map<String, Object> otMap = (Map<String, Object>) valueObj;
421 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
425 value = valueObj + "";
436 * @throws MusicTypeConversionException
439 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
440 String valueObjString = valueObj + "";
441 switch (colType.getName()) {
443 return UUID.fromString(valueObjString);
445 return BigInteger.valueOf(Long.parseLong(valueObjString));
447 return Long.parseLong(valueObjString);
449 return Integer.parseInt(valueObjString);
451 return Float.parseFloat(valueObjString);
453 return Double.parseDouble(valueObjString);
455 return Boolean.parseBoolean(valueObjString);
457 return (Map<String, Object>) valueObj;
459 return valueObjString;
465 * Utility function to parse json map into sql like string
468 * @param lineDelimiter
472 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
473 StringBuilder sqlString = new StringBuilder();
475 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
476 Object ot = entry.getValue();
477 String value = ot + "";
478 if (ot instanceof String) {
479 value = "'" + value.replace("'", "''") + "'";
481 sqlString.append("'" + entry.getKey() + "':" + value);
482 if (counter != jMap.size() - 1)
483 sqlString.append(lineDelimiter);
484 counter = counter + 1;
486 return sqlString.toString();
489 @SuppressWarnings("unused")
490 public static String buildVersion(String major, String minor, String patch) {
492 major += "." + minor;
494 major += "." + patch;
501 * Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
502 * X-latestVerstion will be equal to the latest full version.
503 * X-minorVersion - will be equal to the latest minor version.
504 * X-pathVersion - will be equal to the latest patch version.
505 * Future plans will change this.
512 public static ResponseBuilder buildVersionResponse(String major, String minor, String patch) {
513 ResponseBuilder response = Response.noContent();
514 String versionIn = buildVersion(major,minor,patch);
515 String version = MusicUtil.getVersion();
516 String[] verArray = version.split("\\.",3);
517 if ( minor != null ) {
518 response.header(XMINORVERSION,minor);
520 response.header(XMINORVERSION,verArray[1]);
522 if ( patch != null ) {
523 response.header(XPATCHVERSION,patch);
525 response.header(XPATCHVERSION,verArray[2]);
527 response.header(XLATESTVERSION,version);
528 logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);
533 public static void loadProperties() throws Exception {
534 Properties prop = new Properties();
535 InputStream input = null;
537 // load the properties file
538 input = MusicUtil.class.getClassLoader().getResourceAsStream("music.properties");
540 } catch (Exception ex) {
541 logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
542 throw new Exception();
547 } catch (IOException e) {
552 // get the property value and return it
553 MusicUtil.setMyCassaHost(prop.getProperty("cassandra.host"));
554 String zkHosts = prop.getProperty("zookeeper.host");
555 MusicUtil.setMyZkHost(zkHosts);
556 MusicUtil.setCassName(prop.getProperty("cassandra.user"));
557 MusicUtil.setCassPwd(prop.getProperty("cassandra.password"));