Merge "Sonar Fixes - CadiAuthFilter.java"
[music.git] / src / main / java / org / onap / music / main / CronJobManager.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Modifications Copyright (c) 2019 Samsung
8  * ===================================================================
9  *  Licensed under the Apache License, Version 2.0 (the "License");
10  *  you may not use this file except in compliance with the License.
11  *  You may obtain a copy of the License at
12  * 
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=============================================
22  * ====================================================================
23  */
24
25 package org.onap.music.main;
26
27 import java.time.LocalDateTime;
28 import java.time.format.DateTimeFormatter;
29 import java.util.Iterator;
30 import org.onap.music.authentication.CachingUtil;
31 import org.onap.music.datastore.PreparedQueryObject;
32 import org.onap.music.eelf.logging.EELFLoggerDelegate;
33 import org.onap.music.eelf.logging.format.AppMessages;
34 import org.onap.music.eelf.logging.format.ErrorSeverity;
35 import org.onap.music.eelf.logging.format.ErrorTypes;
36 import org.onap.music.exceptions.MusicLockingException;
37 import org.onap.music.exceptions.MusicServiceException;
38 import org.springframework.scheduling.annotation.Scheduled;
39 import org.springframework.stereotype.Component;
40
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43
44 @Component
45 public class CronJobManager {
46
47     private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CronJobManager.class);
48
49     private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
50
51
52     @Scheduled(cron = "0 0 0 * * ?")
53     public void scheduleTaskWithFixedRate() {
54         logger.info("Executing cronjob to cleanup locks..", dateTimeFormatter.format(LocalDateTime.now()) );
55         deleteLocksFromDB();
56     }
57
58     public void deleteLocksFromDB() {
59         PreparedQueryObject pQuery = new PreparedQueryObject();
60         String consistency = MusicUtil.EVENTUAL;
61         pQuery.appendQueryString("CREATE TABLE IF NOT EXISTS admin.locks ( lock_id text PRIMARY KEY, ctime text)");
62         try {
63             ResultType result = MusicCore.nonKeyRelatedPut(pQuery, consistency);
64             if ( result.equals(ResultType.FAILURE)) {
65                 logger.error(EELFLoggerDelegate.errorLogger,"Error creating Admin.locks table.",AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
66             }
67         } catch (MusicServiceException e1) {
68             logger.error(EELFLoggerDelegate.errorLogger,e1,AppMessages.QUERYERROR,ErrorSeverity.CRITICAL, ErrorTypes.QUERYERROR);
69         }
70
71         pQuery = new PreparedQueryObject();
72         pQuery.appendQueryString(
73                         "select * from admin.locks");
74             try {
75                 ResultSet rs = MusicCore.get(pQuery);
76                 Iterator<Row> it = rs.iterator();
77                 StringBuilder deleteKeys = new StringBuilder();
78                 Boolean expiredKeys = false;
79                 while (it.hasNext()) {
80                     Row row = it.next();
81                     String id = row.getString("lock_id");
82                     long ctime = Long.parseLong(row.getString("ctime"));
83                     if(System.currentTimeMillis() >= ctime + 24 * 60 * 60 * 1000) {
84                         expiredKeys = true;
85                         String new_id = id.substring(1);
86                         try {
87                             MusicCore.deleteLock(new_id);
88                         } catch (MusicLockingException e) {
89                             logger.info(EELFLoggerDelegate.applicationLogger,
90                                      e.getMessage());
91                         }
92                         deleteKeys.append("'").append(id).append("'").append(",");
93                     }
94                 }
95                 if(expiredKeys) {
96                     deleteKeys.deleteCharAt(deleteKeys.length()-1);
97                     CachingUtil.deleteKeysFromDB(deleteKeys.toString());
98                }
99             } catch (MusicServiceException e) {
100                 logger.error(EELFLoggerDelegate.errorLogger,e,AppMessages.CACHEERROR,ErrorSeverity.CRITICAL, ErrorTypes.DATAERROR);
101             }
102     }
103 }