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";
58 private static final String LOCALHOST = "localhost";
59 private static final String PROPERTIES_FILE = "/opt/app/music/etc/music.properties";
61 private static int myId = 0;
62 private static ArrayList<String> allIds = new ArrayList<>();
63 private static String publicIp = "";
64 private static ArrayList<String> allPublicIps = new ArrayList<>();
65 private static String myZkHost = LOCALHOST;
66 private static String myCassaHost = LOCALHOST;
67 private static String defaultMusicIp = LOCALHOST;
68 private static boolean debug = true;
69 private static String version = "2.3.0";
70 private static String musicRestIp = LOCALHOST;
71 private static String musicPropertiesFilePath = PROPERTIES_FILE;
72 private static long defaultLockLeasePeriod = 6000;
73 private static final String[] propKeys = new String[] { "zookeeper.host", "cassandra.host", "music.ip", "debug",
74 "version", "music.rest.ip", "music.properties", "lock.lease.period", "id", "all.ids", "public.ip",
75 "all.pubic.ips", "cassandra.user", "cassandra.password", "aaf.endpoint.url" };
77 private static String cassName = "cassandra";
78 private static String cassPwd;
79 private static String aafEndpointUrl = null;
82 throw new IllegalStateException("Utility Class");
87 * @return the cassName
89 public static String getCassName() {
96 public static String getCassPwd() {
101 * @return the aafEndpointUrl
103 public static String getAafEndpointUrl() {
104 return aafEndpointUrl;
109 * @param aafEndpointUrl
111 public static void setAafEndpointUrl(String aafEndpointUrl) {
112 MusicUtil.aafEndpointUrl = aafEndpointUrl;
119 public static int getMyId() {
127 public static void setMyId(int myId) {
128 MusicUtil.myId = myId;
135 public static List<String> getAllIds() {
143 public static void setAllIds(List<String> allIds) {
144 MusicUtil.allIds = (ArrayList<String>) allIds;
151 public static String getPublicIp() {
159 public static void setPublicIp(String publicIp) {
160 MusicUtil.publicIp = publicIp;
167 public static List<String> getAllPublicIps() {
173 * @param allPublicIps
175 public static void setAllPublicIps(List<String> allPublicIps) {
176 MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
180 * Returns An array of property names that should be in the Properties
185 public static String[] getPropkeys() {
190 * Get MusicRestIp - default = localhost property file value - music.rest.ip
194 public static String getMusicRestIp() {
203 public static void setMusicRestIp(String musicRestIp) {
204 MusicUtil.musicRestIp = musicRestIp;
208 * Get MusicPropertiesFilePath - Default = /opt/music/music.properties
209 * property file value - music.properties
213 public static String getMusicPropertiesFilePath() {
214 return musicPropertiesFilePath;
218 * Set MusicPropertiesFilePath
220 * @param musicPropertiesFilePath
222 public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
223 MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
227 * Get DefaultLockLeasePeriod - Default = 6000 property file value -
232 public static long getDefaultLockLeasePeriod() {
233 return defaultLockLeasePeriod;
237 * Set DefaultLockLeasePeriod
239 * @param defaultLockLeasePeriod
241 public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
242 MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
250 public static void setDebug(boolean debug) {
251 MusicUtil.debug = debug;
255 * Is Debug - Default = true property file value - debug
259 public static boolean isDebug() {
268 public static void setVersion(String version) {
269 MusicUtil.version = version;
273 * Return the version property file value - version
277 public static String getVersion() {
282 * Get MyZkHost - Zookeeper Hostname - Default = localhost property file
283 * value - zookeeper.host
287 public static String getMyZkHost() {
292 * Set MyZkHost - Zookeeper Hostname
296 public static void setMyZkHost(String myZkHost) {
297 MusicUtil.myZkHost = myZkHost;
301 * Get MyCassHost - Cassandra Hostname - Default = localhost property file
302 * value - cassandra.host
306 public static String getMyCassaHost() {
311 * Set MyCassHost - Cassandra Hostname
315 public static void setMyCassaHost(String myCassaHost) {
316 MusicUtil.myCassaHost = myCassaHost;
320 * Get DefaultMusicIp - Default = localhost property file value - music.ip
324 public static String getDefaultMusicIp() {
325 return defaultMusicIp;
331 * @param defaultMusicIp
333 public static void setDefaultMusicIp(String defaultMusicIp) {
334 MusicUtil.defaultMusicIp = defaultMusicIp;
341 public static String getTestType() {
342 String testType = "";
344 Scanner fileScanner = new Scanner(new File(""));
345 testType = fileScanner.next();// ignore the my id line
346 String batchSize = fileScanner.next();// ignore the my public ip
349 } catch (FileNotFoundException e) {
350 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
360 public static void sleep(long time) {
363 } catch (InterruptedException e) {
364 logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
365 Thread.currentThread().interrupt();
370 * Utility function to check if the query object is valid.
376 public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
378 int noOfValues = queryObject.getValues().size();
380 char[] temp = queryObject.getQuery().toCharArray();
381 for (int i = 0; i < temp.length; i++) {
385 return (noOfValues == noOfParams);
387 return !queryObject.getQuery().isEmpty();
392 public static void setCassName(String cassName) {
393 MusicUtil.cassName = cassName;
396 public static void setCassPwd(String cassPwd) {
397 MusicUtil.cassPwd = cassPwd;
400 public static String convertToCQLDataType(DataType type, Object valueObj) throws Exception {
403 switch (type.getName()) {
405 value = valueObj + "";
409 String valueString = valueObj + "";
410 valueString = valueString.replace("'", "''");
411 value = "'" + valueString + "'";
414 Map<String, Object> otMap = (Map<String, Object>) valueObj;
415 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
419 value = valueObj + "";
430 * @throws MusicTypeConversionException
433 public static Object convertToActualDataType(DataType colType, Object valueObj) throws Exception {
434 String valueObjString = valueObj + "";
435 switch (colType.getName()) {
437 return UUID.fromString(valueObjString);
439 return BigInteger.valueOf(Long.parseLong(valueObjString));
441 return Long.parseLong(valueObjString);
443 return Integer.parseInt(valueObjString);
445 return Float.parseFloat(valueObjString);
447 return Double.parseDouble(valueObjString);
449 return Boolean.parseBoolean(valueObjString);
451 return (Map<String, Object>) valueObj;
453 return valueObjString;
459 * Utility function to parse json map into sql like string
462 * @param lineDelimiter
466 public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) throws Exception{
467 StringBuilder sqlString = new StringBuilder();
469 for (Map.Entry<String, Object> entry : jMap.entrySet()) {
470 Object ot = entry.getValue();
471 String value = ot + "";
472 if (ot instanceof String) {
473 value = "'" + value.replace("'", "''") + "'";
475 sqlString.append("'" + entry.getKey() + "':" + value);
476 if (counter != jMap.size() - 1)
477 sqlString.append(lineDelimiter);
478 counter = counter + 1;
480 return sqlString.toString();
483 @SuppressWarnings("unused")
484 public static String buildVersion(String major, String minor, String patch) {
486 major += "." + minor;
488 major += "." + patch;
495 * Currently this will build a header with X-latestVersion, X-minorVersion and X-pathcVersion
496 * X-latestVerstion will be equal to the latest full version.
497 * X-minorVersion - will be equal to the latest minor version.
498 * X-pathVersion - will be equal to the latest patch version.
499 * Future plans will change this.
506 public static ResponseBuilder buildVersionResponse(String major, String minor, String patch) {
507 ResponseBuilder response = Response.noContent();
508 String versionIn = buildVersion(major,minor,patch);
509 String version = MusicUtil.getVersion();
510 String[] verArray = version.split("\\.",3);
511 if ( minor != null ) {
512 response.header(XMINORVERSION,minor);
514 response.header(XMINORVERSION,verArray[1]);
516 if ( patch != null ) {
517 response.header(XPATCHVERSION,patch);
519 response.header(XPATCHVERSION,verArray[2]);
521 response.header(XLATESTVERSION,version);
522 logger.info(EELFLoggerDelegate.applicationLogger,"Version In:" + versionIn);