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