Merge "Updated Docker Image Version"
[integration.git] / test / mocks / pnfsimulator / 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         "build")
20             build_image;;
21         "start")
22             start $COMPOSE_FILE_NAME;;
23         "start-dev")
24             start_netconf_server $COMPOSE_FILE_NAME;;
25         "stop")
26             stop;;
27         "run-simulator")
28             run_simulator;;
29         "stop-simulator")
30             stop_simulator;;
31         "status")
32              get_status;;
33         "clear-logs")
34              clear_logs;;
35         *)
36             print_help;;
37     esac
38 }
39
40 function build_image(){
41     if [ -f pom.xml ]; then
42         mvn clean package docker:build
43     else
44         echo "pom.xml file not found"
45         exit 1
46     fi
47 }
48
49 function set_vsftpd_file_owner() {
50     sudo chown root ./ftpes/vsftpd/configuration/vsftpd_ssl.conf
51 }
52
53 function start_netconf_server() {
54     set_vsftpd_file_owner
55     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 }
63
64 function start(){
65
66     if [[ $(running_containers) ]]; then
67         echo "Simulator containers are already up"
68     else
69         echo "Starting simulator containers using netconf model specified in config/netconf.env"
70         set_vsftpd_file_owner
71         archive_logs
72         start_netconf_server $1
73         docker-compose -f $1 up -d $SIMULATOR_CONTAINER_NAME
74         RUNNING_COMPOSE_CONFIG=$1
75     fi
76 }
77
78 function running_containers(){
79    docker-compose -f $COMPOSE_FILE_NAME ps -q
80 }
81
82 function 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
91 function run_simulator(){
92 cat << EndOfMessage
93 Simulator response:
94 $(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)
95 EndOfMessage
96 }
97
98 function stop_simulator(){
99 cat << EndOfMessage
100 Simulator response:
101 $(curl -s -X POST $SIMULATOR_STOP_URL)
102 EndOfMessage
103 }
104
105 function get_status(){
106
107     if [[ $(running_containers) ]]; then
108         print_status
109     else
110         echo "Simulator containers are down"
111     fi
112 }
113
114 function print_status(){
115 cat << EndOfMessage
116 $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
117
118 Simulator response:
119 $(curl -s -X GET $SIMULATOR_STATUS_URL)
120 EndOfMessage
121 }
122
123 function print_help(){
124 cat << EndOfMessage
125 Available options:
126 build - locally builds simulator image from existing code
127 start - starts simulator and netopeer2 containers using remote simulator image and specified model name
128 start-dev - starts only  netopeer2 container
129 run-simulator - starts sending PNF registration messages with parameters specified in config.json
130 stop-simulator - stop sending PNF registration messages
131 stop - stops both containers
132 status - prints simulator status
133 clear-logs - deletes log folder
134
135 Starting simulation:
136
137 - 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
140 To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
141 If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
142 Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive
143
144 FOR DEVELOPERS
145 1. Build local simulator image using "./simulator.sh build"
146 2. Run containers with "./simulator.sh start-dev"
147
148 If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again
149 EndOfMessage
150 }
151
152 function 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
171 function 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
180 function timestamp(){
181   date "+%Y-%m-%d_%T"
182 }
183
184 main $@