TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 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 |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 9 | |
| 10 | SIMULATOR_BASE=http://localhost:$SIMULATOR_PORT/simulator/ |
| 11 | SIMULATOR_START_URL=$SIMULATOR_BASE/start |
| 12 | SIMULATOR_STOP_URL=$SIMULATOR_BASE/stop |
| 13 | SIMULATOR_STATUS_URL=$SIMULATOR_BASE/status |
| 14 | |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 15 | RUNNING_COMPOSE_CONFIG=$COMPOSE_FILE_NAME |
| 16 | |
| 17 | function main(){ |
| 18 | |
| 19 | COMMAND=${1:-"help"} |
| 20 | |
| 21 | case $COMMAND in |
| 22 | "compose") |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 23 | compose $2 $3 $4 $5 $6 $7 $8 $9 "${10}" "${11}" ;; |
| 24 | #IPGW, #IPSUBNET, #I, #IPVES, #IPPNFSIM, #IPFILESERVER, #PORTSFTP, #PORTFTPS, #IPFTPS, #IPSFTP |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 25 | "build") |
| 26 | build_image;; |
| 27 | "start") |
| 28 | start $COMPOSE_FILE_NAME;; |
| 29 | "stop") |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 30 | if [[ -z ${2+x} ]] |
| 31 | then |
| 32 | echo "Error: action 'stop' requires the instance identifier" |
| 33 | exit |
| 34 | fi |
RehanRaza | 48f9204 | 2019-03-20 08:12:55 +0000 | [diff] [blame] | 35 | stop $2;; |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 36 | "run-simulator") |
| 37 | run_simulator;; |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 38 | "trigger-simulator") |
| 39 | trigger_simulator;; |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 40 | "stop-simulator") |
| 41 | stop_simulator;; |
| 42 | "status") |
| 43 | get_status;; |
| 44 | "clear-logs") |
| 45 | clear_logs;; |
| 46 | *) |
| 47 | print_help;; |
| 48 | esac |
| 49 | } |
| 50 | |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 51 | |
| 52 | function get_pnfsim_ip() { |
| 53 | |
| 54 | export IPPNFSIM=$(cat ./config/config.yml | grep ippnfsim | awk -F'[ ]' '{print $2}') |
| 55 | echo "PNF-Sim IP: " $IPPNFSIM |
| 56 | |
| 57 | export SIMULATOR_BASE=http://$IPPNFSIM:$SIMULATOR_PORT/simulator/ |
| 58 | export SIMULATOR_START_URL=$SIMULATOR_BASE/start |
| 59 | export SIMULATOR_STOP_URL=$SIMULATOR_BASE/stop |
| 60 | export SIMULATOR_STATUS_URL=$SIMULATOR_BASE/status |
| 61 | } |
| 62 | |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 63 | function compose(){ |
| 64 | #creating custom docker-compose based on IP arguments |
| 65 | #creting config.json by injecting the same IP |
| 66 | |
| 67 | export IPGW=$1 |
| 68 | export IPSUBNET=$2 |
| 69 | export I=$3 |
| 70 | export IPVES=$4 |
| 71 | export IPPNFSIM=$5 |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 72 | export IPFILESERVER=$6 |
| 73 | export PORTSFTP=$7 |
| 74 | export PORTFTPS=$8 |
| 75 | export IPFTPS=$9 |
| 76 | export IPSFTP=${10} |
RehanRaza | c5121c5 | 2019-04-04 12:42:15 +0000 | [diff] [blame] | 77 | LOCALTIME=$(ls -l /etc/localtime) |
| 78 | export TIMEZONE=${LOCALTIME//*zoneinfo\/} |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 79 | |
| 80 | #will insert $I to distinguish containers, networks properly |
| 81 | #docker compose cannot substitute these, as they are keys, not values. |
| 82 | envsubst < docker-compose-template.yml > docker-compose-temporary.yml |
| 83 | #variable substitution |
| 84 | docker-compose -f docker-compose-temporary.yml config > docker-compose.yml |
| 85 | rm docker-compose-temporary.yml |
| 86 | |
RehanRaza | 48f9204 | 2019-03-20 08:12:55 +0000 | [diff] [blame] | 87 | ./ROP_file_creator.sh $I & |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 88 | |
| 89 | set_vsftpd_file_owner |
| 90 | |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 91 | write_config $IPVES $IPFILESERVER $PORTSFTP $PORTFTPS $IPPNFSIM |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 92 | |
| 93 | } |
| 94 | |
| 95 | function build_image(){ |
| 96 | if [ -f pom.xml ]; then |
RehanRaza | f2a0cfd | 2019-04-09 08:18:07 +0000 | [diff] [blame] | 97 | mvn clean package docker:build -Dcheckstyle.skip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 98 | else |
| 99 | echo "pom.xml file not found" |
| 100 | exit 1 |
| 101 | fi |
| 102 | } |
| 103 | |
| 104 | function set_vsftpd_file_owner() { |
| 105 | sudo chown root ./config/vsftpd_ssl.conf |
| 106 | } |
| 107 | |
| 108 | |
| 109 | function write_config(){ |
| 110 | #building a YML file for usage in Java |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 111 | echo "urlves: $1" > config/config.yml |
| 112 | echo "urlsftp: sftp://onap:pano@$2:$3" >> config/config.yml |
| 113 | echo "urlftps: ftps://onap:pano@$2:$4" >> config/config.yml |
TamasBakai | b59bffb | 2019-03-22 09:52:03 +0000 | [diff] [blame] | 114 | echo "ippnfsim: $5" >> config/config.yml |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 115 | echo "defaultfileserver: sftp" >> config/config.yml |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | function start(){ |
| 119 | |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 120 | get_pnfsim_ip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 121 | if [[ $(running_containers) ]]; then |
| 122 | echo "Simulator containers are already up" |
| 123 | else |
| 124 | echo "Starting simulator containers using netconf model specified in config/netconf.env" |
| 125 | set_vsftpd_file_owner |
| 126 | archive_logs |
| 127 | docker-compose -f $1 up -d |
| 128 | RUNNING_COMPOSE_CONFIG=$1 |
| 129 | fi |
| 130 | } |
| 131 | |
| 132 | function running_containers(){ |
| 133 | docker-compose -f $COMPOSE_FILE_NAME ps -q |
| 134 | } |
| 135 | |
| 136 | function stop(){ |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 137 | get_pnfsim_ip |
TamasBakai | bab325b | 2019-03-26 09:20:16 +0000 | [diff] [blame] | 138 | kill $(ps -ef | grep "[.]/ROP_file_creator.sh $1" | head -n 1 | awk '{print $2}') |
RehanRaza | 48f9204 | 2019-03-20 08:12:55 +0000 | [diff] [blame] | 139 | |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 140 | if [[ $(running_containers) ]]; then |
| 141 | docker-compose -f $RUNNING_COMPOSE_CONFIG down |
| 142 | docker-compose -f $RUNNING_COMPOSE_CONFIG rm |
| 143 | else |
| 144 | echo "Simulator containers are already down" |
| 145 | fi |
| 146 | } |
| 147 | |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 148 | function trigger_simulator(){ |
| 149 | get_pnfsim_ip |
| 150 | cat << EndOfMessage |
| 151 | Simulator response: |
| 152 | $(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) |
| 153 | EndOfMessage |
| 154 | } |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 155 | |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 156 | function run_simulator(){ |
| 157 | get_pnfsim_ip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 158 | cat << EndOfMessage |
| 159 | Simulator response: |
| 160 | $(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) |
| 161 | EndOfMessage |
| 162 | } |
| 163 | |
| 164 | function stop_simulator(){ |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 165 | get_pnfsim_ip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 166 | cat << EndOfMessage |
| 167 | Simulator response: |
| 168 | $(curl -s -X POST $SIMULATOR_STOP_URL) |
| 169 | EndOfMessage |
| 170 | } |
| 171 | |
| 172 | function get_status(){ |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 173 | get_pnfsim_ip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 174 | if [[ $(running_containers) ]]; then |
| 175 | print_status |
| 176 | else |
| 177 | echo "Simulator containers are down" |
| 178 | fi |
| 179 | } |
| 180 | |
| 181 | function print_status(){ |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 182 | get_pnfsim_ip |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 183 | cat << EndOfMessage |
| 184 | $(docker-compose -f $RUNNING_COMPOSE_CONFIG ps) |
| 185 | |
| 186 | Simulator response: |
| 187 | $(curl -s -X GET $SIMULATOR_STATUS_URL) |
| 188 | EndOfMessage |
| 189 | } |
| 190 | |
| 191 | function print_help(){ |
| 192 | cat << EndOfMessage |
| 193 | Available options: |
| 194 | build - locally builds simulator image from existing code |
| 195 | start - starts simulator and netopeer2 containers using remote simulator image and specified model name |
| 196 | compose - customize the docker-compose and configuration based on arguments |
TamasBakai | 114c21c | 2019-03-19 09:56:28 +0000 | [diff] [blame] | 197 | trigger-simulator - start monitoring the ROP files and report periodically |
TamasBakai | d38feb6 | 2019-02-28 09:06:19 +0000 | [diff] [blame] | 198 | run-simulator - starts sending PNF registration messages with parameters specified in config.json |
| 199 | stop-simulator - stop sending PNF registration messages |
| 200 | stop - stops both containers |
| 201 | status - prints simulator status |
| 202 | clear-logs - deletes log folder |
| 203 | |
| 204 | Starting simulation: |
| 205 | - Setup the instance of this simulator by: |
| 206 | - ./simulator.sh compose IPGW IPSUBNET I IPVES IPPNFSIM IPFTPS IPSFTP |
| 207 | where Gw and subnet will be used for docker network |
| 208 | where I is the integer suffix to differentiate instances |
| 209 | where IPVES is the address of the VES collector |
| 210 | where IPPNFSIM, IPFTPS, IPSFTP are the addresses for containers |
| 211 | e.g. ./simulator.sh compose 10.11.0.65 10.11.0.64 3 10.11.0.2 10.11.0.66 10.11.0.67 10.11.0.68 |
| 212 | |
| 213 | - Setup environment with "./simulator.sh start". It will download required docker images from the internet and run them on docker machine |
| 214 | - To start the simulation use "./simulator.sh run-simulator", which will start sending PNF registration messages with parameters specified in config.json {TODO, might not be needed} |
| 215 | |
| 216 | To stop simulation use "./simulator.sh stop-simulator" command. To check simulator's status use "./simulator.sh status". |
| 217 | If you want to change message parameters simply edit config.json, then start the simulation with "./simulator.sh run-simulator" again |
| 218 | Logs are written to logs/pnf-simulator.log. |
| 219 | |
| 220 | If you change the source code you have to rebuild image with "./simulator.sh build" and run "./simulator.sh start" again |
| 221 | EndOfMessage |
| 222 | } |
| 223 | |
| 224 | function archive_logs(){ |
| 225 | |
| 226 | if [ -d logs ]; then |
| 227 | echo "Moving log file to archive" |
| 228 | DIR_PATH=logs/archive/simulator[$(timestamp)] |
| 229 | mkdir -p $DIR_PATH |
| 230 | if [ -f logs/pnfsimulator.log ]; then |
| 231 | mv logs/pnfsimulator.log $DIR_PATH |
| 232 | fi |
| 233 | |
| 234 | if [ -f logs/*.xml ]; then |
| 235 | mv logs/*.xml $DIR_PATH |
| 236 | fi |
| 237 | |
| 238 | else |
| 239 | mkdir logs |
| 240 | fi |
| 241 | } |
| 242 | |
| 243 | function clear_logs(){ |
| 244 | |
| 245 | if [[ $(running_containers) ]]; then |
| 246 | echo "Cannot delete logs when simulator is running" |
| 247 | else |
| 248 | rm -rf logs |
| 249 | fi |
| 250 | } |
| 251 | |
| 252 | function timestamp(){ |
| 253 | date "+%Y-%m-%d_%T" |
| 254 | } |
| 255 | |
| 256 | main $@ |