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