|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Function to display help message |
| 4 | +display_help() { |
| 5 | + echo "Usage: sudo prefix-list <command> <PREFIX_TYPE> <NETWORK>" |
| 6 | + echo "" |
| 7 | + echo "Commands:" |
| 8 | + echo " add Add a prefix with prefix type and network." |
| 9 | + echo " Requires: <PREFIX_TYPE>, <NETWORK>." |
| 10 | + echo "" |
| 11 | + echo " remove Remove a prefix with prefix type and network." |
| 12 | + echo " Requires: <PREFIX_TYPE>, <NETWORK>." |
| 13 | + echo "" |
| 14 | + echo " status Display current prefix lists." |
| 15 | + echo " No additional parameters required." |
| 16 | + echo "" |
| 17 | + echo "Arguments:" |
| 18 | + echo " <PREFIX_TYPE> Type of prefix list. Allowed values: {$(printf "%s" "${supported_prefix_types[*]}" | tr ' ' '|')}." |
| 19 | + echo " <NETWORK> Network in CIDR format." |
| 20 | + echo "" |
| 21 | + echo "Options:" |
| 22 | + echo " -h, --help Display this help message." |
| 23 | + exit 0 |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +# Function to check if the user has root privileges |
| 28 | +check_root_privileges() { |
| 29 | + if [ "$EUID" -ne 0 ] ; then |
| 30 | + echo "Root privileges are needed for this operation." >&2 |
| 31 | + exit 1 |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# Function to check if the device is supported device with type spine routers and subtype UpstreamLC |
| 36 | +check_spine_router() { |
| 37 | + type=$(sonic-cfggen -d -v DEVICE_METADATA.localhost.type) |
| 38 | + sub_type=$(sonic-cfggen -d -v DEVICE_METADATA.localhost.sub_type) |
| 39 | + |
| 40 | + # only supported on spine routers and UpstreamLC |
| 41 | + if [[ "$type" != "SpineRouter" || "$sub_type" != "UpstreamLC" ]]; then |
| 42 | + echo "Operation is only supported on UpstreamLC of SpineRouter." >&2 |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | +} |
| 46 | + |
| 47 | +# Function to skip operation on chassis supervisor |
| 48 | +skip_chassis_supervisor() { |
| 49 | + if [ -f /etc/sonic/chassisdb.conf ]; then |
| 50 | + echo "Skipping Operation on chassis supervisor" |
| 51 | + exit 0 |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Function to validate the operation and prefix type parameters |
| 56 | +validate_operation() { |
| 57 | + local valid_operation=false |
| 58 | + local valid_prefix_type=false |
| 59 | + |
| 60 | + for operation in "${prefix_list_operations[@]}"; do |
| 61 | + if [[ "$1" == "$operation" ]]; then |
| 62 | + valid_operation=true |
| 63 | + break |
| 64 | + fi |
| 65 | + done |
| 66 | + |
| 67 | + if [ $valid_operation == false ]; then |
| 68 | + echo "Invalid parameter $1, Operation not supported" >&2 |
| 69 | + echo "" |
| 70 | + display_help |
| 71 | + exit 1 |
| 72 | + fi |
| 73 | + |
| 74 | + # Check if the prefix type is supported or not if the operation is not status |
| 75 | + if [ $1 != "status" ]; then |
| 76 | + for prefix_type in "${supported_prefix_types[@]}"; do |
| 77 | + if [[ "$2" == "$prefix_type" ]]; then |
| 78 | + valid_prefix_type=true |
| 79 | + break |
| 80 | + fi |
| 81 | + done |
| 82 | + |
| 83 | + if [ $valid_prefix_type == false ]; then |
| 84 | + echo "Invalid parameter $2, Prefix type not supported" >&2 |
| 85 | + echo "" |
| 86 | + display_help |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Function to handle prefix list operations for a specific ASIC |
| 93 | +handle_prefix_list_asic() { |
| 94 | + local asic=$1 |
| 95 | + local operation=$2 |
| 96 | + local PREFIX_TYPE=$3 |
| 97 | + local network=$4 |
| 98 | + local namespace_prefix='asic' |
| 99 | + |
| 100 | + if [ $operation == 'status' ] ; then |
| 101 | + echo "BGP$asic: Current prefix lists:" |
| 102 | + sonic-cfggen -d -v PREFIX_LIST -n $namespace_prefix$asic |
| 103 | + else |
| 104 | + if [ $operation == 'add' ]; then |
| 105 | + local prefix_list_entry="{\"PREFIX_LIST\":{\"$PREFIX_TYPE|$network\":{}}}" |
| 106 | + sonic-cfggen -a "$prefix_list_entry" -w -n $namespace_prefix$asic |
| 107 | + logger -t $operation -p user.info "Added prefix list: $PREFIX_TYPE with network: $network" |
| 108 | + echo "BGP$asic: Added prefix list: $PREFIX_TYPE with network: $network" |
| 109 | + elif [ $operation == 'remove' ]; then |
| 110 | + sonic-db-cli -n $namespace_prefix$asic CONFIG_DB DEL "PREFIX_LIST|$PREFIX_TYPE|$network" |
| 111 | + logger -t $operation -p user.info "Removed prefix list: $PREFIX_TYPE with network: $network" |
| 112 | + echo "BGP$asic: Removed prefix list: $PREFIX_TYPE with network: $network" |
| 113 | + fi |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +# Function to handle prefix list operations for a single ASIC |
| 118 | +handle_prefix_list_single() { |
| 119 | + local operation=$1 |
| 120 | + local PREFIX_TYPE=$2 |
| 121 | + local network=$3 |
| 122 | + |
| 123 | + if [ $operation == 'status' ] ; then |
| 124 | + echo "Current prefix lists:" |
| 125 | + sonic-cfggen -d -v PREFIX_LIST |
| 126 | + else |
| 127 | + if [ $operation == 'add' ]; then |
| 128 | + local prefix_list_entry="{\"PREFIX_LIST\":{\"$PREFIX_TYPE|$network\":{}}}" |
| 129 | + sonic-cfggen -a "$prefix_list_entry" -w |
| 130 | + logger -t $operation -p user.info "Added prefix list: $PREFIX_TYPE with network: $network" |
| 131 | + echo "Added prefix list: $PREFIX_TYPE with network: $network" |
| 132 | + elif [ $operation == 'remove' ]; then |
| 133 | + sonic-db-cli CONFIG_DB DEL "PREFIX_LIST|$PREFIX_TYPE|$network" |
| 134 | + logger -t $operation -p user.info "Removed prefix list: $PREFIX_TYPE with network: $network" |
| 135 | + echo "Removed prefix list: $PREFIX_TYPE with network: $network" |
| 136 | + fi |
| 137 | + fi |
| 138 | +} |
| 139 | + |
| 140 | +prefix_list_operations=("add" "remove" "status") |
| 141 | +supported_prefix_types=("ANCHOR_PREFIX") |
| 142 | +# Main script execution |
| 143 | +if [[ "$1" == "-h" || "$1" == "--help" ]]; then |
| 144 | + display_help |
| 145 | +fi |
| 146 | + |
| 147 | +check_root_privileges |
| 148 | +check_spine_router |
| 149 | +skip_chassis_supervisor |
| 150 | + |
| 151 | +validate_operation $1 $2 |
| 152 | + |
| 153 | +# Read SONiC immutable variables |
| 154 | +[ -f /etc/sonic/sonic-environment ] && . /etc/sonic/sonic-environment |
| 155 | + |
| 156 | +PLATFORM=${PLATFORM:-`sonic-cfggen -H -v DEVICE_METADATA.localhost.platform`} |
| 157 | + |
| 158 | +# Parse the device specific asic conf file, if it exists |
| 159 | +ASIC_CONF=/usr/share/sonic/device/$PLATFORM/asic.conf |
| 160 | +[ -f $ASIC_CONF ] && . $ASIC_CONF |
| 161 | + |
| 162 | +if [[ ($NUM_ASIC -gt 1) ]]; then |
| 163 | + asic=0 |
| 164 | + while [ $asic -lt $NUM_ASIC ] |
| 165 | + do |
| 166 | + sub_role=`sonic-cfggen -d -v "DEVICE_METADATA['localhost']['sub_role']" -n asic$asic` |
| 167 | + if [ $sub_role == 'FrontEnd' ]; then |
| 168 | + handle_prefix_list_asic $asic $1 $2 $3 |
| 169 | + fi |
| 170 | + asic=$((asic+1)) |
| 171 | + done |
| 172 | +else |
| 173 | + handle_prefix_list_single $1 $2 $3 |
| 174 | +fi |
| 175 | + |
| 176 | +if [ $1 != 'status' ]; then |
| 177 | + echo "Please execute 'sudo config save' to preserve prefix list after reboot or config reload" |
| 178 | +fi |
0 commit comments