Initial code Import.
[music.git] / src / main / java / org / onap / music / main / MusicUtil.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.main;
23
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.math.BigInteger;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map;
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 com.datastax.driver.core.DataType;
36
37 /**
38  * @author nelson24
39  * 
40  *         Properties This will take Properties and load them into MusicUtil. This is a hack for
41  *         now. Eventually it would bebest to do this in another way.
42  * 
43  */
44 public class MusicUtil {
45     private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
46
47     private static int myId = 0;
48     private static ArrayList<String> allIds = new ArrayList<String>();
49     private static String publicIp = "";
50     private static ArrayList<String> allPublicIps = new ArrayList<String>();
51     private static String myZkHost = "localhost";
52     private static String myCassaHost = "localhost";
53     private static String defaultMusicIp = "localhost";
54     private static boolean debug = true;
55     private static String version = "2.3.0";
56     public static String musicRestIp = "localhost";
57     private static String musicPropertiesFilePath = "/opt/app/music/etc/music.properties";
58     private static long defaultLockLeasePeriod = 6000;
59     private static final String[] propKeys = new String[] {"zookeeper.host", "cassandra.host",
60                     "music.ip", "debug", "version", "music.rest.ip", "music.properties",
61                     "lock.lease.period", "id", "all.ids", "public.ip", "all.pubic.ips",
62                     "cassandra.user", "cassandra.password", "aaf.endpoint.url"};
63
64     public static final String ATOMIC = "atomic";
65     public static final String EVENTUAL = "eventual";
66     public static final String CRITICAL = "critical";
67     public static final String DEFAULTKEYSPACENAME = "TBD";
68     private static String cassName = "cassandra";
69     private static String cassPwd = "cassandra";
70     private static String aafEndpointUrl = null;
71
72     /**
73      * @return the cassName
74      */
75     public static String getCassName() {
76         return cassName;
77     }
78
79     /**
80      * @return the cassPwd
81      */
82     public static String getCassPwd() {
83         return cassPwd;
84     }
85
86     /**
87      * @return the aafEndpointUrl
88      */
89     public static String getAafEndpointUrl() {
90         return aafEndpointUrl;
91     }
92
93     /**
94      * 
95      * @param aafEndpointUrl
96      */
97     public static void setAafEndpointUrl(String aafEndpointUrl) {
98         MusicUtil.aafEndpointUrl = aafEndpointUrl;
99     }
100
101     /**
102      * 
103      * @return
104      */
105     public static int getMyId() {
106         return myId;
107     }
108
109     /**
110      * 
111      * @param myId
112      */
113     public static void setMyId(int myId) {
114         MusicUtil.myId = myId;
115     }
116
117
118     /**
119      * 
120      * @return
121      */
122     public static ArrayList<String> getAllIds() {
123         return allIds;
124     }
125
126     /**
127      * 
128      * @param allIds
129      */
130     public static void setAllIds(List<String> allIds) {
131         MusicUtil.allIds = (ArrayList<String>) allIds;
132     }
133
134     /**
135      * 
136      * @return
137      */
138     public static String getPublicIp() {
139         return publicIp;
140     }
141
142     /**
143      * 
144      * @param publicIp
145      */
146     public static void setPublicIp(String publicIp) {
147         MusicUtil.publicIp = publicIp;
148     }
149
150     /**
151      * 
152      * @return
153      */
154     public static ArrayList<String> getAllPublicIps() {
155         return allPublicIps;
156     }
157
158     /**
159      * 
160      * @param allPublicIps
161      */
162     public static void setAllPublicIps(List<String> allPublicIps) {
163         MusicUtil.allPublicIps = (ArrayList<String>) allPublicIps;
164     }
165
166     /**
167      * Returns An array of property names that should be in the Properties files.
168      * 
169      * @return
170      */
171     public static String[] getPropkeys() {
172         return propKeys;
173     }
174
175     /**
176      * Get MusicRestIp - default = localhost property file value - music.rest.ip
177      * 
178      * @return
179      */
180     public static String getMusicRestIp() {
181         return musicRestIp;
182     }
183
184     /**
185      * Set MusicRestIp
186      * 
187      * @param musicRestIp
188      */
189     public static void setMusicRestIp(String musicRestIp) {
190         MusicUtil.musicRestIp = musicRestIp;
191     }
192
193     /**
194      * Get MusicPropertiesFilePath - Default = /opt/music/music.properties property file value -
195      * music.properties
196      * 
197      * @return
198      */
199     public static String getMusicPropertiesFilePath() {
200         return musicPropertiesFilePath;
201     }
202
203     /**
204      * Set MusicPropertiesFilePath
205      * 
206      * @param musicPropertiesFilePath
207      */
208     public static void setMusicPropertiesFilePath(String musicPropertiesFilePath) {
209         MusicUtil.musicPropertiesFilePath = musicPropertiesFilePath;
210     }
211
212     /**
213      * Get DefaultLockLeasePeriod - Default = 6000 property file value - lock.lease.period
214      * 
215      * @return
216      */
217     public static long getDefaultLockLeasePeriod() {
218         return defaultLockLeasePeriod;
219     }
220
221     /**
222      * Set DefaultLockLeasePeriod
223      * 
224      * @param defaultLockLeasePeriod
225      */
226     public static void setDefaultLockLeasePeriod(long defaultLockLeasePeriod) {
227         MusicUtil.defaultLockLeasePeriod = defaultLockLeasePeriod;
228     }
229
230     /**
231      * Set Debug
232      * 
233      * @param debug
234      */
235     public static void setDebug(boolean debug) {
236         MusicUtil.debug = debug;
237     }
238
239     /**
240      * Is Debug - Default = true property file value - debug
241      * 
242      * @return
243      */
244     public static boolean isDebug() {
245         return debug;
246     }
247
248     /**
249      * Set Version
250      * 
251      * @param version
252      */
253     public static void setVersion(String version) {
254         MusicUtil.version = version;
255     }
256
257     /**
258      * Return the version property file value - version
259      * 
260      * @return
261      */
262     public static String getVersion() {
263         return version;
264     }
265
266     /**
267      * Get MyZkHost - Zookeeper Hostname - Default = localhost property file value - zookeeper.host
268      * 
269      * @return
270      */
271     public static String getMyZkHost() {
272         return myZkHost;
273     }
274
275     /**
276      * Set MyZkHost - Zookeeper Hostname
277      * 
278      * @param myZkHost
279      */
280     public static void setMyZkHost(String myZkHost) {
281         MusicUtil.myZkHost = myZkHost;
282     }
283
284     /**
285      * Get MyCassHost - Cassandra Hostname - Default = localhost property file value -
286      * cassandra.host
287      * 
288      * @return
289      */
290     public static String getMyCassaHost() {
291         return myCassaHost;
292     }
293
294     /**
295      * Set MyCassHost - Cassandra Hostname
296      * 
297      * @param myCassaHost
298      */
299     public static void setMyCassaHost(String myCassaHost) {
300         MusicUtil.myCassaHost = myCassaHost;
301     }
302
303     /**
304      * Get DefaultMusicIp - Default = localhost property file value - music.ip
305      * 
306      * @return
307      */
308     public static String getDefaultMusicIp() {
309         return defaultMusicIp;
310     }
311
312     /**
313      * Set DefaultMusicIp
314      * 
315      * @param defaultMusicIp
316      */
317     public static void setDefaultMusicIp(String defaultMusicIp) {
318         MusicUtil.defaultMusicIp = defaultMusicIp;
319     }
320
321     /**
322      * 
323      * @return
324      */
325     public static String getTestType() {
326         String testType = "";
327         try {
328             Scanner fileScanner = new Scanner(new File(""));
329             testType = fileScanner.next();// ignore the my id line
330             String batchSize = fileScanner.next();// ignore the my public ip line
331             fileScanner.close();
332         } catch (FileNotFoundException e) {
333             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
334         }
335         return testType;
336
337     }
338
339     /**
340      * 
341      * @param time
342      */
343     public static void sleep(long time) {
344         try {
345             Thread.sleep(time);
346         } catch (InterruptedException e) {
347             logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
348         }
349     }
350
351     /**
352      * Utility function to check if the query object is valid.
353      * 
354      * @param withparams
355      * @param queryObject
356      * @return
357      */
358     public static boolean isValidQueryObject(boolean withparams, PreparedQueryObject queryObject) {
359         if (withparams) {
360             int noOfValues = queryObject.getValues().size();
361             int noOfParams = 0;
362             char[] temp = queryObject.getQuery().toCharArray();
363             for (int i = 0; i < temp.length; i++) {
364                 if (temp[i] == '?')
365                     noOfParams++;
366             }
367             return (noOfValues == noOfParams);
368         } else {
369             return !queryObject.getQuery().isEmpty();
370         }
371
372     }
373
374     public static void setCassName(String cassName) {
375         MusicUtil.cassName = cassName;
376     }
377
378     public static void setCassPwd(String cassPwd) {
379         MusicUtil.cassPwd = cassPwd;
380     }
381
382     public static String convertToCQLDataType(DataType type, Object valueObj) {
383
384         String value = "";
385         switch (type.getName()) {
386             case UUID:
387                 value = valueObj + "";
388                 break;
389             case TEXT:
390             case VARCHAR:
391                 String valueString = valueObj + "";
392                 valueString = valueString.replace("'", "''");
393                 value = "'" + valueString + "'";
394                 break;
395             case MAP: {
396                 Map<String, Object> otMap = (Map<String, Object>) valueObj;
397                 value = "{" + jsonMaptoSqlString(otMap, ",") + "}";
398                 break;
399             }
400             default:
401                 value = valueObj + "";
402                 break;
403         }
404         return value;
405     }
406
407
408     /**
409      * 
410      * @param colType
411      * @param valueObj
412      * @return
413      * @throws Exception
414      */
415     public static Object convertToActualDataType(DataType colType, Object valueObj) {
416         String valueObjString = valueObj + "";
417         switch (colType.getName()) {
418             case UUID:
419                 return UUID.fromString(valueObjString);
420             case VARINT:
421                 return BigInteger.valueOf(Long.parseLong(valueObjString));
422             case BIGINT:
423                 return Long.parseLong(valueObjString);
424             case INT:
425                 return Integer.parseInt(valueObjString);
426             case FLOAT:
427                 return Float.parseFloat(valueObjString);
428             case DOUBLE:
429                 return Double.parseDouble(valueObjString);
430             case BOOLEAN:
431                 return Boolean.parseBoolean(valueObjString);
432             case MAP:
433                 return (Map<String, Object>) valueObj;
434             default:
435                 return valueObjString;
436         }
437     }
438
439
440     /**
441      *
442      * Utility function to parse json map into sql like string
443      * 
444      * @param jMap
445      * @param lineDelimiter
446      * @return
447      */
448
449     public static String jsonMaptoSqlString(Map<String, Object> jMap, String lineDelimiter) {
450         StringBuilder sqlString = new StringBuilder();
451         int counter = 0;
452         for (Map.Entry<String, Object> entry : jMap.entrySet()) {
453             Object ot = entry.getValue();
454             String value = ot + "";
455             if (ot instanceof String) {
456                 value = "'" + value.replace("'", "''") + "'";
457             }
458             sqlString.append("'" + entry.getKey() + "':" + value);
459             if (counter != jMap.size() - 1)
460                 sqlString.append(lineDelimiter);
461             counter = counter + 1;
462         }
463         return sqlString.toString();
464     }
465 }