2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.xacml.parser;
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;
31 import org.onap.policy.common.logging.flexlogger.FlexLogger;
33 public class CleanUpSystemLogDb extends TimerTask {
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
41 * Set time frame for database cleanup.
43 * @param dbConnect the database connection object
44 * @param argTimeFrame the time frame
46 public CleanUpSystemLogDb(final Connection dbConnect, final int argTimeFrame) {
47 localConnect = dbConnect;
48 if (argTimeFrame > 0) {
49 timeFrame = argTimeFrame;
53 String className = this.getClass().getSimpleName();
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));
62 cleanLogDbTableEntries(localConnect, timeFrame);
63 } catch (final SQLException e) {
67 logger.debug(className + " Cleanup systemlogdb done");
71 * Clean system log database table entries based on input time frame.
73 * @param dbConnect the database connection object
74 * @param timeFrame the time frame
75 * @throws SQLException if an error occurs
77 public static void cleanLogDbTableEntries(final Connection dbConnect, final int timeFrame) throws SQLException {
79 Connection connect = dbConnect;
80 if (dbConnect == null || dbConnect.isClosed()) {
81 connect = ParseLog.getDbConnection();
83 try (java.sql.PreparedStatement statement =
84 connect.prepareStatement("DELETE FROM SYSTEMLOGDB WHERE date < DATE_SUB(CURDATE(), INTERVAL ? DAY)");) {
86 statement.setInt(1, timeFrame);
88 final int records = statement.executeUpdate();
90 logger.debug("cleanLogDBTableEntries:deleting Log files ended with " + records + " deleted.");
93 } catch (final Exception e) {
94 logger.error("Failed to create SQLContainer for System Log Database", e);