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