aboutsummaryrefslogtreecommitdiffstats
path: root/LICENSE.txt
blob: bb235ffb2c8ad2f08521a713c7a648e81dc7f8b5 (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
/*
* ============LICENSE_START==========================================
* ===================================================================
* Copyright © 2017 AT&T Intellectual Property. All rights reserved.
* ===================================================================
*
* Unless otherwise specified, all software contained herein is licensed
* under the Apache License, Version 2.0 (the “License”);
* you may not use this software 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.
*
*
*
* Unless otherwise specified, all documentation contained herein is licensed
* under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
* you may not use this documentation except in compliance with the License.
* You may obtain a copy of the License at
*
*             https://creativecommons.org/licenses/by/4.0/
*
* Unless required by applicable law or agreed to in writing, documentation
* 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============================================
*
* ECOMP is a trademark and service mark of AT&T Intellectual Property.
*
*/
r: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*-
 * ============LICENSE_START=======================================================
 * feature-state-management
 * ================================================================================
 * Copyright (C) 2017 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.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
  final static private 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.
  static private boolean createTableNeeded = true;

  synchronized private static void setCreateTableNeeded(boolean b) {
		DbAudit.createTableNeeded = b;
	}
  /**
   * @return the single 'DbAudit' instance
   */
  static DroolsPDPIntegrityMonitor.AuditBase getInstance()
  {
	return(instance);
  }

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

  /**
   * Invoke the audit
   *
   * @param properties properties to be passed to the audit
   */
  @Override
	public void invoke(Properties properties)
  {
	if(logger.isDebugEnabled()){
		logger.debug("Running 'DbAudit.invoke'");
	}
	boolean isActive = true;
	String dbAuditIsActive = StateManagementProperties.getProperty("db.audit.is.active");
	if(logger.isDebugEnabled()){
		logger.debug("DbAudit.invoke: dbAuditIsActive = {}", dbAuditIsActive);
	}
	
	if (dbAuditIsActive != null) {
		try {
			isActive = Boolean.parseBoolean(dbAuditIsActive.trim());
		} catch (NumberFormatException e) {
			logger.warn("DbAudit.invoke: Ignoring invalid property: db.audit.is.active = {}", dbAuditIsActive);
		}
	}
	
	if(!isActive){
		
		logger.info("DbAudit.invoke: exiting because isActive = {}", isActive);
		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";
	if(logger.isDebugEnabled()){
		logger.debug("DbAudit: Creating connection to {}", url);
	}
	try (Connection connection = DriverManager.getConnection(url, user, password))
	  {

		// create audit table, if needed
		if (createTableNeeded)
		  {
			phase = "create table";
			if(logger.isDebugEnabled()){
				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);
			} catch (Exception e) {
				throw e;
			}
		  }

		// insert an entry into the table
		phase = "insert entry";
		String key = UUID.randomUUID().toString();
		try (PreparedStatement statement = connection.prepareStatement
		  ("INSERT INTO Audit (name) VALUES (?)")) {
			statement.setString(1, key);
			statement.executeUpdate();
		} catch (Exception e) {
			throw e;
		}
		
		// fetch the entry from the table
		phase = "fetch entry";
		try (PreparedStatement statement = connection.prepareStatement
		  ("SELECT name FROM Audit WHERE name = ?")) {
			statement.setString(1, key);
			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");
				  }
			} catch (Exception e) {
				throw e;
			}
		}
		// delete entries from table
		phase = "delete entry";
		try (PreparedStatement statement = connection.prepareStatement
		  ("DELETE FROM Audit WHERE name = ?")) {
			statement.setString(1, key);
			statement.executeUpdate();
		} catch (Exception e) {
			throw e;
		}
	}
	catch (Exception e)
	  {
		String message = "DbAudit: Exception during audit, phase = " + phase;
		logger.error(message, e);
		setResponse(message);
	  }
  }

}