Updated the Version of Stating Dockers
[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 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 }
58
59 function start(){
60
61     if [[ $(running_containers) ]]; then
62         echo "Simulator containers are already up"
63     else
64         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
70     fi
71 }
72
73 function running_containers(){
74    docker-compose -f $COMPOSE_FILE_NAME ps -q
75 }
76
77 function 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
86 function run_simulator(){
87 cat << EndOfMessage
88 Simulator response:
89 $(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)
90 EndOfMessage
91 }
92
93 function stop_simulator(){
94 cat << EndOfMessage
95 Simulator response:
96 $(curl -s -X POST $SIMULATOR_STOP_URL)
97 EndOfMessage
98 }
99
100 function get_status(){
101
102     if [[ $(running_containers) ]]; then
103         print_status
104     else
105         echo "Simulator containers are down"
106     fi
107 }
108
109 function print_status(){
110 cat << EndOfMessage
111 $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
112
113 Simulator response:
114 $(curl -s -X GET $SIMULATOR_STATUS_URL)
115 EndOfMessage
116 }
117
118 function print_help(){
119 cat << EndOfMessage
120 Available options:
121 build - locally builds simulator image from existing code
122 start - starts simulator and netopeer2 containers using remote simulator image and specified model name
123 start-dev - starts only  netopeer2 container
124 run-simulator - starts sending PNF registration messages with parameters specified in config.json
125 stop-simulator - stop sending PNF registration messages
126 stop - stops both containers
127 status - prints simulator status
128 clear-logs - deletes log folder
129
130 Starting simulation:
131
132 - 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
135 To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
136 If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
137 Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive
138
139 FOR DEVELOPERS
140 1. Build local simulator image using "./simulator.sh build"
141 2. Run containers with "./simulator.sh start-debug"
142
143 If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again
144 EndOfMessage
145 }
146
147 function 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
166 function 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
175 function timestamp(){
176   date "+%Y-%m-%d_%T"
177 }
178
179 main $@