Merge "Update INFO.yaml file"
[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 SIMULATOR_START_URL=http://localhost:$SIMULATOR_PORT/simulator/start
10 SIMULATOR_STOP_URL=http://localhost:$SIMULATOR_PORT/simulator/stop
11 SIMULATOR_STATUS_URL=http://localhost:$SIMULATOR_PORT/simulator/status
12 RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
13
14 function 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
41 function 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
70 function 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
79 function set_vsftpd_file_owner() {
80     sudo chown root ./config/vsftpd_ssl.conf
81 }
82
83
84 function 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
93 function 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
106 function running_containers(){
107    docker-compose -f $COMPOSE_FILE_NAME ps -q
108 }
109
110 function 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
120 function run_simulator(){
121
122 cat << EndOfMessage
123 Simulator 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)
125 EndOfMessage
126 }
127
128 function stop_simulator(){
129 cat << EndOfMessage
130 Simulator response:
131 $(curl -s -X POST $SIMULATOR_STOP_URL)
132 EndOfMessage
133 }
134
135 function get_status(){
136
137     if [[ $(running_containers) ]]; then
138         print_status
139     else
140         echo "Simulator containers are down"
141     fi
142 }
143
144 function print_status(){
145 cat << EndOfMessage
146 $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
147
148 Simulator response:
149 $(curl -s -X GET $SIMULATOR_STATUS_URL)
150 EndOfMessage
151 }
152
153 function print_help(){
154 cat << EndOfMessage
155 Available options:
156 build - locally builds simulator image from existing code
157 start - starts simulator and netopeer2 containers using remote simulator image and specified model name
158 compose - customize the docker-compose and configuration based on arguments
159 run-simulator - starts sending PNF registration messages with parameters specified in config.json
160 stop-simulator - stop sending PNF registration messages
161 stop - stops both containers
162 status - prints simulator status
163 clear-logs - deletes log folder
164
165 Starting 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
177 To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
178 If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
179 Logs are written to logs/pnf-simulator.log.
180
181 If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again
182 EndOfMessage
183 }
184
185 function 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
204 function 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
213 function timestamp(){
214   date "+%Y-%m-%d_%T"
215 }
216
217 main $@