Commit includes ControlLoopPolicy API and bugfixes
[policy/engine.git] / packages / base / src / files / bin / policy.sh
1 ###
2 # ============LICENSE_START=======================================================
3 # ECOMP Policy Engine
4 # ================================================================================
5 # Copyright (C) 2017 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
10
11 #      http://www.apache.org/licenses/LICENSE-2.0
12
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=========================================================
19 ###
20
21 #!/bin/bash
22
23 function usage() {
24         echo -n "syntax: $(basename $0) "
25         echo -n "[--debug] "
26         echo    "status|start|stop"
27 }
28
29 function check_r_file() {       
30         if [[ $DEBUG == y ]]; then
31                 echo "-- ${FUNCNAME[0]} --"
32                 set -x
33         fi
34
35         FILE=$1
36         if [[ ! -f ${FILE} || ! -r ${FILE} ]]; then
37         return 1
38         fi
39
40         return 0
41 }
42
43 function check_x_file() {       
44         if [[ $DEBUG == y ]]; then
45                 echo "-- ${FUNCNAME[0]} --"
46                 set -x
47         fi
48
49         FILE=$1
50         if [[ ! -f ${FILE} || ! -x ${FILE} ]]; then
51         return 1
52         fi
53
54         return 0
55 }
56
57 function component_status() {
58         COMPONENT=$1
59         output=$("${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status)
60         if [[ $? == 0 ]]; then 
61                 echo "  ${COMPONENT}: UP: ${output}"
62         else
63                 echo "  ${COMPONENT}: DOWN"
64         fi
65 }
66
67 function component_start() {
68         COMPONENT=$1
69         "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status > /dev/null 2>&1
70         if [[ $? == 0 ]]; then 
71                 echo "  ${COMPONENT}: UP: already running .."
72         else
73                 "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" start > /dev/null 2>&1
74                 echo "  ${COMPONENT}: STARTING .."
75         fi
76 }
77
78 function component_stop() {
79         COMPONENT=$1
80         "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" status > /dev/null 2>&1
81         if [[ $? != 0 ]]; then 
82                 echo "  ${COMPONENT}: DOWN: already stopped .."
83         else
84                 "${POLICY_HOME}"/etc/init.d/"${COMPONENT}" stop > /dev/null 2>&1
85                 echo "  ${COMPONENT}: STOPPING .."
86         fi
87 }
88
89 function policy_status() {
90         if [[ $DEBUG == y ]]; then
91                 echo "-- ${FUNCNAME[0]} --"
92                 set -x
93         fi
94         
95         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
96                 component_status pap
97         fi
98         
99         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
100                 component_status pdp
101         fi
102
103         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
104                 component_status configs
105         fi
106         
107         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
108                 component_status console
109         fi
110         
111         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
112                 component_status brmsgw
113         fi
114         
115         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
116                 component_status paplp
117         fi
118         
119         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
120                 component_status pdplp
121         fi
122
123         NUM_CRONS=$(crontab -l 2> /dev/null | wc -l)
124         echo "  ${NUM_CRONS} cron jobs installed."
125         
126 }
127
128 function policy_start() {
129         if [[ $DEBUG == y ]]; then
130                 echo "-- ${FUNCNAME[0]} --"
131                 set -x
132         fi
133         
134         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
135                 component_start pap
136         fi
137         
138         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
139                 component_start pdp
140         fi
141
142         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
143                 component_start configs
144         fi
145         
146         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
147                 component_start console
148         fi
149         
150         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
151                 component_start brmsgw
152         fi
153         
154         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
155                 component_start paplp
156         fi
157         
158         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
159                 component_start pdplp
160         fi
161         
162         cat "${POLICY_HOME}"/etc/cron.d/*.cron | crontab
163 }
164
165 function policy_stop() {
166         if [[ $DEBUG == y ]]; then
167                 echo "-- ${FUNCNAME[0]} --"
168                 set -x
169         fi
170         
171         pkill -f "/bin/bash ${POLICY_HOME}/bin/monitor.sh"
172         crontab -r > /dev/null 2>&1
173         sleep 2
174
175         if check_x_file "${POLICY_HOME}/etc/init.d/paplp"; then
176                 component_stop paplp
177         fi
178         
179         if check_x_file "${POLICY_HOME}/etc/init.d/pdplp"; then
180                 component_stop pdplp
181         fi
182         
183         if check_x_file "${POLICY_HOME}/etc/init.d/brmsgw"; then
184                 component_stop brmsgw
185         fi
186         
187         if check_x_file "${POLICY_HOME}/etc/init.d/console"; then
188                 component_stop console
189         fi
190         
191         if check_x_file "${POLICY_HOME}/etc/init.d/configs"; then
192                 component_stop configs
193         fi
194         
195         if check_x_file "${POLICY_HOME}/etc/init.d/pdp"; then
196                 component_stop pdp
197         fi
198         
199         if check_x_file "${POLICY_HOME}/etc/init.d/pap"; then
200                 component_stop pap
201         fi
202         
203 }
204
205 #########################################################################
206 ##
207 ## script execution body
208 ##
209 #########################################################################
210
211 DEBUG=n
212 OPERATION=none
213
214 until [[ -z "$1" ]]; do
215         case $1 in
216                 -d|--debug|debug)       DEBUG=y
217                                                 set -x
218                                                 ;;
219                 -i|--status|status)             OPERATION=status
220                                                                         ;;
221                 -s|--start|start)                       OPERATION=start
222                                                                         ;;
223                 -h|--stop|stop|--halt|halt)     OPERATION=halt
224                                                                         ;;
225                 *)                                                      usage
226                                                                         exit 1
227                                                                         ;;
228         esac
229         shift
230 done
231
232 # operation validation
233 case $OPERATION in
234         status) ;;
235         start)  ;;
236         halt)   ;;
237         *)              echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}";
238                         usage
239                         exit 1
240                         ;;
241 esac
242
243 if [[ -z ${POLICY_HOME} ]]; then
244         echo "error: POLICY_HOME is unset."
245         exit 1
246 fi
247
248 # operation validation
249 case $OPERATION in
250         status) 
251                 policy_status
252                 ;;
253         start)  
254                 policy_start
255                 ;;
256         halt)   
257                 policy_stop
258                 ;;
259         *)              echo "invalid operation \(${OPERATION}\): must be in {status|start|stop}";
260                         usage
261                         exit 1
262                         ;;
263 esac