blob: 6b61e6b87585901c83c912ccc40d678aebb2df21 [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
Marcin Migdal7b601172018-10-30 14:08:37 +010049function set_vsftpd_file_owner() {
50 sudo chown root ./ftpes/vsftpd/configuration/vsftpd_ssl.conf
51}
52
Pawel Kadlubanski43269652018-06-15 16:13:45 +020053function start_netconf_server() {
Marcin Migdal7b601172018-10-30 14:08:37 +010054 set_vsftpd_file_owner
Pawel Kadlubanski43269652018-06-15 16:13:45 +020055 docker-compose -f $1 up -d $NETOPEER_CONTAINER_NAME
56 echo
57 echo "NETCONF server container's logs:"
58 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepoctl --install --yang=/netconf/\$NETCONF_MODEL.yang --owner=netconf:nogroup --permissions=777"
59 docker exec $NETOPEER_CONTAINER_NAME /bin/bash -c "sysrepocfg --import=/netconf/\$NETCONF_MODEL.data.xml --datastore=startup --format=xml --level=3 \$NETCONF_MODEL"
60 docker exec -d $NETOPEER_CONTAINER_NAME /bin/bash -c "/opt/dev/sysrepo/build/examples/application_example \$NETCONF_MODEL"
61 echo
62}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020063
Pawel Kadlubanski43269652018-06-15 16:13:45 +020064function start(){
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020065
Pawel Kadlubanski43269652018-06-15 16:13:45 +020066 if [[ $(running_containers) ]]; then
67 echo "Simulator containers are already up"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020068 else
Pawel Kadlubanski43269652018-06-15 16:13:45 +020069 echo "Starting simulator containers using netconf model specified in config/netconf.env"
Marcin Migdal7b601172018-10-30 14:08:37 +010070 set_vsftpd_file_owner
Pawel Kadlubanski43269652018-06-15 16:13:45 +020071 archive_logs
72 start_netconf_server $1
73 docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME
74 RUNNING_COMPOSE_CONFIG=$1
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020075 fi
76}
77
Pawel Kadlubanski43269652018-06-15 16:13:45 +020078function running_containers(){
Marcin Migdal3b2d33d2018-08-20 12:03:10 +020079 docker-compose -f $COMPOSE_FILE_NAME ps -q
Pawel Kadlubanski43269652018-06-15 16:13:45 +020080}
81
82function stop(){
83
84 if [[ $(running_containers) ]]; then
85 docker-compose -f $RUNNING_COMPOSE_CONFIG down
86 else
87 echo "Simulator containers are already down"
88 fi
89}
90
91function run_simulator(){
92cat << EndOfMessage
93Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +020094$(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 +020095EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +020096}
97
98function stop_simulator(){
Pawel Kadlubanski43269652018-06-15 16:13:45 +020099cat << EndOfMessage
100Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +0200101$(curl -s -X POST $SIMULATOR_STOP_URL)
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200102EndOfMessage
103}
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200104
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200105function get_status(){
106
107 if [[ $(running_containers) ]]; then
108 print_status
109 else
110 echo "Simulator containers are down"
111 fi
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200112}
113
114function print_status(){
115cat << EndOfMessage
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200116$(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200117
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200118Simulator response:
Marcin Migdalffd52662018-08-02 13:40:25 +0200119$(curl -s -X GET $SIMULATOR_STATUS_URL)
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200120EndOfMessage
121}
122
123function print_help(){
124cat << EndOfMessage
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200125Available options:
126build - locally builds simulator image from existing code
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200127start - starts simulator and netopeer2 containers using remote simulator image and specified model name
Marcin Migdal3b2d33d2018-08-20 12:03:10 +0200128start-dev - starts only netopeer2 container
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200129run-simulator - starts sending PNF registration messages with parameters specified in config.json
130stop-simulator - stop sending PNF registration messages
131stop - stops both containers
132status - prints simulator status
133clear-logs - deletes log folder
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200134
135Starting simulation:
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200136
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200137- Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine
138- To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json
139
140To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
141If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
142Logs 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 +0200143
144FOR DEVELOPERS
1451. Build local simulator image using "./simulator.sh build"
Marcin Migdal7b601172018-10-30 14:08:37 +01001462. Run containers with "./simulator.sh start-dev"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200147
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200148If 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 +0200149EndOfMessage
150}
151
Pawel Kadlubanski43269652018-06-15 16:13:45 +0200152function archive_logs(){
153
154 if [ -d logs ]; then
155 echo "Moving log file to archive"
156 DIR_PATH=logs/archive/simulator[$(timestamp)]
157 mkdir -p $DIR_PATH
158 if [ -f logs/pnfsimulator.log ]; then
159 mv logs/pnfsimulator.log $DIR_PATH
160 fi
161
162 if [ -f logs/*.xml ]; then
163 mv logs/*.xml $DIR_PATH
164 fi
165
166 else
167 mkdir logs
168 fi
169}
170
171function clear_logs(){
172
173 if [[ $(running_containers) ]]; then
174 echo "Cannot delete logs when simulator is running"
175 else
176 rm -rf logs
177 fi
178}
179
180function timestamp(){
181 date "+%Y-%m-%d_%T"
Pawel Kadlubanski8d0066e2018-05-11 14:35:43 +0200182}
183
184main $@