Merge "update the MR image with the new certificate"
[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 DEV_COMPOSE_FILE_NAME=docker-compose.dev.yml
7 NETOPEER_CONTAINER_NAME=netopeer
8 SIMULATOR_CONTAINER_NAME=pnf-simulator
9 SIMULATOR_PORT=5000
10 SIMULATOR_START_URL=http://localhost:$SIMULATOR_PORT/simulator/start
11 SIMULATOR_STOP_URL=http://localhost:$SIMULATOR_PORT/simulator/stop
12 SIMULATOR_STATUS_URL=http://localhost:$SIMULATOR_PORT/simulator/status
13 RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME
14
15 function main(){
16
17     COMMAND=${1:-"help"}
18
19     case $COMMAND in
20         "build")
21             build_image;;
22         "start")
23             start $COMPOSE_FILE_NAME;;
24         "start-dev")
25             start $DEV_COMPOSE_FILE_NAME;;
26         "start-debug")
27             start_netconf_server $DEV_COMPOSE_FILE_NAME;;
28         "stop")
29             stop;;
30         "run-simulator")
31             run_simulator;;
32         "stop-simulator")
33             stop_simulator;;
34         "status")
35              get_status;;
36         "clear-logs")
37              clear_logs;;
38         *)
39             print_help;;
40     esac
41 }
42
43 function build_image(){
44     if [ -f pom.xml ]; then
45         mvn clean package 
46     else
47         echo "pom.xml file not found"
48         exit 1
49     fi
50 }
51
52 function 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 }
61
62 function start(){
63
64     if [[ $(running_containers) ]]; then
65         echo "Simulator containers are already up"
66     else
67         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
73     fi
74 }
75
76 function running_containers(){
77    docker-compose -f $COMPOSE_FILE_NAME ps -q && docker-compose -f $DEV_COMPOSE_FILE_NAME ps -q
78 }
79
80 function 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
89 function run_simulator(){
90 cat << EndOfMessage
91 Simulator response:
92 $(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)
93 EndOfMessage
94 }
95
96 function stop_simulator(){
97 cat << EndOfMessage
98 Simulator response:
99 $(curl -s -X POST $SIMULATOR_STOP_URL)
100 EndOfMessage
101 }
102
103 function get_status(){
104
105     if [[ $(running_containers) ]]; then
106         print_status
107     else
108         echo "Simulator containers are down"
109     fi
110 }
111
112 function print_status(){
113 cat << EndOfMessage
114 $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps)
115
116 Simulator response:
117 $(curl -s -X GET $SIMULATOR_STATUS_URL)
118 EndOfMessage
119 }
120
121 function print_help(){
122 cat << EndOfMessage
123 Available options:
124 build - locally builds simulator image from existing code
125 start - starts simulator and netopeer2 containers using remote simulator image and specified model name
126 start-dev - starts simulator and netopeer2 containers using remote simulator image
127 start-debug - starts only  netopeer2 container
128 run-simulator - starts sending PNF registration messages with parameters specified in config.json
129 stop-simulator - stop sending PNF registration messages
130 stop - stops both containers
131 status - prints simulator status
132 clear-logs - deletes log folder
133
134 Starting simulation:
135
136 - 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
139 To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status".
140 If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again
141 Logs are written to logs/pnf-simulator.log. After each "start/start-dev" old log files are moved to the archive
142
143 FOR DEVELOPERS
144 1. Build local simulator image using "./simulator.sh build"
145 2. Run containers with "./simulator.sh start-dev"
146
147 If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start/start-dev" again
148 EndOfMessage
149 }
150
151 function 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
170 function 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
179 function timestamp(){
180   date "+%Y-%m-%d_%T"
181 }
182
183 main $@