summaryrefslogtreecommitdiffstats
path: root/ecomp-portal-BE-common/src/main/java/org/onap/portalapp/music/service/MusicService.java
blob: e6d9c323ac183399a04b8e3543802cbca5109337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*
 * ============LICENSE_START==========================================
 * ONAP Portal
 * ===================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * ===================================================================
 *
 * Unless otherwise specified, all software contained herein is licensed
 * under the Apache License, Version 2.0 (the "License");
 * you may not use this software except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *             https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END============================================
 *
 *.
 */

package org.onap.portalapp.music.service;

import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.eelf.logging.EELFLoggerDelegate;
import org.onap.music.exceptions.MusicLockingException;
import org.onap.music.exceptions.MusicQueryException;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.music.main.CipherUtil;
import org.onap.music.main.MusicCore;
import org.onap.music.main.ResultType;
import org.onap.music.main.ReturnType;
import org.onap.portalapp.music.conf.MusicSession;
import org.onap.portalapp.music.model.RestResponse;
import org.onap.portalapp.music.model.RestStatusEnum;
import org.onap.portalapp.music.util.MusicProperties;
import org.onap.portalapp.music.util.MusicUtil;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.session.Session;
import org.springframework.web.client.RestTemplate;
import org.onap.music.datastore.MusicDataStore;

