blob: 4c5d9f0625a44e495aa66764cb3fcc109fa9e7f4 [file] [log] [blame]
TamasBakaid38feb62019-02-28 09:06:19 +00001#!/usr/bin/env bash
2
3set -euo pipefail
4
5COMPOSE_FILE_NAME=docker-compose.yml
6NETOPEER_CONTAINER_NAME=netopeer
7SIMULATOR_CONTAINER_NAME=pnf-simulator
8SIMULATOR_PORT=5000
TamasBakai114c21c2019-03-19 09:56:28 +00009
10SIMULATOR_BASE=http://localhost:$SIMULATOR_PORT/simulator/
11SIMULATOR_START_URL=$SIMULATOR_BASE/start
12SIMULATOR_STOP_URL=$SIMULATOR_BASE/stop
13SIMULATOR_STATUS_URL=$SIMULATOR_BASE/status
14
TamasBakaid38feb62019-02-28 09:06:19 +000015RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
16
17function main(){
18
19 COMMAND=${1:-"help"}
20
21 case $COMMAND in
22 "compose")
TamasBakaib59bffb2019-03-22 09:52:03 +000023 compose $2 $3 $4 $5 $6 $7 $8 $9 "${10}" "${11}" ;;
24 #IPGW, #IPSUBNET, #I, #IPVES, #IPPNFSIM, #IPFILESERVER, #PORTSFTP, #PORTFTPS, #IPFTPS, #IPSFTP
TamasBakaid38feb62019-02-28 09:06:19 +000025 "build")
26 build_image;;
27 "start")
28 start $COMPOSE_FILE_NAME;;
29 "stop")
TamasBakaibab325b2019-03-26 09:20:16 +000030 if [[ -z ${2+x} ]]
31 then
32 echo "Error: action 'stop' requires the instance identifier"
33 exit
34 fi
RehanRaza48f92042019-03-20 08:12:55 +000035 stop $2;;
TamasBakaid38feb62019-02-28 09:06:19 +000036 "run-simulator")
37 run_simulator;;
TamasBakai114c21c2019-03-19 09:56:28 +000038 "trigger-simulator")
39 trigger_simulator;;
TamasBakaid38feb62019-02-28 09:06:19 +000040 "stop-simulator")
41 stop_simulator;;
42 "status")
43 get_status;;
44 "clear-logs")
45 clear_logs;;
46 *)
47 print_help;;
48 esac
49}
50
TamasBakai114c21c2019-03-19 09:56:28 +000051
52function 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
TamasBakaid38feb62019-02-28 09:06:19 +000063function 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 IPVES=$4
71 export IPPNFSIM=$5
TamasBakaib59bffb2019-03-22 09:52:03 +000072 export IPFILESERVER=$6
73 export PORTSFTP=$7
74 export PORTFTPS=$8
75 export IPFTPS=$9
76 export IPSFTP=${10}
TamasBakaid38feb62019-02-28 09:06:19 +000077
78 #will insert $I to distinguish containers, networks properly
79 #docker compose cannot substitute these, as they are keys, not values.
80 envsubst < docker-compose-template.yml > docker-compose-temporary.yml
81 #variable substitution
82 docker-compose -f docker-compose-temporary.yml config > docker-compose.yml
83 rm docker-compose-temporary.yml
84
RehanRaza48f92042019-03-20 08:12:55 +000085 ./ROP_file_creator.sh $I &
TamasBakaid38feb62019-02-28 09:06:19 +000086
87 set_vsftpd_file_owner
88
TamasBakaib59bffb2019-03-22 09:52:03 +000089 write_config $IPVES $IPFILESERVER $PORTSFTP $PORTFTPS $IPPNFSIM
TamasBakaid38feb62019-02-28 09:06:19 +000090
91}
92
93function build_image(){
94 if [ -f pom.xml ]; then
TamasBakai114c21c2019-03-19 09:56:28 +000095 mvn clean package docker:build -Dcheckstyle.skip -DskipTests
TamasBakaid38feb62019-02-28 09:06:19 +000096 else
97 echo "pom.xml file not found"
98 exit 1
99 fi
100}
101
102function set_vsftpd_file_owner() {
103 sudo chown root ./config/vsftpd_ssl.conf
104}
105
106
107function write_config(){
108 #building a YML file for usage in Java
TamasBakaibab325b2019-03-26 09:20:16 +0000109 echo "urlves: $1" > config/config.yml
110 echo "urlsftp: sftp://onap:pano@$2:$3" >> config/config.yml
111 echo "urlftps: ftps://onap:pano@$2:$4" >> config/config.yml
TamasBakaib59bffb2019-03-22 09:52:03 +0000112 echo "ippnfsim: $5" >> config/config.yml
TamasBakaibab325b2019-03-26 09:20:16 +0000113 echo "defaultfileserver: sftp" >> config/config.yml
TamasBakaid38feb62019-02-28 09:06:19 +0000114}
115
116function start(){
117
TamasBakai114c21c2019-03-19 09:56:28 +0000118 get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000119 if [[ $(running_containers) ]]; then
120 echo "Simulator containers are already up"
121 else
122 echo "Starting simulator containers using netconf model specified in config/netconf.env"
123 set_vsftpd_file_owner
124 archive_logs
125 docker-compose -f $1 up -d
126 RUNNING_COMPOSE_CONFIG=$1
127 fi
128}
129
130function running_containers(){
131 docker-compose -f $COMPOSE_FILE_NAME ps -q
132}
133
134function stop(){
TamasBakai114c21c2019-03-19 09:56:28 +0000135 get_pnfsim_ip
TamasBakaibab325b2019-03-26 09:20:16 +0000136 kill $(ps -ef | grep "[.]/ROP_file_creator.sh $1" | head -n 1 | awk '{print $2}')
RehanRaza48f92042019-03-20 08:12:55 +0000137
TamasBakaid38feb62019-02-28 09:06:19 +0000138 if [[ $(running_containers) ]]; then
139 docker-compose -f $RUNNING_COMPOSE_CONFIG down
140 docker-compose -f $RUNNING_COMPOSE_CONFIG rm
141 else
142 echo "Simulator containers are already down"
143 fi
144}
145
TamasBakai114c21c2019-03-19 09:56:28 +0000146function trigger_simulator(){
147get_pnfsim_ip
148cat << EndOfMessage
149Simulator response:
150$(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)
151EndOfMessage
152}
TamasBakaid38feb62019-02-28 09:06:19 +0000153
TamasBakai114c21c2019-03-19 09:56:28 +0000154function run_simulator(){
155get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000156cat << EndOfMessage
157Simulator response:
158$(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)
159EndOfMessage
160}
161
162function stop_simulator(){
TamasBakai114c21c2019-03-19 09:56:28 +0000163get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000164cat << EndOfMessage
165Simulator response:
166$(curl -s -X POST $SIMULATOR_STOP_URL)
167EndOfMessage
168}
169
170function get_status(){
TamasBakai114c21c2019-03-19 09:56:28 +0000171 get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000172 if [[ $(running_containers) ]]; then
173 print_status
174 else
175 echo "Simulator containers are down"
176 fi
177}
178
179function print_status(){
TamasBakai114c21c2019-03-19 09:56:28 +0000180get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000181cat << EndOfMessage
182$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
183
184Simulator response:
185$(curl -s -X GET $SIMULATOR_STATUS_URL)
186EndOfMessage
187}
188
189function print_help(){
190cat << EndOfMessage
191Available options:
192build - locally builds simulator image from existing code
193start - starts simulator and netopeer2 containers using remote simulator image and specified model name
194compose - customize the docker-compose and configuration based on arguments
TamasBakai114c21c2019-03-19 09:56:28 +0000195trigger-simulator - start monitoring the ROP files and report periodically
TamasBakaid38feb62019-02-28 09:06:19 +0000196run-simulator - starts sending PNF registration messages with parameters specified in config.json
197stop-simulator - stop sending PNF registration messages
198stop - stops both containers
199status - prints simulator status
200clear-logs - deletes log folder
201
202Starting simulation:
203- Setup the instance of this simulator by:
204 - ./simulator.sh compose IPGW IPSUBNET I IPVES IPPNFSIM IPFTPS IPSFTP
205 where Gw and subnet will be used for docker network
206 where I is the integer suffix to differentiate instances
207 where IPVES is the address of the VES collector
208 where IPPNFSIM, IPFTPS, IPSFTP are the addresses for containers
209 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
210
211- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
212- 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}
213
214To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
215If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
216Logs are written to logs/pnf-simulator.log.
217
218If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again
219EndOfMessage
220}
221
222function archive_logs(){
223
224 if [ -d logs ]; then
225 echo "Moving log file to archive"
226 DIR_PATH=logs/archive/simulator[$(timestamp)]
227 mkdir -p $DIR_PATH
228 if [ -f logs/pnfsimulator.log ]; then
229 mv logs/pnfsimulator.log $DIR_PATH
230 fi
231
232 if [ -f logs/*.xml ]; then
233 mv logs/*.xml $DIR_PATH
234 fi
235
236 else
237 mkdir logs
238 fi
239}
240
241function clear_logs(){
242
243 if [[ $(running_containers) ]]; then
244 echo "Cannot delete logs when simulator is running"
245 else
246 rm -rf logs
247 fi
248}
249
250function timestamp(){
251 date "+%Y-%m-%d_%T"
252}
253
254main $@