Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 3 | # Three input parameters: |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 4 | # $1 - one of {device, port, link}, specify what needs to be checked. |
| 5 | # $2 - IP address of ONOS instance. |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 6 | # $3 - Optional. absolute path of net summary json file. The default path is "/tmp/odtn/net-summary.json". |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 7 | |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 8 | summary="/tmp/odtn/net-summary.json" |
| 9 | if [[ $# == 3 ]];then |
| 10 | summary=$3 |
| 11 | fi |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 12 | # The 'sed'command behind 'wc -l' is uset to strip leading spaces. |
| 13 | # Because in some scenarios, 'wc -l' always outputs leading spaces (https://lists.gnu.org/archive/html/bug-coreutils/2005-01/msg00029.html). |
| 14 | line_num=`cat $summary | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 15 | if [[ "$line_num" != "1" ]]; then |
| 16 | echo "JSON file should have only 1 line." |
| 17 | exit 1 |
| 18 | fi |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 19 | content=`cat $summary` |
| 20 | echo -e "The content of the json file is :\n $content" |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 21 | # Extract specific value from returned json string under onos command "odtn-show-tapi-context" |
| 22 | function get_json_value() |
| 23 | { |
| 24 | local json=$1 |
| 25 | local key=$2 |
| 26 | |
| 27 | if [[ -z "$3" ]]; then |
| 28 | local num=1 |
| 29 | else |
| 30 | local num=$3 |
| 31 | fi |
| 32 | |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 33 | eval value=$(echo "${json}" | awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'${key}'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p) |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 34 | |
| 35 | return ${value} |
| 36 | } |
| 37 | |
| 38 | tried=0 |
| 39 | case "$1" in |
| 40 | "device" ) |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 41 | eval get_json_value '$content' device_num |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 42 | device_num=$? |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 43 | num_in_topo=`onos $2 devices | wc -l | sed -e 's/^[ ]*//g'` |
| 44 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<node>" | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 45 | while [[ "$num_in_topo" != "$device_num" || "$num_in_tapi" != "$device_num" ]] |
| 46 | do |
| 47 | echo "On ONOS $2, current device num in topo:$num_in_topo, num in tapi:$num_in_tapi, expected $device_num. Waiting..." |
| 48 | sleep 10 |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 49 | num_in_topo=`onos $2 devices | wc -l | sed -e 's/^[ ]*//g'` |
| 50 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<node>" | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 51 | let "tried=tried+1" |
| 52 | if [[ "$tried" == "10" ]]; then |
| 53 | exit 99 |
| 54 | fi |
| 55 | done |
| 56 | ;; |
| 57 | "port" ) |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 58 | eval get_json_value '$content' port_num |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 59 | port_num=$? |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 60 | eval get_json_value '$content' device_num |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 61 | device_num=$? |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 62 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<owned-node-edge-point>" | wc -l | sed -e 's/^[ ]*//g'` |
| 63 | num_in_topo=`onos $2 ports | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 64 | num_in_topo=$[num_in_topo-device_num] |
| 65 | while [[ "$num_in_topo" != "$port_num" || "$num_in_tapi" != "$port_num" ]] |
| 66 | do |
| 67 | echo "On ONOS $2, current port num in topo: $num_in_topo, num in tapi: $num_in_tapi, expected $port_num. Waiting..." |
| 68 | sleep 10 |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 69 | num_in_topo=`onos $2 ports | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 70 | num_in_topo=$[num_in_topo-device_num] |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 71 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<owned-node-edge-point>" | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 72 | let "tried=tried+1" |
| 73 | if [[ "$tried" == "10" ]]; then |
| 74 | exit 99 |
| 75 | fi |
| 76 | done |
| 77 | ;; |
| 78 | "link" ) |
Boyuan Yan | 36231d2 | 2019-04-09 18:38:23 +0000 | [diff] [blame] | 79 | eval get_json_value '$content' link_num |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 80 | link_num=$? |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 81 | num_in_topo=`onos $2 links | wc -l | sed -e 's/^[ ]*//g'` |
| 82 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<link>" | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 83 | while [[ "$num_in_topo" != "$link_num" || "$num_in_tapi" != "$link_num" ]] |
| 84 | do |
| 85 | echo "On ONOS $2, current link num: $num_in_topo, expected $link_num. Waiting..." |
| 86 | sleep 10 |
Boyuan Yan | c684db3 | 2019-04-21 15:48:23 -0700 | [diff] [blame] | 87 | num_in_topo=`onos $2 links | wc -l | sed -e 's/^[ ]*//g'` |
| 88 | num_in_tapi=`onos $2 odtn-show-tapi-context | grep "<link>" | wc -l | sed -e 's/^[ ]*//g'` |
Boyuan Yan | 1c27bc7 | 2019-02-15 19:22:19 +0000 | [diff] [blame] | 89 | let "tried=tried+1" |
| 90 | if [[ "$tried" == "10" ]]; then |
| 91 | exit 99 |
| 92 | fi |
| 93 | done |
| 94 | esac |