blob: 3de96dafe2b25235c319b85056af113563b684fa [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")
TamasBakaie781c7c2019-07-11 11:26:20 +000023 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
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
TamasBakaie781c7c2019-07-11 11:26:20 +000066
TamasBakaid38feb62019-02-28 09:06:19 +000067 export IPGW=$1
68 export IPSUBNET=$2
69 export I=$3
RehanRaza4b50bdd2019-05-03 14:28:39 +000070 export URLVES=$4
TamasBakaid38feb62019-02-28 09:06:19 +000071 export IPPNFSIM=$5
TamasBakaib59bffb2019-03-22 09:52:03 +000072 export IPFILESERVER=$6
RehanRaza4b50bdd2019-05-03 14:28:39 +000073 export TYPEFILESERVER=$7
74 export PORTSFTP=$8
75 export PORTFTPS=$9
76 export IPFTPS=${10}
77 export IPSFTP=${11}
TamasBakaie781c7c2019-07-11 11:26:20 +000078 export FTPS_PASV_MIN=${12}
79 export FTPS_PASV_MAX=${13}
RehanRazac5121c52019-04-04 12:42:15 +000080 LOCALTIME=$(ls -l /etc/localtime)
81 export TIMEZONE=${LOCALTIME//*zoneinfo\/}
TamasBakaid38feb62019-02-28 09:06:19 +000082
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
RehanRaza48f92042019-03-20 08:12:55 +000090 ./ROP_file_creator.sh $I &
TamasBakaid38feb62019-02-28 09:06:19 +000091
RehanRaza4b50bdd2019-05-03 14:28:39 +000092 write_config $URLVES $IPFILESERVER $TYPEFILESERVER $PORTSFTP $PORTFTPS $IPPNFSIM
TamasBakaid38feb62019-02-28 09:06:19 +000093
TamasBakaie781c7c2019-07-11 11:26:20 +000094 cd config
95 envsubst < vsftpd_ssl-TEMPLATE.conf > vsftpd_ssl.conf
96 cd -
97
98 set_vsftpd_file_owner
99
TamasBakaid38feb62019-02-28 09:06:19 +0000100}
101
102function build_image(){
103 if [ -f pom.xml ]; then
RehanRazaf2a0cfd2019-04-09 08:18:07 +0000104 mvn clean package docker:build -Dcheckstyle.skip
TamasBakaid38feb62019-02-28 09:06:19 +0000105 else
106 echo "pom.xml file not found"
107 exit 1
108 fi
109}
110
111function set_vsftpd_file_owner() {
112 sudo chown root ./config/vsftpd_ssl.conf
113}
114
115
116function write_config(){
117 #building a YML file for usage in Java
TamasBakaibab325b2019-03-26 09:20:16 +0000118 echo "urlves: $1" > config/config.yml
RehanRaza4b50bdd2019-05-03 14:28:39 +0000119 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
TamasBakaid38feb62019-02-28 09:06:19 +0000123}
124
125function start(){
126
TamasBakai114c21c2019-03-19 09:56:28 +0000127 get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000128 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
139function running_containers(){
140 docker-compose -f $COMPOSE_FILE_NAME ps -q
141}
142
143function stop(){
TamasBakai114c21c2019-03-19 09:56:28 +0000144 get_pnfsim_ip
TamasBakaibab325b2019-03-26 09:20:16 +0000145 kill $(ps -ef | grep "[.]/ROP_file_creator.sh $1" | head -n 1 | awk '{print $2}')
RehanRaza48f92042019-03-20 08:12:55 +0000146
TamasBakaid38feb62019-02-28 09:06:19 +0000147 if [[ $(running_containers) ]]; then
148 docker-compose -f $RUNNING_COMPOSE_CONFIG down
149 docker-compose -f $RUNNING_COMPOSE_CONFIG rm
150 else
151 echo "Simulator containers are already down"
152 fi
153}
154
TamasBakai114c21c2019-03-19 09:56:28 +0000155function trigger_simulator(){
156get_pnfsim_ip
157cat << EndOfMessage
158Simulator response:
159$(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)
160EndOfMessage
161}
TamasBakaid38feb62019-02-28 09:06:19 +0000162
TamasBakai114c21c2019-03-19 09:56:28 +0000163function run_simulator(){
164get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000165cat << EndOfMessage
166Simulator response:
167$(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)
168EndOfMessage
169}
170
171function stop_simulator(){
TamasBakai114c21c2019-03-19 09:56:28 +0000172get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000173cat << EndOfMessage
174Simulator response:
175$(curl -s -X POST $SIMULATOR_STOP_URL)
176EndOfMessage
177}
178
179function get_status(){
TamasBakai114c21c2019-03-19 09:56:28 +0000180 get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000181 if [[ $(running_containers) ]]; then
182 print_status
183 else
184 echo "Simulator containers are down"
185 fi
186}
187
188function print_status(){
TamasBakai114c21c2019-03-19 09:56:28 +0000189get_pnfsim_ip
TamasBakaid38feb62019-02-28 09:06:19 +0000190cat << EndOfMessage
191$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
192
193Simulator response:
194$(curl -s -X GET $SIMULATOR_STATUS_URL)
195EndOfMessage
196}
197
198function print_help(){
199cat << EndOfMessage
200Available options:
201build - locally builds simulator image from existing code
202start - starts simulator and netopeer2 containers using remote simulator image and specified model name
203compose - customize the docker-compose and configuration based on arguments
TamasBakai114c21c2019-03-19 09:56:28 +0000204trigger-simulator - start monitoring the ROP files and report periodically
TamasBakaid38feb62019-02-28 09:06:19 +0000205run-simulator - starts sending PNF registration messages with parameters specified in config.json
206stop-simulator - stop sending PNF registration messages
207stop - stops both containers
208status - prints simulator status
209clear-logs - deletes log folder
210
211Starting simulation:
212- Setup the instance of this simulator by:
RehanRaza4b50bdd2019-05-03 14:28:39 +0000213 - ./simulator.sh compose IPGW IPSUBNET I URLVES IPPNFSIM IPFILESERVER TYPEFILESERVER PORTSFTP PORTFTPS IPFTPS IPSFTP
214 where IPGW and IPSUBNET will be used for docker network
TamasBakaid38feb62019-02-28 09:06:19 +0000215 where I is the integer suffix to differentiate instances
RehanRaza4b50bdd2019-05-03 14:28:39 +0000216 where URLVES is the URL of the VES collector
217 where IPPNFSIM, IPFILESERVER, IPFTPS, IPSFTP are the IP addresses for containers
218 where TYPEFILESERVER is the type of fileserver, i.e., FTPS or SFTP
219 where PORTSFTP, PORTFTPS are the SFTP and FTPS ports
220 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
TamasBakaid38feb62019-02-28 09:06:19 +0000221
222- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
223- 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}
224
225To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
226If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
227Logs are written to logs/pnf-simulator.log.
228
229If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again
230EndOfMessage
231}
232
233function archive_logs(){
234
235 if [ -d logs ]; then
236 echo "Moving log file to archive"
237 DIR_PATH=logs/archive/simulator[$(timestamp)]
238 mkdir -p $DIR_PATH
239 if [ -f logs/pnfsimulator.log ]; then
240 mv logs/pnfsimulator.log $DIR_PATH
241 fi
242
243 if [ -f logs/*.xml ]; then
244 mv logs/*.xml $DIR_PATH
245 fi
246
247 else
248 mkdir logs
249 fi
250}
251
252function clear_logs(){
253
254 if [[ $(running_containers) ]]; then
255 echo "Cannot delete logs when simulator is running"
256 else
257 rm -rf logs
258 fi
259}
260
261function timestamp(){
262 date "+%Y-%m-%d_%T"
263}
264
265main $@