import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class MusicService {
	static RestTemplate template = new RestTemplate();
	private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicService.class);
	
    
    public static void init() {
    	try {
        	Properties prop = new Properties();
        	// We load encryption key from key.properties on the classpath. This key is used for decrypting cassandra password
            try(InputStream input = MusicUtil.class.getClassLoader().getResourceAsStream("key.properties")) {
                prop.load(input);
            } catch (Exception var11) {
                logger.error(EELFLoggerDelegate.errorLogger, "Unable to find properties file.");
                //throw new Exception();
            }
        	try {
        		// Load music.properties from classpath
                org.onap.music.main.MusicUtil.loadProperties();
                // decrypt encrypted password using the key we loaded before.
                String decryptedPassword = CipherUtil.decryptPKC(org.onap.music.main.MusicUtil.getCassPwd(), prop.getProperty("cipher.enc.key"));
                logger.debug(EELFLoggerDelegate.debugLogger, "Cassandra Password Decrypted " + decryptedPassword);
                logger.debug(EELFLoggerDelegate.debugLogger, "Setting Decrypted password ");
                // set decrypted password 
                org.onap.music.main.MusicUtil.setCassPwd(decryptedPassword);
                logger.debug(EELFLoggerDelegate.debugLogger, "Creating cassandra connections pool and sessions by calling MusicDataStore and passing the cassandra hostname ");
                // Here we are creating cassandra connections pool and sessions by calling MusicDataStore and passing the cassandrra hostname to that. 
                MusicCore.mDstoreHandle = new MusicDataStore(org.onap.music.main.MusicUtil.getMyCassaHost());
				// Since mDstoreHandle is already initialized in init mthod, calling this method again will have no impact on mDstoreHandle.
                logger.debug(EELFLoggerDelegate.debugLogger, "Calling MusicCore getDSHandle() method ");
                MusicCore.getDSHandle();
            } catch (Exception e) {
            	logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
            }
        	
        	} catch(Exception e) {
        		logger.error(EELFLoggerDelegate.errorLogger, e.getMessage(), e);
        	}
    }


	private static boolean isAtomicPut = MusicUtil.isAtomicPut();
	private static boolean isAtomicGet = MusicUtil.isAtomicGet();
	private static String musicKeySpace = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_KEYSPACE);
	private static String musicMetaTable = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_META_TABLES);
	private static String musicAttrTable = MusicProperties.getProperty(MusicProperties.MUSIC_SESSION_ATTR_TABLES);

  private static final String WITH_SESSION_ID = " with session id: ";
  private static final String RESULT = "result:";
  private static final String WHERE = " WHERE ";
  private static final String FROM = " FROM ";
  private static final String DELETE = "DELETE ";
  private static final String REMOVE_SESSION = "removeSession: ";
  private static final String SUCCESS = "success";
  public static final String ATOMIC = "atomic";
  public static final String EVENTUAL = "eventual";
  public static final String CRITICAL = "critical";

	/**
	 * Store session attribute name and values into Cassandra via Music
	 *
	 * @param attributeName
	 * @param value
	 * @param sessionId
	 * @return ReturnType that includes required body information for Music api
	 * @throws Exception
	 */
	public static ReturnType setAttribute(String attributeName, Object value, String sessionId) throws MusicLockingException {
		logger.debug(EELFLoggerDelegate.debugLogger, "setAttribute: start with id " + sessionId);
		String tableName = null;
		ReturnType result = null;
		boolean isMeta = MusicUtil.isSessionMetaAttr(attributeName);
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append("INSERT INTO ").append(musicKeySpace).append(".").append(getTableName(isMeta))
				.append(getInsertQuery(isMeta, attributeName));

		queryObject.appendQueryString(querySB.toString());
		if (isMeta) {
			queryObject.addValue(sessionId);
			queryObject.addValue(String.valueOf(value));
			tableName = musicMetaTable;
		} else {
			queryObject.addValue(sessionId);
			queryObject.addValue(attributeName);
			queryObject.addValue(MusicUtil.musicSerialize(value));
			tableName = musicAttrTable;

		}
		if (isAtomicPut)
			result = MusicCore.atomicPut(musicKeySpace, tableName, sessionId, queryObject, null);
		else
			result = modEventualPut(queryObject);
		logger.debug(EELFLoggerDelegate.debugLogger, "setAttribute: attributeName: " + attributeName
				+ WITH_SESSION_ID + sessionId + RESULT + result.getMessage());
		return result;
	}

	/**
	 * Store session meta data values into Cassandra via Music
	 *
	 * @param session
	 * @return ReturnType that includes required body information for Music api
	 * @throws Exception
	 */
	public static ReturnType setMetaAttribute(Session session) throws MusicLockingException {
		logger.debug(EELFLoggerDelegate.debugLogger, "setMetaAttribute: start with session id: " + session.getId());
		ReturnType result = null;
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append("INSERT INTO ").append(musicKeySpace).append(".").append(musicMetaTable).append("(")
				.append(MusicProperties.PRIMARY_ID).append(",").append(MusicProperties.CREATION_TIME).append(",")
				.append(MusicProperties.LAST_ACCESS_TIME).append(",").append(MusicProperties.MAX_INACTIVE_INTERVAL)
				.append(",").append(MusicProperties.SESSION_ID).append(") VALUES (?,?,?,?,?);");

		queryObject.appendQueryString(querySB.toString());
		queryObject.addValue(session.getId());
		queryObject.addValue(String.valueOf(session.getCreationTime()));
		queryObject.addValue(String.valueOf(session.getLastAccessedTime()));
		queryObject.addValue(String.valueOf(session.getMaxInactiveInterval()));
		queryObject.addValue(session.getId());
		if (isAtomicPut)
			result = MusicCore.atomicPut(musicKeySpace, musicMetaTable, session.getId(), queryObject, null);
		else
			result = modEventualPut(queryObject);
		logger.debug(EELFLoggerDelegate.debugLogger,
				"setMetaAttribute: with session id: " + session + RESULT + result.getMessage());

		return result;
	}

	/**
	 * Retrieve session meta data from Cassandra via Music
	 *
	 * @param sessionId
	 * @return MusicSession
	 * @throws Exception
	 */
	public static MusicSession getMetaAttribute(String sessionId) throws MusicLockingException,MusicServiceException {
		logger.debug(EELFLoggerDelegate.debugLogger, "getMetaAttribute: start with session Id: "+ sessionId);
		ResultSet result = null;
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append("SELECT * FROM ").append(musicKeySpace).append(".").append(musicMetaTable).append(WHERE)
				.append(MusicProperties.PRIMARY_ID).append("=?;");
		queryObject.appendQueryString(querySB.toString());
		queryObject.addValue(sessionId);
		if (isAtomicGet)
			result = MusicCore.atomicGet(musicKeySpace, musicMetaTable, sessionId, queryObject);
		else
			result = modEventualGet(queryObject);
		logger.debug(EELFLoggerDelegate.debugLogger, "getMetaAttribute: with session id: " + sessionId);
		return MusicUtil.parseMetaData(result.one());
	}

	
	/**
	 * Get proper column names (from meta or attribute table) base on isMeta
	 *
	 * @param isMeta
	 * @param attributeName
	 * @return String
	 */
	private static String getInsertQuery(boolean isMeta, String attributeName) {
		logger.debug(EELFLoggerDelegate.debugLogger, "getInsertQuery: start inserting : " + attributeName);
		StringBuilder querySB = new StringBuilder();
		if (isMeta) {
			querySB.append(" (").append(MusicProperties.PRIMARY_ID).append(",").append(attributeName)
					.append(") VALUES (?,?);");
		} else {
			querySB.append(" (").append(MusicProperties.PRIMARY_ID).append(",").append(MusicProperties.ATTRIBUTE_NAME)
					.append(",").append(MusicProperties.ATTRIBUTE_BYTES).append(") VALUES (?,?,?);");
		}
		return querySB.toString();
	}

	/**
	 * Retrieve session attribute data from Cassandra via Music
	 *
	 * @param attributeName
	 * @param sessionId
	 * @return attribute value with T type
	 * @throws Exception
	 */
	public static <T> T getAttribute(String attributeName, String sessionId) throws Exception {
		logger.debug(EELFLoggerDelegate.debugLogger, "getAttribute: start with session id: " + sessionId);
		ResultSet result = null;
		String tableName = null;
		boolean isMeta = MusicUtil.isSessionMetaAttr(attributeName);
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append("SELECT ").append(getColumn(attributeName, isMeta)).append(FROM).append(musicKeySpace)
				.append(".").append(getTableName(isMeta)).append(WHERE).append(MusicProperties.PRIMARY_ID)
				.append("= ?");

		queryObject.addValue(sessionId);
		if (!isMeta) {
			querySB.append(" and ").append(MusicProperties.ATTRIBUTE_NAME).append("= ?");
			queryObject.addValue(attributeName);
			tableName = musicAttrTable;
		} else
			tableName = musicMetaTable;

		queryObject.appendQueryString(querySB.toString());
		if (isAtomicGet)
			result = MusicCore.atomicGet(musicKeySpace, tableName, sessionId, queryObject);
		else
			result = modEventualGet(queryObject);
		return MusicUtil.musicRestResponseDataParsing(result, attributeName);

	}

	/**
	 * Remove session attribute data from Cassandra via Music
	 *
	 * @param attributeName
	 * @param sessionId
	 * @return ReturnType
	 * @throws MusicServiceException
	 * @throws MusicLockingException
	 */
	public static ReturnType removeAttribute(String attributeName, String sessionId) throws MusicLockingException {
		logger.debug(EELFLoggerDelegate.debugLogger, "removeAttribute: start with session id: " + sessionId);
		boolean isMeta = MusicUtil.isSessionMetaAttr(attributeName);
		ReturnType result = null;
		String tableName = null;
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append(DELETE).append(getDelColumn(isMeta, attributeName)).append(FROM).append(musicKeySpace)
				.append(".").append(getTableName(isMeta)).append(WHERE).append(MusicProperties.PRIMARY_ID)
				.append("= ? ");
		queryObject.addValue(sessionId);

		if (!isMeta) {
			querySB.append(" and ").append(MusicProperties.ATTRIBUTE_NAME).append("= ?");
			queryObject.addValue(attributeName);
			tableName = musicAttrTable;
		} else
			tableName = musicMetaTable;
		queryObject.appendQueryString(querySB.toString());
		if (isAtomicPut)
			result = MusicCore.atomicPut(musicKeySpace, tableName, sessionId, queryObject, null);
		else
			result = modEventualPut(queryObject);
		logger.debug(EELFLoggerDelegate.debugLogger,
        REMOVE_SESSION + attributeName + WITH_SESSION_ID + sessionId + RESULT + result.getMessage());

		return result;
	}

	/**
	 * Remove entire session from Cassandra via Music
	 *
	 * @param sessionId
	 * @return ReturnType
	 * @throws MusicServiceException
	 * @throws MusicLockingException
	 */
	public static ReturnType removeSession(String sessionId) throws MusicLockingException {
		ReturnType result = null;
		boolean isAtomic = isAtomicPut;
		logger.debug(EELFLoggerDelegate.debugLogger, "removeSession: start with session id: " + sessionId);
		PreparedQueryObject queryObject = new PreparedQueryObject();
		StringBuilder querySB = new StringBuilder();
		querySB.append(DELETE).append(FROM).append(musicKeySpace).append(".").append(musicMetaTable)
				.append(WHERE).append(MusicProperties.PRIMARY_ID).append("= ? ");
		queryObject.appendQueryString(querySB.toString());
		queryObject.addValue(sessionId);
		if (isAtomic)
			result = MusicCore.atomicPut(musicKeySpace, musicMetaTable, sessionId, queryObject, null);
		else
			result = modEventualPut(queryObject);
		logger.debug(EELFLoggerDelegate.debugLogger, REMOVE_SESSION + musicMetaTable + WITH_SESSION_ID
				+ sessionId + RESULT + result.getMessage());

		queryObject = new PreparedQueryObject();
		querySB = new StringBuilder();
		querySB.append(DELETE).append(FROM).append(musicKeySpace).append(".").append(musicAttrTable)
				.append(WHERE).append(MusicProperties.PRIMARY_ID).append("= ? ");
		queryObject.appendQueryString(querySB.toString());
		queryObject.addValue(sessionId);
		if (isAtomic)
			result = MusicCore.atomicPut(musicKeySpace, musicAttrTable, sessionId, queryObject, null);
		else
			result = modEventualPut(queryObject);

		logger.debug(EELFLoggerDelegate.debugLogger, REMOVE_SESSION + musicAttrTable + WITH_SESSION_ID
				+ sessionId + RESULT + result.getMessage());

		return result;
	}

	/**
	 * Get proper table name (Meta or Attribute) base on isMeta.
	 *
	 * @param isMeta
	 * @return String
	 */
	private static String getTableName(boolean isMeta) {
		StringBuilder querySB = new StringBuilder();
		if (isMeta)
			querySB.append(musicMetaTable);
		else
			querySB.append(musicAttrTable);
		return querySB.toString();
	}

	/**
	 * Get proper column name (Meta or Attribute) base on isMeta.
	 *
	 * @param attributeName
	 * @param isMeta
	 * @return String
	 */
	private static String getColumn(String attributeName, boolean isMeta) {
		StringBuilder querySB = new StringBuilder();
		if (isMeta)
			querySB.append(attributeName);
		else
			querySB.append("attribute_bytes");
		return querySB.toString();
	}

	/**
	 * Get proper column name (Meta or Attribute) base on isMeta for removing.
	 *
	 * @param attributeName
	 * @param isMeta
	 * @return String
	 */
	private static String getDelColumn(boolean isMeta, String attributeName) {
		StringBuilder querySB = new StringBuilder();
		if (isMeta)
			querySB.append(attributeName);
		return querySB.toString();
	}

	/**
	 * To set session attributes in Music
	 *
	 * @param attributeName
	 * @param value
	 * @param session
	 * @param sessionId
	 * @param isMeta
	 * @return RestResponse<String>
	 * @throws JsonProcessingException
	 */

	public static RestResponse<String> setAttributeAPI(String attributeName, Object value, Session session,
			String sessionId, String className, boolean isMeta) throws JsonProcessingException {
		logger.debug(EELFLoggerDelegate.debugLogger, "setAttribute: " + attributeName);
		RestResponse<String> portalRestResponse = null;
		HttpEntity<Map<String, Object>> entity = new HttpEntity<>(
				getMusicRestBody(attributeName, value, sessionId, session, className, isMeta), getMusicHeader());
		String url = getInsertUrl(isMeta);
		ResponseEntity<String> response = null;
		try {
			response = template.exchange(url, HttpMethod.POST, entity, String.class);
			portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, e.getLocalizedMessage());
			portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
		}
		return portalRestResponse;
	}

	/**
	 * To get session attribute in Music
	 *
	 * @param attributeName
	 * @param value
	 * @param sessionId
	 * @param isMeta
	 * @return RestResponse<String>
	 */

	public static RestResponse<String> getAttributeAPI(String attributeName, Object value, String sessionId,
			boolean isMeta) {
		logger.debug(EELFLoggerDelegate.debugLogger, "setAttribute: " + attributeName);
		RestResponse<String> portalRestResponse = null;
		HttpEntity<String> entity = new HttpEntity<>(null, getMusicHeader());
		ResponseEntity<String> response = null;
		String url = getSelectSessionIdUrl(attributeName, sessionId, isMeta);
		try {
			response = template.exchange(url, HttpMethod.GET, entity, String.class);
			portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, e.getLocalizedMessage());
			portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
		}
		return portalRestResponse;
	}

	/**
	 * To remove session attribute or session meta in Music
	 *
	 * @param attributeName
	 * @param sessionId
	 * @param isMeta
	 * @return RestResponse<String>
	 */
	public static RestResponse<String> removeAttributeAPI(String attributeName, String sessionId, boolean isMeta) {
		RestResponse<String> portalRestResponse = null;
		HttpEntity<Map<String, Object>> entity = new HttpEntity<>(getMusicDelRestBody(null), getMusicHeader());
		ResponseEntity<String> response = null;
		String url = getSelectSessionIdUrl(attributeName, sessionId, true);
		try {
			url = getSelectSessionIdUrl(attributeName, sessionId, false);
			response = template.exchange(url, HttpMethod.DELETE, entity, String.class);
			portalRestResponse = new RestResponse<>(RestStatusEnum.OK, SUCCESS, response.getBody());
		} catch (Exception e) {
			logger.debug(EELFLoggerDelegate.debugLogger, e.getLocalizedMessage());
			portalRestResponse = new RestResponse<>(RestStatusEnum.ERROR, e.getMessage(), null);
		}
		return portalRestResponse;
	}

	/**
	 * Generate body for Music api calls
	 *
	 * @return String that includes required body information for Music api
	 *         calls
	 * @throws JsonProcessingException
	 */
	public static Map<String, Object> getMusicRestBody(String attributeName, Object value, String sessionId,
			Session session, String className, boolean isMeta) throws JsonProcessingException {
		Map<String, Object> map = new HashMap<>();
		/* Set up column values */
		Map<String, Object> valueMap = new HashMap<>();
		if (isMeta) {
			valueMap.put(MusicProperties.PRIMARY_ID, session.getId());
			valueMap.put(MusicProperties.SESSION_ID, session.getId());
			valueMap.put(MusicProperties.CREATION_TIME, session.getCreationTime().toString());
			valueMap.put(MusicProperties.LAST_ACCESS_TIME, session.getLastAccessedTime().toString());
			valueMap.put(MusicProperties.MAX_INACTIVE_INTERVAL, session.getMaxInactiveInterval().toString());
		} else {
			ObjectMapper mapper = new ObjectMapper();
			valueMap.put(MusicProperties.PRIMARY_ID, sessionId);
			valueMap.put(MusicProperties.ATTRIBUTE_NAME, attributeName);
			valueMap.put(MusicProperties.ATTRIBUTE_BYTES, mapper.writeValueAsString(value));
			valueMap.put(MusicProperties.ATTRIBUTE_CLASS, className);
		}
		map.put("values", valueMap);
		/* Set up consistency setting */
		Map<String, String> consistencyInfoMap = new HashMap<>();
		consistencyInfoMap.put(MusicProperties.getProperty(MusicProperties.MUSIC_CONSISTENCYINFO),
				MusicProperties.getProperty(MusicProperties.MUSIC_CONSISTENCYINFO_VALUE));
		map.put("consistencyInfo", consistencyInfoMap);
		return map;
	}

	/**
	 * Generate body for Music delete api calls
	 *
	 * @return String that includes required body information for Music api
	 *         calls
	 * @throws JsonProcessingException
	 */
	public static Map<String, Object> getMusicDelRestBody(String attributeName) {
		Map<String, Object> map = new HashMap<>();
		Map<String, String> consistencyInfoMap = new HashMap<>();
		consistencyInfoMap.put(MusicProperties.getProperty(MusicProperties.MUSIC_CONSISTENCYINFO),
				MusicProperties.getProperty(MusicProperties.MUSIC_CONSISTENCYINFO_VALUE));
		if (attributeName != null && !attributeName.isEmpty()) {
			Map<String, String> conditionsMap = new HashMap<>();
			conditionsMap.put("attribute_name", attributeName);
			map.put("conditions", conditionsMap);
		}
		map.put("consistencyInfo", consistencyInfoMap);
		return map;
	}

	private static String getSelectSessionIdUrl(String attributeName, String sessionId, boolean isMeta) {
		String path = constructPath(isMeta);
		StringBuilder attriPath = new StringBuilder();
		attriPath.append(path).append(MusicProperties.getProperty(MusicProperties.MUSIC_ROWS)).append("?")
				.append(MusicProperties.PRIMARY_ID).append("=").append(sessionId);
		return attriPath.toString();
	}

	private static String getInsertUrl(boolean isMeta) {
		String path = constructPath(isMeta);
		StringBuilder attriPath = new StringBuilder();
		attriPath.append(path);
		attriPath.append(MusicProperties.getProperty(MusicProperties.MUSIC_ROWS));
		return attriPath.toString();
	}

	/**
	 * Generate header for Music api calls
	 *
	 * @return header that contains required header information for Music api
	 *         calls
	 */
	private static HttpHeaders getMusicHeader() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.add("X-minorVersion", MusicProperties.getProperty(MusicProperties.MUSIC_X_MINOR_VERSION));
		headers.add("X-patchVersion", MusicProperties.getProperty(MusicProperties.MUSIC_X_PATCH_VERSION));
		headers.add("ns", MusicProperties.getProperty(MusicProperties.MUSIC_NS));
		headers.add("userId", MusicProperties.getProperty(MusicProperties.MUSIC_USER_ID));
		headers.add("password", MusicProperties.getProperty(MusicProperties.MUSIC_PASSWORD));
		return headers;
	}

	/**
	 * Construct URL for Music api calls
	 *
	 * @return path
	 */
	private static String constructPath(boolean isMeta) {
		StringBuilder path = new StringBuilder();
		path.append(MusicProperties.getProperty(MusicProperties.MUSIC_ENDPOINT))
				.append(MusicProperties.getProperty(MusicProperties.MUSIC_VERSION)).append("/")
				.append(MusicProperties.getProperty(MusicProperties.MUSIC_KEYSPACE)).append("/").append(musicKeySpace)
				.append("/").append(MusicProperties.getProperty(MusicProperties.MUSIC_TABLES)).append("/");
		if (isMeta)
			path.append(musicMetaTable);
		else
			path.append(musicAttrTable);
		path.append("/");
		return path.toString();
	}

	/**
	 * Get a list of sessions that need to be cleaned up
	 *
	 * @return List<String>
	 */
	private static List<String> getSessionToBeDeleted(){
		logger.debug(EELFLoggerDelegate.debugLogger, "initial getSessionToBeDeleted ...");

		PreparedQueryObject queryObject = new PreparedQueryObject();
		ResultSet result = null;
		List<String> sessionIDList = new ArrayList<>();
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
		StringBuilder querySB = new StringBuilder();
		String cleanUpThreshold = MusicProperties.getProperty(MusicProperties.MUSIC_CLEAN_UP_THRESHOLD); //Clean up sessions that's cleanUpThreshold hours ago
		Date dateForCleanup = new Date(System.currentTimeMillis() - 3600 * 1000 * Integer.valueOf(cleanUpThreshold)); // Get the threshold date that needs to be clean up
		String dateForCleanupCondition = dateFormat.format(dateForCleanup);
		querySB.append("SELECT ").append(MusicProperties.PRIMARY_ID).append(FROM).append(musicKeySpace)
		.append(".").append(getTableName(true)).append(WHERE).append(MusicProperties.LAST_ACCESS_TIME)
		.append("< ? ").append(" ALLOW FILTERING");
		queryObject.appendQueryString(querySB.toString());
		queryObject.addValue(dateForCleanupCondition);

		try{
			if (isAtomicGet)
				result = MusicCore.atomicGet(musicKeySpace, musicMetaTable, null, queryObject);
			else
				result = modEventualGet(queryObject);
			Row row = result.one();
			while(row!=null){
				sessionIDList.add(row.get(MusicProperties.PRIMARY_ID, String.class));
				row = result.one();
			}
		}catch(Exception e){
			logger.error(EELFLoggerDelegate.errorLogger, "Error while cleaning up music tables" , e);
		}
		return sessionIDList;
	}

	/**
	 * Remove session data in music base on the defined frequency
	 *
	 * @return List<String>
	 */
	public static void cleanUpMusic() {
		boolean timeToCleanUp = MusicUtil.cleanUp(); // Decide whether to clean up or not

		if(timeToCleanUp){
			/**Getting a list of sessions that need to be cleaned up*/
			List<String> sessionIDList = getSessionToBeDeleted();
			if(sessionIDList!=null || !sessionIDList.isEmpty()){
				StringBuilder sessionIDListCondition = new StringBuilder();
				sessionIDListCondition.append("('");
				for(String s : sessionIDList){
					sessionIDListCondition.append(s);
					sessionIDListCondition.append("','");
				}
				sessionIDListCondition.deleteCharAt(sessionIDListCondition.length()-1);
				sessionIDListCondition.deleteCharAt(sessionIDListCondition.length()-1);
				sessionIDListCondition.append(")");
				StringBuilder querySB = new StringBuilder();
				PreparedQueryObject queryObject = new PreparedQueryObject();
				/**Deleting attributes table**/
				querySB = new StringBuilder();
				queryObject = new PreparedQueryObject();
				querySB.append("DELETE FROM ").append(musicKeySpace)
				.append(".").append(getTableName(false)).append(WHERE).append(MusicProperties.PRIMARY_ID)
				.append(" in ").append(sessionIDListCondition);
				queryObject.appendQueryString(querySB.toString());
				try{
					if (isAtomicPut)
						MusicCore.atomicPut(musicKeySpace, null, null, queryObject, null);
					else
						modEventualPut(queryObject);
				}catch(Exception e){
					logger.error(EELFLoggerDelegate.errorLogger, "Error while cleaning up music attributes tables" , e);
				}
				logger.debug(EELFLoggerDelegate.debugLogger, "Music sessions have been cleaned up !");

				/**Deleting meta table**/
				logger.debug(EELFLoggerDelegate.debugLogger, "Cleaning up meta table ...");
				querySB = new StringBuilder();
				queryObject = new PreparedQueryObject();
				querySB.append("DELETE FROM ").append(musicKeySpace)
				.append(".").append(getTableName(true)).append(WHERE).append(MusicProperties.PRIMARY_ID)
				.append(" in ").append(sessionIDListCondition);
				queryObject.appendQueryString(querySB.toString());
				try{
					if (isAtomicPut)
						MusicCore.atomicPut(musicKeySpace, null, null, queryObject, null);
					else
						modEventualPut(queryObject);
				}catch(Exception e){
					logger.error(EELFLoggerDelegate.errorLogger, "Error while cleaning up music meta tables" , e);
				}

				logger.debug(EELFLoggerDelegate.debugLogger, "Cleaned up attributes table ... ");
			}else{
				logger.debug(EELFLoggerDelegate.debugLogger, "No Session needs to be cleaned up");
			}

		}
	}

	public static ReturnType modEventualPut(PreparedQueryObject queryObject) {
		 boolean result = false;
	        try {
	            result = MusicCore.getDSHandle().executePut(queryObject,CRITICAL);
	        } catch (MusicServiceException | MusicQueryException ex) {
	        	 logger.error(EELFLoggerDelegate.errorLogger,ex.getMessage() + "  " + ex.getCause() + " " + ex);
	            return new ReturnType(ResultType.FAILURE, ex.getMessage());
	        }
	        if (result) {
	            return new ReturnType(ResultType.SUCCESS, "Success");
	        } else {
	            return new ReturnType(ResultType.FAILURE, "Failure");
	        }
	}
	
	public static ResultSet modEventualGet(PreparedQueryObject queryObject) throws MusicServiceException {
		ResultSet result;
		result = MusicCore.quorumGet(queryObject);
		return result;
	}


}