2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.music.util;
40 import java.io.ByteArrayInputStream;
41 import java.io.ByteArrayOutputStream;
42 import java.io.IOException;
43 import java.io.ObjectInputStream;
44 import java.io.ObjectOutputStream;
45 import java.nio.ByteBuffer;
46 import java.time.Duration;
47 import java.time.Instant;
48 import java.util.Arrays;
49 import java.util.Date;
50 import java.util.HashSet;
52 import java.util.zip.GZIPInputStream;
53 import java.util.zip.GZIPOutputStream;
55 import org.onap.music.eelf.logging.EELFLoggerDelegate;
56 import org.onap.portalapp.music.conf.MusicSession;
58 import com.datastax.driver.core.ResultSet;
59 import com.datastax.driver.core.Row;
61 public class MusicUtil {
62 private static final Set<String> sessionAttrNameSet = new HashSet<>(Arrays.asList("CREATION_TIME", "LAST_ACCESS_TIME","MAX_INACTIVE_INTERVAL","EXPIRY_TIME","PRINCIPAL_NAME"));
64 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MusicUtil.class);
65 private static String atomicPut = MusicProperties.getProperty(MusicProperties.MUSIC_ATOMIC_PUT);
66 private static String atomicGet = MusicProperties.getProperty(MusicProperties.MUSIC_ATOMIC_GET);
67 private static String cached = MusicProperties.getProperty(MusicProperties.MUSIC_CACHE);
68 private static String cleanUpFreq = MusicProperties.getProperty(MusicProperties.MUSIC_CLEAN_UP_FREQUENCY);
69 private static String musicSerializeCompress = MusicProperties.getProperty(MusicProperties.MUSIC_SERIALIZE_COMPRESS);
70 private static String musicEnable = MusicProperties.getProperty(MusicProperties.MUSIC_ENABLE);
71 private static final int MILLIS_IN_HOUR = 3600000;
73 public static boolean isSessionMetaAttr(String key){
74 return sessionAttrNameSet.contains(key);
77 public static <T> T musicRestResponseDataParsing(ResultSet rs, String attributeName) throws Exception{
78 logger.debug(EELFLoggerDelegate.debugLogger, "musicRestResponseDataParsing: start");
80 if(!sessionAttrNameSet.contains(attributeName)){
82 return MusicUtil.musicDeserialize(row.getBytes("attribute_bytes"));
84 return (T) row.getString(attributeName);
89 public static <T> T musicDeserialize (ByteBuffer byteBuf) throws Exception{
90 logger.debug(EELFLoggerDelegate.debugLogger, "musicDeserialize: start");
91 ByteArrayInputStream byteArr = new ByteArrayInputStream(byteBuf.array());
92 ObjectInputStream ois = null;
93 if(isMusicSerializeCompress()){
94 GZIPInputStream zos = new GZIPInputStream(byteArr);
95 ois = new ObjectInputStream(zos);
97 ois = new ObjectInputStream(byteArr);
99 return (T) ois.readObject();
102 public static ByteBuffer musicSerialize (Object value) throws Exception{
103 logger.debug(EELFLoggerDelegate.debugLogger, "musicSerialize: start");
104 ByteArrayOutputStream bo = new ByteArrayOutputStream();
106 if(isMusicSerializeCompress()){
107 GZIPOutputStream zos = new GZIPOutputStream(bo);
108 ObjectOutputStream oos = new ObjectOutputStream(zos);
109 oos.writeObject(value);
113 ObjectOutputStream oos = new ObjectOutputStream(bo);
114 oos.writeObject(value);
117 } catch (IOException e) {
118 logger.error(EELFLoggerDelegate.errorLogger, "Failed to serialize ");
120 return ByteBuffer.wrap(bo.toByteArray());
123 public static MusicSession parseMetaData (Row row) throws Exception{
124 logger.debug(EELFLoggerDelegate.debugLogger, "parseMetaData: start");
128 String sessionId = row.getString("primary_id");
129 MusicSession musicSession = new MusicSession(sessionId);
130 musicSession.setCreationTime(Instant.parse(row.getString("creation_time")));
131 musicSession.setLastAccessedTime(Instant.parse(row.getString("last_access_time")));
132 musicSession.setMaxInactiveInterval(Duration.parse(row.getString("max_inactive_interval")));
136 public static Set<String> getMusicExcludedAPI(){
137 Set<String> excludedApiSet = new HashSet<>();
138 String musicExcludedApi = MusicProperties.getProperty(MusicProperties.MUSIC_EXCLUDE_API);
139 String[] musicExcludedApiArray = musicExcludedApi.split(",");
140 if(musicExcludedApiArray.length>0){
141 for(String str : musicExcludedApiArray){
142 excludedApiSet.add(str.trim());
145 return excludedApiSet;
148 public static boolean isExcludedApi(String api){
149 Set<String> excludedApiSet = getMusicExcludedAPI();
150 for(String str: excludedApiSet){
158 public static boolean isMusicSerializeCompress(){
159 if(musicSerializeCompress==null){
160 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_SERIALIZE_COMPRESS +" fall back to eventual put");
163 return musicSerializeCompress.trim().equalsIgnoreCase("true");
166 public static boolean isAtomicPut(){
168 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_ATOMIC_PUT +" fall back to eventual put");
171 return atomicPut.trim().equalsIgnoreCase("true");
174 public static boolean isAtomicGet(){
176 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_ATOMIC_GET +" fall back to eventual get");
179 return atomicGet.trim().equalsIgnoreCase("true");
182 public static boolean isCached(){
184 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_CACHE +" fall back to non cache");
187 return cached.trim().equalsIgnoreCase("true");
190 public static int convertHoursToMillSec(int hour){
191 return hour* MILLIS_IN_HOUR;
194 public static boolean cleanUp(){
195 Date lastCleanUpDate = MusicCleanUp.getInstance().getLastCleanUpTime();
196 if(lastCleanUpDate==null)
199 int cleanUpDurationMili = convertHoursToMillSec(Integer.valueOf(cleanUpFreq));
200 Date currentTime = new Date();
201 long diffInMillies = Math.abs(currentTime.getTime() - lastCleanUpDate.getTime());
202 if(diffInMillies > cleanUpDurationMili){
203 MusicCleanUp.getInstance().updateLastCleanUpTimeToCurrent();
211 public static boolean isMusicEnable(){
212 if(musicEnable==null)
214 if(musicEnable.equals("true"))