Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / packages / base / src / files / install / elk / bin / elk.sh
1 #!/bin/bash
2
3 ELK_MAP_SUCCESS_RE="\"acknowledged\": *true"
4
5 function usage() {
6         echo -n "syntax: $(basename $0) "
7         echo -n "[--debug] "
8         echo -n "([--audit] |"  
9 }
10
11 function log() {
12         echo "$(date +"%Y-%m-%d_%H-%M-%S") $1" >> ${POLICY_HOME}/logs/elk.log
13         echo "$1"
14 }
15
16 function delete_index() {
17         if [[ $DEBUG == y ]]; then
18                 echo "-- ${FUNCNAME[0]} $@ --"
19                 set -x
20         fi
21         
22         COMMAND="curl --silent -X DELETE http://localhost:9200/policy"
23         OUTPUT="$(${COMMAND} 2>&1)"
24         RC=$?
25         
26         log "${RC}: ${COMMAND}"
27         log "${OUTPUT}"
28         
29         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ ${ELK_MAP_SUCCESS_RE} ]]; then
30                 log "WARNING: curl: delete /policy: ${RC}"
31                 return 1
32         fi
33         
34         log "OK: curl: delete /policy: ${OUTPUT}"
35         return 0
36 }
37
38 function create_index() {
39         if [[ $DEBUG == y ]]; then
40                 echo "-- ${FUNCNAME[0]} $@ --"
41                 set -x
42         fi
43         
44         COMMAND="curl --silent -X PUT http://localhost:9200/policy"
45         OUTPUT="$(${COMMAND} 2>&1)"
46         RC=$?
47         
48         log "${RC}: ${COMMAND}"
49         log "${OUTPUT}"
50         
51         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ ${ELK_MAP_SUCCESS_RE} ]]; then
52                 log "ERROR: curl: put /policy: ${RC}"
53                 return 1
54         fi
55                 
56         log "OK: curl: put /policy."
57         return 0
58 }
59
60 function check_elk_status() {
61         if [[ $DEBUG == y ]]; then
62                 echo "-- ${FUNCNAME[0]} $@ --"
63                 set -x
64         fi
65                 
66         ${POLICY_HOME}/etc/init.d/elk status
67         if [[ $? != 0 ]]; then
68                 log "ERROR: elk is down.   Aborting .."
69                 exit 1
70         fi
71 }
72
73 function check_elk_policy_index() {
74         if [[ $DEBUG == y ]]; then
75                 echo "-- ${FUNCNAME[0]} $@ --"
76                 set -x
77         fi
78         
79         COMMAND="curl --silent -X GET http://localhost:9200/policy"
80         OUTPUT="$(${COMMAND} 2>&1)"
81         RC=$?
82         
83         log "${RC}: ${COMMAND}"
84         
85         if [[ ${RC} != 0 ]] || [[ ! ${OUTPUT} =~ policy ]]; then
86                 log "ERROR: curl: get /policy: ${RC}"
87                 return 1
88         fi
89                 
90         log "OK: curl: get /policy."
91         return 0                
92 }
93
94 #The Script will update the policy data on querying from database as a bulk to Elastic Database
95 function audit() {
96         if [[ $DEBUG == y ]]; then
97                 echo "-- ${FUNCNAME[0]} $@ --"
98                 set -x
99         fi
100         
101         check_elk_status
102         
103         if ! check_elk_policy_index; then
104                 echo "policy index does not exist. So, Policy Index is created."
105                 if ! create_index; then
106                         echo "abort: policy index creation failed."
107                         exit 1
108                 fi
109         fi
110         
111         $JAVA_HOME/bin/java -cp $POLICY_HOME/servers/pap/webapps/pap/WEB-INF/lib/ECOMP-PAP-REST-*.jar:$POLICY_HOME/servers/pap/webapps/pap/WEB-INF/lib/*: -DPROPERTY_FILE=$POLICY_HOME/servers/pap/bin/policyelk.properties org.openecomp.policy.pap.xacml.rest.elk.client.ElasticSearchPolicyUpdate
112 }
113
114 #########################################################################
115 ##
116 ## script execution body
117 ##
118 #########################################################################
119
120 DEBUG=n
121 OPERATION=none
122
123 until [[ -z "$1" ]]; do
124         case $1 in
125                 -d|--debug|debug)       DEBUG=y
126                                                         set -x
127                                                         ;;
128                 -a|--audit|audit)       OPERATION=audit
129                                                         ;;                                                                                                                                      
130                 *)      usage
131                         exit 1
132                         ;;
133         esac
134         shift
135 done
136
137 # operation validation
138 case $OPERATION in
139         audit)  ;;
140         *)              echo "invalid operation (${OPERATION}).";
141                         usage
142                         exit 1
143                         ;;
144 esac
145
146 if [[ -z ${POLICY_HOME} ]]; then
147         echo "error: POLICY_HOME is unset."
148         exit 1
149 fi
150
151 log "**** $OPERATION ****"
152
153 if pidof -o %PPID -x $(basename $0) > /dev/null 2>&1; then
154         echo "WARNING: an $(basename $0) process is already running.  Exiting."
155         exit 1
156 fi
157
158 case $OPERATION in
159         audit)  
160                 audit
161                 ;;              
162         *)      echo "invalid operation (${OPERATION}).";
163                 usage
164                 exit 1
165                 ;;
166 esac