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