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