22
22
23
23
SONIC_CFGGEN_PATH = '/usr/local/bin/sonic-cfggen'
24
24
SYSLOG_IDENTIFIER = "config"
25
+ VLAN_SUB_INTERFACE_SEPARATOR = '.'
25
26
26
27
# ========================== Syslog wrappers ==========================
27
28
@@ -86,16 +87,26 @@ def interface_alias_to_name(interface_alias):
86
87
config_db .connect ()
87
88
port_dict = config_db .get_table ('PORT' )
88
89
90
+ vlan_id = ""
91
+ sub_intf_sep_idx = - 1
92
+ if interface_alias is not None :
93
+ sub_intf_sep_idx = interface_alias .find (VLAN_SUB_INTERFACE_SEPARATOR )
94
+ if sub_intf_sep_idx != - 1 :
95
+ vlan_id = interface_alias [sub_intf_sep_idx + 1 :]
96
+ # interface_alias holds the parent port name so the subsequent logic still applies
97
+ interface_alias = interface_alias [:sub_intf_sep_idx ]
98
+
89
99
if interface_alias is not None :
90
100
if not port_dict :
91
101
click .echo ("port_dict is None!" )
92
102
raise click .Abort ()
93
103
for port_name in port_dict .keys ():
94
104
if interface_alias == port_dict [port_name ]['alias' ]:
95
- return port_name
105
+ return port_name if sub_intf_sep_idx == - 1 else port_name + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id
96
106
97
- # Interface alias not in port_dict, just return interface_alias
98
- return interface_alias
107
+ # Interface alias not in port_dict, just return interface_alias, e.g.,
108
+ # portchannel is passed in as argument, which does not have an alias
109
+ return interface_alias if sub_intf_sep_idx == - 1 else interface_alias + VLAN_SUB_INTERFACE_SEPARATOR + vlan_id
99
110
100
111
101
112
def interface_name_is_valid (interface_name ):
@@ -105,6 +116,7 @@ def interface_name_is_valid(interface_name):
105
116
config_db .connect ()
106
117
port_dict = config_db .get_table ('PORT' )
107
118
port_channel_dict = config_db .get_table ('PORTCHANNEL' )
119
+ sub_port_intf_dict = config_db .get_table ('VLAN_SUB_INTERFACE' )
108
120
109
121
if get_interface_naming_mode () == "alias" :
110
122
interface_name = interface_alias_to_name (interface_name )
@@ -120,6 +132,10 @@ def interface_name_is_valid(interface_name):
120
132
for port_channel_name in port_channel_dict .keys ():
121
133
if interface_name == port_channel_name :
122
134
return True
135
+ if sub_port_intf_dict :
136
+ for sub_port_intf_name in sub_port_intf_dict .keys ():
137
+ if interface_name == sub_port_intf_name :
138
+ return True
123
139
return False
124
140
125
141
def interface_name_to_alias (interface_name ):
@@ -1037,9 +1053,15 @@ def startup(ctx, interface_name):
1037
1053
ctx .fail ("Interface name is invalid. Please enter a valid interface name!!" )
1038
1054
1039
1055
if interface_name .startswith ("Ethernet" ):
1040
- config_db .mod_entry ("PORT" , interface_name , {"admin_status" : "up" })
1056
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1057
+ config_db .mod_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "up" })
1058
+ else :
1059
+ config_db .mod_entry ("PORT" , interface_name , {"admin_status" : "up" })
1041
1060
elif interface_name .startswith ("PortChannel" ):
1042
- config_db .mod_entry ("PORTCHANNEL" , interface_name , {"admin_status" : "up" })
1061
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1062
+ config_db .mod_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "up" })
1063
+ else :
1064
+ config_db .mod_entry ("PORTCHANNEL" , interface_name , {"admin_status" : "up" })
1043
1065
#
1044
1066
# 'shutdown' subcommand
1045
1067
#
@@ -1059,9 +1081,15 @@ def shutdown(ctx, interface_name):
1059
1081
ctx .fail ("Interface name is invalid. Please enter a valid interface name!!" )
1060
1082
1061
1083
if interface_name .startswith ("Ethernet" ):
1062
- config_db .mod_entry ("PORT" , interface_name , {"admin_status" : "down" })
1084
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1085
+ config_db .mod_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "down" })
1086
+ else :
1087
+ config_db .mod_entry ("PORT" , interface_name , {"admin_status" : "down" })
1063
1088
elif interface_name .startswith ("PortChannel" ):
1064
- config_db .mod_entry ("PORTCHANNEL" , interface_name , {"admin_status" : "down" })
1089
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1090
+ config_db .mod_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "down" })
1091
+ else :
1092
+ config_db .mod_entry ("PORTCHANNEL" , interface_name , {"admin_status" : "down" })
1065
1093
1066
1094
#
1067
1095
# 'speed' subcommand
@@ -1113,11 +1141,19 @@ def add(ctx, interface_name, ip_addr):
1113
1141
try :
1114
1142
ipaddress .ip_network (unicode (ip_addr ), strict = False )
1115
1143
if interface_name .startswith ("Ethernet" ):
1116
- config_db .set_entry ("INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1117
- config_db .set_entry ("INTERFACE" , interface_name , {"NULL" : "NULL" })
1144
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1145
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "up" })
1146
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1147
+ else :
1148
+ config_db .set_entry ("INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1149
+ config_db .set_entry ("INTERFACE" , interface_name , {"NULL" : "NULL" })
1118
1150
elif interface_name .startswith ("PortChannel" ):
1119
- config_db .set_entry ("PORTCHANNEL_INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1120
- config_db .set_entry ("PORTCHANNEL_INTERFACE" , interface_name , {"NULL" : "NULL" })
1151
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1152
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , interface_name , {"admin_status" : "up" })
1153
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1154
+ else :
1155
+ config_db .set_entry ("PORTCHANNEL_INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1156
+ config_db .set_entry ("PORTCHANNEL_INTERFACE" , interface_name , {"NULL" : "NULL" })
1121
1157
elif interface_name .startswith ("Vlan" ):
1122
1158
config_db .set_entry ("VLAN_INTERFACE" , (interface_name , ip_addr ), {"NULL" : "NULL" })
1123
1159
config_db .set_entry ("VLAN_INTERFACE" , interface_name , {"NULL" : "NULL" })
@@ -1148,11 +1184,19 @@ def remove(ctx, interface_name, ip_addr):
1148
1184
try :
1149
1185
ipaddress .ip_network (unicode (ip_addr ), strict = False )
1150
1186
if interface_name .startswith ("Ethernet" ):
1151
- config_db .set_entry ("INTERFACE" , (interface_name , ip_addr ), None )
1152
- if_table = "INTERFACE"
1187
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1188
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , (interface_name , ip_addr ), None )
1189
+ if_table = "VLAN_SUB_INTERFACE"
1190
+ else :
1191
+ config_db .set_entry ("INTERFACE" , (interface_name , ip_addr ), None )
1192
+ if_table = "INTERFACE"
1153
1193
elif interface_name .startswith ("PortChannel" ):
1154
- config_db .set_entry ("PORTCHANNEL_INTERFACE" , (interface_name , ip_addr ), None )
1155
- if_table = "PORTCHANNEL_INTERFACE"
1194
+ if VLAN_SUB_INTERFACE_SEPARATOR in interface_name :
1195
+ config_db .set_entry ("VLAN_SUB_INTERFACE" , (interface_name , ip_addr ), None )
1196
+ if_table = "VLAN_SUB_INTERFACE"
1197
+ else :
1198
+ config_db .set_entry ("PORTCHANNEL_INTERFACE" , (interface_name , ip_addr ), None )
1199
+ if_table = "PORTCHANNEL_INTERFACE"
1156
1200
elif interface_name .startswith ("Vlan" ):
1157
1201
config_db .set_entry ("VLAN_INTERFACE" , (interface_name , ip_addr ), None )
1158
1202
if_table = "VLAN_INTERFACE"
0 commit comments