1841d3c57f3a750393b3e60e105d0ba408a728a1
[policy/engine.git] / packages / base / src / files / install / mysql / bin / cleanup_policy.sh
1 #!/bin/bash
2 ###
3 # ============LICENSE_START=======================================================
4 # ONAP Policy Engine
5 # ================================================================================
6 # Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
7 # ================================================================================
8 # Licensed under the Apache License, Version 2.0 (the "License");
9 # you may not use this file except in compliance with the License.
10 # You may obtain a copy of the License at
11
12 #      http://www.apache.org/licenses/LICENSE-2.0
13
14 # Unless required by applicable law or agreed to in writing, software
15 # distributed under the License is distributed on an "AS IS" BASIS,
16 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 # See the License for the specific language governing permissions and
18 # limitations under the License.
19 # ============LICENSE_END=========================================================
20 ###
21
22 # cleanup_policy.sh: Run this script to delete policy record marked as 'deleted'
23
24 # Usage  : cleanup_policy.sh db_user     db_user_password retention_period
25 # Example: cleanup_policy.sh policy_user password         90
26 #
27 #
28 #
29
30 . $HOME/.profile
31
32 DB_USER=""
33 DB_PASSWORD=""
34 RETENTION_PERIOD=""
35 DATE=`date +"%Y%m%d"`
36 DATETIME=`date +"%Y%m%d%H%M%S"`
37 LOG=""
38 ERR=""
39
40 function cleanup_deleted_policy
41 {
42   # 1
43   echo "1- cleanup_deleted_policy [policyGroupEntity] ... `date`" | tee -a $LOG
44   ${MYSQL} -e "delete from xacml.policyGroupEntity where policyId in ( select policyId from xacml.policyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)) and groupKey in ( select groupKey from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG
45   echo "--" | tee -a $LOG
46
47   # 2
48   echo "2- cleanup_deleted_policy [pdpEntity] ... `date`" | tee -a $LOG
49   ${MYSQL} -e "delete from xacml.pdpEntity where groupKey in ( select groupKey from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG
50   echo "--" | tee -a $LOG
51
52   # 3
53   echo "3- cleanup_deleted_policy [groupEntity] ... `date`" | tee -a $LOG
54   ${MYSQL} -e "delete from xacml.groupEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG
55   echo "--" | tee -a $LOG
56
57   # 4
58   echo "4- cleanup_deleted_policy [policyEntity] ... `date`" | tee -a $LOG
59   ${MYSQL} -e "delete from xacml.policyEntity where configurationDataId in ( select configurationDataId from xacml.configurationDataEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)) and actionBodyId in ( select actionBodyId from xacml.actionBodyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY)); " 2>&1 | tee -a $LOG
60   echo "--" | tee -a $LOG
61
62   # 5
63   echo "5- cleanup_deleted_policy [configurationDataEntity] ... `date`" | tee -a $LOG
64   ${MYSQL} -e "delete from xacml.configurationDataEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG
65   echo "--" | tee -a $LOG
66
67   # 6
68   echo "6- cleanup_deleted_policy [actionBodyEntity] ... `date`" | tee -a $LOG
69   ${MYSQL} mysql --verbose -u${DB_USER} -p${DB_PASSWORD} -e "delete from xacml.actionBodyEntity where deleted = true and modified_date < (current_date - INTERVAL $RETENTION_PERIOD DAY); " 2>&1 | tee -a $LOG
70   echo "--" | tee -a $LOG
71 }
72
73 # MAIN
74 LOG=$POLICY_HOME/logs/cleanup_policy_$DATE.log
75 ERR=$POLICY_HOME/logs/cleanup_policy_$DATE.err
76 echo "cleanup_policy.sh started ... `date`" | tee -a $LOG
77 if [ $# -eq 3 ]; then 
78   DB_USER="${1}"
79   DB_PASSWORD="${2}"
80   RETENTION_PERIOD="${3}"
81   echo "DB_USER: $DB_USER" | tee -a $LOG
82
83   typeset -r MYSQL="mysql -u${DB_USER} -p${DB_PASSWORD} --verbose ";
84
85   cleanup_deleted_policy
86
87   echo "cleanup_policy.sh completed ... `date`" | tee -a $LOG
88 else
89   echo "Usage  : cleanup_policy.sh db_user_id  db_user_password retention_period" 
90   echo "Example: cleanup_policy.sh policy_user password         90" 
91 fi
92