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