Get and kill ROP_file_creator.sh childs as well
[integration.git] / test / mocks / mass-pnf-sim / pnf-sim-lightweight / simulator.sh
1 #!/usr/bin/env bash
2
3 set -euo pipefail
4
5 COMPOSE_FILE_NAME=docker-compose.yml
6 NETOPEER_CONTAINER_NAME=netopeer
7 SIMULATOR_CONTAINER_NAME=pnf-simulator
8 SIMULATOR_PORT=5000
9
10 SIMULATOR_BASE=http://localhost:$SIMULATOR_PORT/simulator/
11 SIMULATOR_START_URL=$SIMULATOR_BASE/start
12 SIMULATOR_STOP_URL=$SIMULATOR_BASE/stop
13 SIMULATOR_STATUS_URL=$SIMULATOR_BASE/status
14
15 RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
16
17 function main(){
18
19     COMMAND=${1:-"help"}
20
21     case $COMMAND in
22         "compose")
23             compose $2 $3 $4 $5 $6 $7 $8 $9 "${10}" "${11}" "${12}" "${13}" "${14}" ;;
24              #IPGW, #IPSUBNET, #I, #URLVES, #IPPNFSIM, #IPFILESERVER, #TYPEFILESERVER, #PORTSFTP, #PORTFTPS, #IPFTPS, #IPSFTP, #FTPS_PASV_MIN, #FTPS_PAST_MAX
25         "build")
26             build_image;;
27         "start")
28             start $COMPOSE_FILE_NAME;;
29         "stop")
30             if [[ -z ${2+x} ]]
31             then
32                echo "Error: action 'stop' requires the instance identifier"
33                exit
34             fi
35             stop $2;;
36         "run-simulator")
37             run_simulator;;
38         "trigger-simulator")
39             trigger_simulator;;
40         "stop-simulator")
41             stop_simulator;;
42         "status")
43              get_status;;
44         "clear-logs")
45              clear_logs;;
46         *)
47             print_help;;
48     esac
49 }
50
51
52 function get_pnfsim_ip() {
53
54         export IPPNFSIM=$(cat ./config/config.yml | grep ippnfsim | awk -F'[ ]' '{print $2}')
55         echo "PNF-Sim IP: " $IPPNFSIM
56         
57         export SIMULATOR_BASE=http://$IPPNFSIM:$SIMULATOR_PORT/simulator/
58         export SIMULATOR_START_URL=$SIMULATOR_BASE/start
59         export SIMULATOR_STOP_URL=$SIMULATOR_BASE/stop
60         export SIMULATOR_STATUS_URL=$SIMULATOR_BASE/status
61 }
62
63 function compose(){
64         #creating custom docker-compose based on IP arguments
65         #creting config.json by injecting the same IP
66         
67         export IPGW=$1
68         export IPSUBNET=$2
69         export I=$3
70         export URLVES=$4
71         export IPPNFSIM=$5
72         export IPFILESERVER=$6
73         export TYPEFILESERVER=$7
74         export PORTSFTP=$8
75         export PORTFTPS=$9
76         export IPFTPS=${10}
77         export IPSFTP=${11}
78         export FTPS_PASV_MIN=${12}
79         export FTPS_PASV_MAX=${13}
80         LOCALTIME=$(ls -l /etc/localtime)
81         export TIMEZONE=${LOCALTIME//*zoneinfo\/}
82
83         #will insert $I to distinguish containers, networks properly
84         #docker compose cannot substitute these, as they are keys, not values.
85         envsubst < docker-compose-template.yml > docker-compose-temporary.yml
86         #variable substitution
87         docker-compose -f docker-compose-temporary.yml config > docker-compose.yml
88         rm docker-compose-temporary.yml
89
90         ./ROP_file_creator.sh $I &
91
92         write_config $URLVES $IPFILESERVER $TYPEFILESERVER $PORTSFTP $PORTFTPS $IPPNFSIM
93
94         cd config
95     envsubst < vsftpd_ssl-TEMPLATE.conf > vsftpd_ssl.conf
96         cd -
97
98         set_vsftpd_file_owner
99
100 }
101
102 function build_image(){
103     if [ -f pom.xml ]; then
104         mvn clean package docker:build -Dcheckstyle.skip
105     else
106         echo "pom.xml file not found"
107         exit 1
108     fi
109 }
110
111 function set_vsftpd_file_owner() {
112     sudo chown root ./config/vsftpd_ssl.conf
113 }
114
115
116 function write_config(){
117         #building a YML file for usage in Java
118         echo "urlves: $1" > config/config.yml
119         echo "urlsftp: sftp://onap:pano@$2:$4" >> config/config.yml
120         echo "urlftps: ftps://onap:pano@$2:$5" >> config/config.yml
121         echo "ippnfsim: $6" >> config/config.yml
122         echo "typefileserver: $3" >> config/config.yml
123 }
124
125 function start(){
126
127         get_pnfsim_ip
128     if [[ $(running_containers) ]]; then
129         echo "Simulator containers are already up"
130     else
131         echo "Starting simulator containers using netconf model specified in config/netconf.env"
132         set_vsftpd_file_owner
133         archive_logs
134         docker-compose -f $1 up -d
135         RUNNING_COMPOSE_CONFIG=$1
136     fi
137 }
138
139 function running_containers(){
140    docker-compose -f $COMPOSE_FILE_NAME ps -q
141 }
142
143 function stop(){
144     get_pnfsim_ip
145
146     set +e # override global script setting
147     declare -a pids_to_kill
148     # get ROP_file_creator.sh instance pid
149     pids_to_kill[0]=$(pgrep -f "ROP_file_creator.sh ${1}$")
150     if [[ ! -z ${pids_to_kill[0]} ]];
151     then
152         # get ROP_file_creator.sh childs pids
153         pids_to_kill=(${pids_to_kill[@]} $(pgrep -P ${pids_to_kill[0]}))
154         kill ${pids_to_kill[@]}
155     else
156         echo "ROP_file_creator.sh already not running"
157     fi
158
159     if [[ $(running_containers) ]]; then
160         docker-compose -f $RUNNING_COMPOSE_CONFIG down
161         docker-compose -f $RUNNING_COMPOSE_CONFIG rm
162     else
163         echo "Simulator containers are already down"
164     fi
165 }
166
167 function trigger_simulator(){
168 get_pnfsim_ip
169 cat << EndOfMessage
170 Simulator response:
171 $(curl -s -X POST -H "Content-Type: application/json" -H "X-ONAP-RequestID: 123" -H "X-InvocationID: 456" -d @config/config.json $SIMULATOR_START_URL)
172 EndOfMessage
173 }
174
175 function run_simulator(){
176 get_pnfsim_ip
177 cat << EndOfMessage
178 Simulator response:
179 $(curl -s -X POST -H "Content-Type: application/json" -H "X-ONAP-RequestID: 123" -H "X-InvocationID: 456" -d @config/$CONFIG_JSON $SIMULATOR_START_URL)
180 EndOfMessage
181 }
182
183 function stop_simulator(){
184 get_pnfsim_ip
185 cat << EndOfMessage
186 Simulator response:
187 $(curl -s -X POST $SIMULATOR_STOP_URL)
188 EndOfMessage
189 }
190
191 function get_status(){
192         get_pnfsim_ip
193     if [[ $(running_containers) ]]; then
194         print_status
195     else
196         echo "Simulator containers are down"
197     fi
198 }
199
200 function print_status(){
201 get_pnfsim_ip
202 cat << EndOfMessage
203 $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
204
205 Simulator response:
206 $(curl -s -X GET $SIMULATOR_STATUS_URL)
207 EndOfMessage
208 }
209
210 function print_help(){
211 cat << EndOfMessage
212 Available options:
213 build - locally builds simulator image from existing code
214 start - starts simulator and netopeer2 containers using remote simulator image and specified model name
215 compose - customize the docker-compose and configuration based on arguments
216 trigger-simulator - start monitoring the ROP files and report periodically
217 run-simulator - starts sending PNF registration messages with parameters specified in config.json
218 stop-simulator - stop sending PNF registration messages
219 stop - stops both containers
220 status - prints simulator status
221 clear-logs - deletes log folder
222
223 Starting simulation:
224 - Setup the instance of this simulator by:
225   - ./simulator.sh compose IPGW IPSUBNET I URLVES IPPNFSIM IPFILESERVER TYPEFILESERVER PORTSFTP PORTFTPS IPFTPS IPSFTP
226         where IPGW and IPSUBNET will be used for docker network
227         where I is the integer suffix to differentiate instances
228         where URLVES is the URL of the VES collector
229         where IPPNFSIM, IPFILESERVER, IPFTPS, IPSFTP are the IP addresses for containers
230     where TYPEFILESERVER is the type of fileserver, i.e., FTPS or SFTP
231         where PORTSFTP, PORTFTPS are the SFTP and FTPS ports
232         e.g. ./simulator.sh compose 10.11.0.65 10.11.0.64 3 http://10.11.0.69:10000/eventListener/v7 10.11.0.2 10.11.0.66 ftps 2001 2002 10.11.0.67 10.11.0.68
233
234 - Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
235 - To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json    {TODO, might not be needed}
236
237 To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
238 If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
239 Logs are written to logs/pnf-simulator.log.
240
241 If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again
242 EndOfMessage
243 }
244
245 function archive_logs(){
246
247     if [ -d logs ]; then
248         echo "Moving log file to archive"
249         DIR_PATH=logs/archive/simulator[$(timestamp)]
250         mkdir -p $DIR_PATH
251         if [ -f logs/pnfsimulator.log ]; then
252            mv logs/pnfsimulator.log $DIR_PATH
253         fi
254
255         if [ -f logs/*.xml ]; then
256             mv logs/*.xml $DIR_PATH
257         fi
258
259     else
260         mkdir logs
261     fi
262 }
263
264 function clear_logs(){
265
266     if [[ $(running_containers) ]]; then
267         echo "Cannot delete logs when simulator is running"
268     else
269          rm -rf logs
270     fi
271 }
272
273 function timestamp(){
274   date "+%Y-%m-%d_%T"
275 }
276
277 main $@