Remove checkstyle warnings in policy/engine
[policy/engine.git] / LogParser / src / main / java / org / onap / xacml / parser / CleanUpSystemLogDb.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * LogParser
4  * ================================================================================
5  * Copyright (C) 2017-2018 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.sql.Connection;
25 import java.sql.SQLException;
26 import java.text.Format;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
29 import java.util.TimerTask;
30
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
32
33 public class CleanUpSystemLogDb extends TimerTask {
34
35     private static org.onap.policy.common.logging.flexlogger.Logger logger =
36             FlexLogger.getLogger(CleanUpSystemLogDb.class.getName());
37     Connection localConnect = null;
38     int timeFrame = 5; // default
39
40     /**
41      * Set time frame for database cleanup.
42      *
43      * @param dbConnect the database connection object
44      * @param argTimeFrame the time frame
45      */
46     public CleanUpSystemLogDb(final Connection dbConnect, final int argTimeFrame) {
47         localConnect = dbConnect;
48         if (argTimeFrame > 0) {
49             timeFrame = argTimeFrame;
50         }
51     }
52
53     String className = this.getClass().getSimpleName();
54
55     @Override
56     public void run() {
57
58         final Date date = new Date();
59         final Format formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
60         logger.debug("cleanLogDBTableEntries:Cleanup systemlogdb starts on date:" + formatter.format(date));
61         try {
62             cleanLogDbTableEntries(localConnect, timeFrame);
63         } catch (final SQLException e) {
64             logger.error(e);
65         }
66
67         logger.debug(className + " Cleanup systemlogdb done");
68     }
69
70     /**
71      * Clean system log database table entries based on input time frame.
72      *
73      * @param dbConnect the database connection object
74      * @param timeFrame the time frame
75      * @throws SQLException if an error occurs
76      */
77     public static void cleanLogDbTableEntries(final Connection dbConnect, final int timeFrame) throws SQLException {
78
79         Connection connect = dbConnect;
80         if (dbConnect == null || dbConnect.isClosed()) {
81             connect = ParseLog.getDbConnection();
82         }
83         try (java.sql.PreparedStatement statement =
84                 connect.prepareStatement("DELETE FROM SYSTEMLOGDB WHERE date < DATE_SUB(CURDATE(), INTERVAL ? DAY)");) {
85
86             statement.setInt(1, timeFrame);
87
88             final int records = statement.executeUpdate();
89
90             logger.debug("cleanLogDBTableEntries:deleting Log files ended with " + records + " deleted.");
91             statement.close();
92
93         } catch (final Exception e) {
94             logger.error("Failed to create SQLContainer for System Log Database", e);
95         } finally {
96             connect.close();
97         }
98     }
99 }