blob: 23facce6a150e0cffd662fea032e5832b651dcb5 [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
Pawel Kadlubanski43269652018-06-15 16:13:45 +02006NETOPEER_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
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020013
14function main(){
15
16 COMMAND=${1:-"help"}
17
18 case $COMMAND in
19 "build")
20 build_image;;
21 "start")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020022 start $COMPOSE_FILE_NAME;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020023 "start-dev")
Marcin Migdal3b2d33d2018-08-20 12:03:10 +020024 start_netconf_server $COMPOSE_FILE_NAME;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020025 "stop")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020026 stop;;
27 "run-simulator")
28 run_simulator;;
29 "stop-simulator")
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020030 stop_simulator;;
31 "status")
Pawel Kadlubanski43269652018-06-15 16:13:45 +020032 get_status;;
33 "clear-logs")
34 clear_logs;;
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020035 *)
36 print_help;;
37 esac
38}
39
40function build_image(){
41 if [ -f pom.xml ]; then
Marcin Migdal3b2d33d2018-08-20 12:03:10 +020042 mvn clean package docker:build
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020043 else
44 echo "pom.xml file not found"
45 exit 1
46 fi
47}
48
Pawel Kadlubanski43269652018-06-15 16:13:45 +020049function start_netconf_server() {
50 docker-compose -f $1 up -d $NETOPEER_CONTAINER_NAME
51 echo
52 echo "NETCONF server container's logs:"
53 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepoctl --install --yang=/netconf/\$NETCONF_MODEL.yang --owner=netconf:nogroup --permissions=777"
54 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepocfg --import=/netconf/\$NETCONF_MODEL.data.xml --datastore=startup --format=xml --level=3 \$NETCONF_MODEL"
55 docker exec -d $NETOPEER_CONTAINER_NAME /bin/bash -c "/opt/dev/sysrepo/build/examples/application_example \$NETCONF_MODEL"
56 echo
57}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020058
Pawel Kadlubanski43269652018-06-15 16:13:45 +020059function start(){
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020060
Pawel Kadlubanski43269652018-06-15 16:13:45 +020061 if [[ $(running_containers) ]]; then
62 echo "Simulator containers are already up"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020063 else
Pawel Kadlubanski43269652018-06-15 16:13:45 +020064 echo "Starting simulator containers using netconf model specified in config/netconf.env"
65
66 archive_logs
67 start_netconf_server $1
68 docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME
69 RUNNING_COMPOSE_CONFIG=$1
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020070 fi
71}
72
Pawel Kadlubanski43269652018-06-15 16:13:45 +020073function running_containers(){
Marcin Migdal3b2d33d2018-08-20 12:03:10 +020074 docker-compose -f $COMPOSE_FILE_NAME ps -q
Pawel Kadlubanski43269652018-06-15 16:13:45 +020075}
76
77function stop(){
78
79 if [[ $(running_containers) ]]; then
80 docker-compose -f $RUNNING_COMPOSE_CONFIG down
81 else
82 echo "Simulator containers are already down"
83 fi
84}
85
86function run_simulator(){
87cat << EndOfMessage
88Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +020089$(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 +020090EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020091}
92
93function stop_simulator(){
Pawel Kadlubanski43269652018-06-15 16:13:45 +020094cat << EndOfMessage
95Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +020096$(curl -s -X POST $SIMULATOR_STOP_URL)
Pawel Kadlubanski43269652018-06-15 16:13:45 +020097EndOfMessage
98}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020099
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200100function get_status(){
101
102 if [[ $(running_containers) ]]; then
103 print_status
104 else
105 echo "Simulator containers are down"
106 fi
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200107}
108
109function print_status(){
110cat << EndOfMessage
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200111$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200112
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200113Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +0200114$(curl -s -X GET $SIMULATOR_STATUS_URL)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200115EndOfMessage
116}
117
118function print_help(){
119cat << EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200120Available options:
121build - locally builds simulator image from existing code
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200122start - starts simulator and netopeer2 containers using remote simulator image and specified model name
Marcin Migdal3b2d33d2018-08-20 12:03:10 +0200123start-dev - starts only netopeer2 container
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200124run-simulator - starts sending PNF registration messages with parameters specified in config.json
125stop-simulator - stop sending PNF registration messages
126stop - stops both containers
127status - prints simulator status
128clear-logs - deletes log folder
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200129
130Starting simulation:
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200131
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200132- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
133- To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json
134
135To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
136If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
137Logs 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 +0200138
139FOR DEVELOPERS
1401. Build local simulator image using "./simulator.sh build"
Marcin Migdal3b2d33d2018-08-20 12:03:10 +02001412. Run containers with "./simulator.sh start-debug"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200142
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200143If 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 +0200144EndOfMessage
145}
146
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200147function archive_logs(){
148
149 if [ -d logs ]; then
150 echo "Moving log file to archive"
151 DIR_PATH=logs/archive/simulator[$(timestamp)]
152 mkdir -p $DIR_PATH
153 if [ -f logs/pnfsimulator.log ]; then
154 mv logs/pnfsimulator.log $DIR_PATH
155 fi
156
157 if [ -f logs/*.xml ]; then
158 mv logs/*.xml $DIR_PATH
159 fi
160
161 else
162 mkdir logs
163 fi
164}
165
166function clear_logs(){
167
168 if [[ $(running_containers) ]]; then
169 echo "Cannot delete logs when simulator is running"
170 else
171 rm -rf logs
172 fi
173}
174
175function timestamp(){
176 date "+%Y-%m-%d_%T"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200177}
178
179main $@