Boyuan Yan | ed2ea357 | 2019-04-02 15:38:42 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | # Two input parameters: |
| 4 | # $1 - one of {device, port, link}, specify what needs to be checked. |
| 5 | # $2 - IP address of ONOS instance. |
| 6 | |
| 7 | # remove all spaces of first input parameter |
| 8 | # In Mac or other systems, there maybe extra whitespaces before the result of `wc -l` |
| 9 | function strip_space() |
| 10 | { |
| 11 | tmp=$1 |
| 12 | tmp="${tmp// /}" |
| 13 | return $tmp |
| 14 | } |
| 15 | |
| 16 | strip_space $(cat ~/emulator/net-summary.json | wc -l) |
| 17 | line_num=$? |
| 18 | if [[ $line_num != 1 ]]; then |
| 19 | echo "JSON file should have only 1 line." |
| 20 | exit 1 |
| 21 | fi |
| 22 | |
| 23 | # Extract specific value from returned json string under onos command "odtn-show-tapi-context" |
| 24 | function get_json_value() |
| 25 | { |
| 26 | local json=$1 |
| 27 | local key=$2 |
| 28 | |
| 29 | if [[ -z "$3" ]]; then |
| 30 | local num=1 |
| 31 | else |
| 32 | local num=$3 |
| 33 | fi |
| 34 | |
| 35 | local value=$(echo "${json}" | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'${key}'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p) |
| 36 | |
| 37 | return ${value} |
| 38 | } |
| 39 | |
| 40 | tried=0 |
| 41 | case "$1" in |
| 42 | "device" ) |
| 43 | get_json_value $( cat ~/emulator/net-summary.json) device_num |
| 44 | device_num=$? |
| 45 | strip_space $(onos $2 devices | wc -l) |
| 46 | num_in_topo=$? |
| 47 | strip_space $(onos $2 odtn-show-tapi-context | grep "<node>" | wc -l) |
| 48 | num_in_tapi=$? |
| 49 | while [[ "$num_in_topo" != "$device_num" || "$num_in_tapi" != "$device_num" ]] |
| 50 | do |
| 51 | echo "On ONOS $2, current device num in topo:$num_in_topo, num in tapi:$num_in_tapi, expected $device_num. Waiting..." |
| 52 | sleep 10 |
| 53 | strip_space $(onos $2 devices | wc -l) |
| 54 | num_in_topo=$? |
| 55 | strip_space $(onos $2 odtn-show-tapi-context | grep "<node>" | wc -l) |
| 56 | num_in_tapi=$? |
| 57 | let "tried=tried+1" |
| 58 | if [[ "$tried" == "10" ]]; then |
| 59 | exit 99 |
| 60 | fi |
| 61 | done |
| 62 | ;; |
| 63 | "port" ) |
| 64 | get_json_value $( cat ~/emulator/net-summary.json) port_num |
| 65 | port_num=$? |
| 66 | get_json_value $( cat ~/emulator/net-summary.json) device_num |
| 67 | device_num=$? |
| 68 | strip_space $(onos $2 ports | wc -l) |
| 69 | num_in_topo=$? |
| 70 | strip_space $(onos $2 odtn-show-tapi-context | grep "<owned-node-edge-point>" | wc -l) |
| 71 | num_in_tapi=$? |
| 72 | num_in_topo=$[num_in_topo-device_num] |
| 73 | while [[ "$num_in_topo" != "$port_num" || "$num_in_tapi" != "$port_num" ]] |
| 74 | do |
| 75 | echo "On ONOS $2, current port num in topo: $num_in_topo, num in tapi: $num_in_tapi, expected $port_num. Waiting..." |
| 76 | sleep 10 |
| 77 | strip_space $(onos $2 ports | wc -l) |
| 78 | num_in_topo=$? |
| 79 | strip_space $(onos $2 odtn-show-tapi-context | grep "<owned-node-edge-point>" | wc -l) |
| 80 | num_in_tapi=$? |
| 81 | num_in_topo=$[num_in_topo-device_num] |
| 82 | let "tried=tried+1" |
| 83 | if [[ "$tried" == "10" ]]; then |
| 84 | exit 99 |
| 85 | fi |
| 86 | done |
| 87 | ;; |
| 88 | "link" ) |
| 89 | get_json_value $( cat ~/emulator/net-summary.json) link_num |
| 90 | link_num=$? |
| 91 | strip_space $(onos $2 links | wc -l) |
| 92 | num_in_topo=$? |
| 93 | strip_space $(onos $2 odtn-show-tapi-context | grep "<link>" | wc -l) |
| 94 | num_in_tapi=$? |
| 95 | while [[ "$num_in_topo" != "$link_num" || "$num_in_tapi" != "$link_num" ]] |
| 96 | do |
| 97 | echo "On ONOS $2, current link num in topo: $num_in_topo, num in tapi: $num_in_tapi, expected $link_num. Waiting..." |
| 98 | sleep 10 |
| 99 | num_in_topo=`onos $2 links | wc -l` |
| 100 | strip_space $(onos $2 links | wc -l) |
| 101 | num_in_topo=$? |
| 102 | strip_space $(onos $2 odtn-show-tapi-context | grep "<link>" | wc -l) |
| 103 | num_in_tapi=$? |
| 104 | let "tried=tried+1" |
| 105 | if [[ "$tried" == "10" ]]; then |
| 106 | exit 99 |
| 107 | fi |
| 108 | done |
| 109 | esac |