blob: a1d7c0f2387bc2ccd1b4b1771e8557b518edb477 [file] [log] [blame]
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +02001#!/usr/bin/env bash
2
3set -euo pipefail
4
Pawel Kadlubanski43269652018-06-15 16:13:45 +02005COMPOSE_FILE_NAME=docker-compose.yml
6DEV_COMPOSE_FILE_NAME=docker-compose.dev.yml
7NETOPEER_CONTAINER_NAME=netopeer
8SIMULATOR_CONTAINER_NAME=pnf-simulator
9SIMULATOR_PORT=5000
10SIMULATOR_START_URL=http://localhost:$SIMULATOR_PORT/simulator/start
11SIMULATOR_STOP_URL=http://localhost:$SIMULATOR_PORT/simulator/stop
12SIMULATOR_STATUS_URL=http://localhost:$SIMULATOR_PORT/simulator/status
13RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020014
15function main(){
16
17 COMMAND=${1:-"help"}
18
19 case $COMMAND in
20 "build")
21 build_image;;
22 "start")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020023 start $COMPOSE_FILE_NAME;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020024 "start-dev")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020025 start $DEV_COMPOSE_FILE_NAME;;
26 "start-debug")
27 start_netconf_server $DEV_COMPOSE_FILE_NAME;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020028 "stop")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020029 stop;;
30 "run-simulator")
31 run_simulator;;
32 "stop-simulator")
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020033 stop_simulator;;
34 "status")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020035 get_status;;
36 "clear-logs")
37 clear_logs;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020038 *)
39 print_help;;
40 esac
41}
42
43function build_image(){
44 if [ -f pom.xml ]; then
Pawel Kadlubanski43269652018-06-15 16:13:45 +020045 mvn clean package
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020046 else
47 echo "pom.xml file not found"
48 exit 1
49 fi
50}
51
Pawel Kadlubanski43269652018-06-15 16:13:45 +020052function start_netconf_server() {
53 docker-compose -f $1 up -d $NETOPEER_CONTAINER_NAME
54 echo
55 echo "NETCONF server container's logs:"
56 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepoctl --install --yang=/netconf/\$NETCONF_MODEL.yang --owner=netconf:nogroup --permissions=777"
57 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepocfg --import=/netconf/\$NETCONF_MODEL.data.xml --datastore=startup --format=xml --level=3 \$NETCONF_MODEL"
58 docker exec -d $NETOPEER_CONTAINER_NAME /bin/bash -c "/opt/dev/sysrepo/build/examples/application_example \$NETCONF_MODEL"
59 echo
60}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020061
Pawel Kadlubanski43269652018-06-15 16:13:45 +020062function start(){
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020063
Pawel Kadlubanski43269652018-06-15 16:13:45 +020064 if [[ $(running_containers) ]]; then
65 echo "Simulator containers are already up"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020066 else
Pawel Kadlubanski43269652018-06-15 16:13:45 +020067 echo "Starting simulator containers using netconf model specified in config/netconf.env"
68
69 archive_logs
70 start_netconf_server $1
71 docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME
72 RUNNING_COMPOSE_CONFIG=$1
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020073 fi
74}
75
Pawel Kadlubanski43269652018-06-15 16:13:45 +020076function running_containers(){
77 docker-compose -f $COMPOSE_FILE_NAME ps -q && docker-compose -f $DEV_COMPOSE_FILE_NAME ps -q
78}
79
80function stop(){
81
82 if [[ $(running_containers) ]]; then
83 docker-compose -f $RUNNING_COMPOSE_CONFIG down
84 else
85 echo "Simulator containers are already down"
86 fi
87}
88
89function run_simulator(){
90cat << EndOfMessage
91Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +020092$(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)
Pawel Kadlubanski43269652018-06-15 16:13:45 +020093EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020094}
95
96function stop_simulator(){
Pawel Kadlubanski43269652018-06-15 16:13:45 +020097cat << EndOfMessage
98Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +020099$(curl -s -X POST $SIMULATOR_STOP_URL)
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200100EndOfMessage
101}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200102
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200103function get_status(){
104
105 if [[ $(running_containers) ]]; then
106 print_status
107 else
108 echo "Simulator containers are down"
109 fi
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200110}
111
112function print_status(){
113cat << EndOfMessage
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200114$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200115
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200116Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +0200117$(curl -s -X GET $SIMULATOR_STATUS_URL)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200118EndOfMessage
119}
120
121function print_help(){
122cat << EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200123Available options:
124build - locally builds simulator image from existing code
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200125start - starts simulator and netopeer2 containers using remote simulator image and specified model name
126start-dev - starts simulator and netopeer2 containers using remote simulator image
127start-debug - starts only netopeer2 container
128run-simulator - starts sending PNF registration messages with parameters specified in config.json
129stop-simulator - stop sending PNF registration messages
130stop - stops both containers
131status - prints simulator status
132clear-logs - deletes log folder
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200133
134Starting simulation:
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200135
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200136- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
137- To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json
138
139To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
140If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
141Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200142
143FOR DEVELOPERS
1441. Build local simulator image using "./simulator.sh build"
Pawel Kadlubanski43269652018-06-15 16:13:45 +02001452. Run containers with "./simulator.sh start-dev"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200146
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200147If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200148EndOfMessage
149}
150
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200151function archive_logs(){
152
153 if [ -d logs ]; then
154 echo "Moving log file to archive"
155 DIR_PATH=logs/archive/simulator[$(timestamp)]
156 mkdir -p $DIR_PATH
157 if [ -f logs/pnfsimulator.log ]; then
158 mv logs/pnfsimulator.log $DIR_PATH
159 fi
160
161 if [ -f logs/*.xml ]; then
162 mv logs/*.xml $DIR_PATH
163 fi
164
165 else
166 mkdir logs
167 fi
168}
169
170function clear_logs(){
171
172 if [[ $(running_containers) ]]; then
173 echo "Cannot delete logs when simulator is running"
174 else
175 rm -rf logs
176 fi
177}
178
179function timestamp(){
180 date "+%Y-%m-%d_%T"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200181}
182
183main $@