Change default argument path for logs directory
[policy/engine.git] / LogParser / src / main / java / org / onap / xacml / parser / CleanUpSystemLogDB.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * LogParser
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.xacml.parser;
23
24 import java.text.Format;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.TimerTask;
28 import java.sql.Connection;
29 import java.sql.SQLException;
30
31
32 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33
34 public class CleanUpSystemLogDB extends TimerTask{
35
36         private static org.onap.policy.common.logging.flexlogger.Logger logger = FlexLogger.getLogger(CleanUpSystemLogDB.class.getName());
37         Connection localConnect = null;
38         int timeFrame = 5; //default
39         public CleanUpSystemLogDB(Connection dbConnect, int argTimeFrame) {
40                 localConnect = dbConnect;
41                 if(argTimeFrame > 0){
42                         timeFrame = argTimeFrame;
43                 }
44         }
45         String className = this.getClass().getSimpleName();
46
47         @Override
48         public void run() {
49
50         Date date = new Date();
51         Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
52         logger.debug("cleanLogDBTableEntries:Cleanup systemlogdb starts on date:" + formatter.format(date));
53                 try {
54                         cleanLogDBTableEntries(localConnect, timeFrame);
55                 } catch (SQLException e) {
56                         logger.error(e);
57                 }
58
59                 logger.debug(className + " Cleanup systemlogdb done");
60         }
61
62         public static void cleanLogDBTableEntries(Connection dbConnect, int timeFrame) throws SQLException {
63                 
64                 Connection connect = dbConnect;
65         if(dbConnect == null || dbConnect.isClosed()) {
66                 connect = ParseLog.getDbConnection();
67         }               
68                 try ( 
69                           java.sql.PreparedStatement statement  = connect
70                                                 .prepareStatement("DELETE FROM SYSTEMLOGDB WHERE date < DATE_SUB(CURDATE(), INTERVAL ? DAY)");
71                 ){
72
73                         statement.setInt(1, timeFrame); 
74
75                         int records = statement.executeUpdate();
76                         
77                         logger.debug("cleanLogDBTableEntries:deleting Log files ended with " + records + " deleted.");
78                         statement.close();
79                         
80                 } catch (Exception e) {
81                         logger.error("Failed to create SQLContainer for System Log Database", e);
82                 } finally{
83                         connect.close();
84                 }
85         }
86 }