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 String message="Failed to serialize ";
119 logger.error(EELFLoggerDelegate.errorLogger, message, e);
121 return ByteBuffer.wrap(bo.toByteArray());
124 public static MusicSession parseMetaData (Row row) throws Exception{
125 logger.debug(EELFLoggerDelegate.debugLogger, "parseMetaData: start");
129 String sessionId = row.getString("primary_id");
130 MusicSession musicSession = new MusicSession(sessionId);
131 musicSession.setCreationTime(Instant.parse(row.getString("creation_time")));
132 musicSession.setLastAccessedTime(Instant.parse(row.getString("last_access_time")));
133 musicSession.setMaxInactiveInterval(Duration.parse(row.getString("max_inactive_interval")));
137 public static Set<String> getMusicExcludedAPI(){
138 Set<String> excludedApiSet = new HashSet<>();
139 String musicExcludedApi = MusicProperties.getProperty(MusicProperties.MUSIC_EXCLUDE_API);
140 String[] musicExcludedApiArray = musicExcludedApi.split(",");
141 if(musicExcludedApiArray.length>0){
142 for(String str : musicExcludedApiArray){
143 excludedApiSet.add(str.trim());
146 return excludedApiSet;
149 public static boolean isExcludedApi(String api){
150 Set<String> excludedApiSet = getMusicExcludedAPI();
151 for(String str: excludedApiSet){
159 public static boolean isMusicSerializeCompress(){
160 if(musicSerializeCompress==null){
161 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_SERIALIZE_COMPRESS +" fall back to eventual put");
164 return musicSerializeCompress.trim().equalsIgnoreCase("true");
167 public static boolean isAtomicPut(){
169 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_ATOMIC_PUT +" fall back to eventual put");
172 return atomicPut.trim().equalsIgnoreCase("true");
175 public static boolean isAtomicGet(){
177 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_ATOMIC_GET +" fall back to eventual get");
180 return atomicGet.trim().equalsIgnoreCase("true");
183 public static boolean isCached(){
185 logger.error(EELFLoggerDelegate.errorLogger, "Failed to read property file " + MusicProperties.MUSIC_CACHE +" fall back to non cache");
188 return cached.trim().equalsIgnoreCase("true");
191 public static int convertHoursToMillSec(int hour){
192 return hour* MILLIS_IN_HOUR;
195 public static boolean cleanUp(){
196 Date lastCleanUpDate = MusicCleanUp.getInstance().getLastCleanUpTime();
197 if(lastCleanUpDate==null)
200 int cleanUpDurationMili = convertHoursToMillSec(Integer.valueOf(cleanUpFreq));
201 Date currentTime = new Date();
202 long diffInMillies = Math.abs(currentTime.getTime() - lastCleanUpDate.getTime());
203 if(diffInMillies > cleanUpDurationMili){
204 MusicCleanUp.getInstance().updateLastCleanUpTimeToCurrent();
212 public static boolean isMusicEnable(){
213 if(musicEnable==null)
215 if(musicEnable.equals("true"))