aboutsummaryrefslogtreecommitdiffstats
path: root/feature-state-management/src/main/java/org/onap/policy/drools/statemanagement/DbAudit.java
blob: 2dc751b17db0aa303b4e57ad72961c128f52880f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*-
 * ============LICENSE_START=======================================================
 * feature-state-management
 * ================================================================================
 * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.statemanagement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** This class audits the database.
 */
public class DbAudit extends DroolsPdpIntegrityMonitor.AuditBase {
    // get an instance of logger
    private static Logger logger = LoggerFactory.getLogger(DbAudit.class);
    // single global instance of this audit object
    private static final DbAudit instance = new DbAudit();

    // This indicates if 'CREATE TABLE IF NOT EXISTS Audit ...' should be
    // invoked -- doing this avoids the need to create the table in advance.
    private static boolean createTableNeeded = true;

    private static boolean isJunit = false;

    /** Constructor - set the name to 'Database'. */
    private DbAudit() {
        super("Database");
    }

    private static synchronized void setCreateTableNeeded(boolean isNeeded) {
        DbAudit.createTableNeeded = isNeeded;
    }

    public static synchronized void setIsJunit(boolean isJUnit) {
        DbAudit.isJunit = isJUnit;
    }

    public static boolean isJunit() {
        return DbAudit.isJunit;
    }

    /**
     * Get the instance.
     *
     * @return the single 'DbAudit' instance. */
    public static DroolsPdpIntegrityMonitor.AuditBase getInstance() {
        return instance;
    }

    /**
     * Invoke the audit.
     *
     * @param properties properties to be passed to the audit
     */
    @Override
    public void invoke(Properties properties) {
        logger.debug("Running 'DbAudit.invoke'");
        boolean doCreate = createTableNeeded && !isJunit;

        if (!isActive()) {
            logger.info("DbAudit.invoke: exiting because isActive = false");
            return;
        }

        // fetch DB properties from properties file -- they are already known
        // to exist, because they were verified by the 'IntegrityMonitor'
        // constructor
        String url = properties.getProperty(StateManagementProperties.DB_URL);
        String user = properties.getProperty(StateManagementProperties.DB_USER);
        String password = properties.getProperty(StateManagementProperties.DB_PWD);

        // operation phase currently running -- used to construct an error
        // message, if needed
        String phase = null;

        // create connection to DB
        phase = "creating connection";
        logger.debug("DbAudit: Creating connection to {}", url);
        try (Connection connection = DriverManager.getConnection(url, user, password)) {

            // create audit table, if needed
            if (doCreate) {
                phase = "create table";
                createTable(connection);
            }

            // insert an entry into the table
            phase = "insert entry";
            String key = UUID.randomUUID().toString();
            insertEntry(connection, key);

            phase = "fetch entry";
            findEntry(connection, key);

            phase = "delete entry";
            deleteEntry(connection, key);
        } catch (Exception e) {
            String message = "DbAudit: Exception during audit, phase = " + phase;
            logger.error(message, e);
            setResponse(message);
        }
    }

    /**
     * Determines if the DbAudit is active, based on properties. Defaults to {@code true}, if not
     * found in the properties.
     *
     * @return {@code true} if DbAudit is active, {@code false} otherwise
     */
    private boolean isActive() {
        String dbAuditIsActive = StateManagementProperties.getProperty("db.audit.is.active");
        logger.debug("DbAudit.invoke: dbAuditIsActive = {}", dbAuditIsActive);

        if (dbAuditIsActive != null) {
            try {
                return Boolean.parseBoolean(dbAuditIsActive.trim());
            } catch (NumberFormatException e) {
                logger.warn(
                        "DbAudit.invoke: Ignoring invalid property: db.audit.is.active = {}", dbAuditIsActive);
            }
        }

        return true;
    }

    /**
     * Creates the table.
     *
     * @param connection connection
     * @throws SQLException exception
     */
    private void createTable(Connection connection) throws SQLException {
        logger.info("DbAudit: Creating 'Audit' table, if needed");
        try (PreparedStatement statement =
                connection.prepareStatement(
                        "CREATE TABLE IF NOT EXISTS Audit (\n"
                                + " name varchar(64) DEFAULT NULL,\n"
                                + " UNIQUE KEY name (name)\n"
                                + ") DEFAULT CHARSET=latin1;")) {
            statement.execute();
            DbAudit.setCreateTableNeeded(false);
        }
    }

    /**
     * Inserts an entry.
     *
     * @param connection connection
     * @param key key
     * @throws SQLException exception
     */
    private void insertEntry(Connection connection, String key) throws SQLException {
        try (PreparedStatement statement =
                connection.prepareStatement("INSERT INTO Audit (name) VALUES (?)")) {
            statement.setString(1, key);
            statement.executeUpdate();
        }
    }

    /**
     * Finds an entry.
     *
     * @param connection connection
     * @param key key
     * @throws SQLException exception
     */
    private void findEntry(Connection connection, String key) throws SQLException {
        try (PreparedStatement statement =
                connection.prepareStatement("SELECT name FROM Audit WHERE name = ?")) {
            statement.setString(1, key);
            getEntry(statement, key);
        }
    }

    /**
     * Executes the query to determine if the entry exists. Sets the response if it fails.
     *
     * @param statement statement
     * @param key key
     * @throws SQLException exception
     */
    private void getEntry(PreparedStatement statement, String key) throws SQLException {
        try (ResultSet rs = statement.executeQuery()) {
            if (rs.first()) {
                // found entry
                if (logger.isDebugEnabled()) {
                    logger.debug("DbAudit: Found key {}", rs.getString(1));
                }
            } else {
                logger.error("DbAudit: can't find newly-created entry with key {}", key);
                setResponse("Can't find newly-created entry");
            }
        }
    }

    /**
     * Deletes an entry.
     *
     * @param connection connection
     * @param key key
     * @throws SQLException exception
     */
    private void deleteEntry(Connection connection, String key) throws SQLException {
        try (PreparedStatement statement =
                connection.prepareStatement("DELETE FROM Audit WHERE name = ?")) {
            statement.setString(1, key);
            statement.executeUpdate();
        }
    }
}