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