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