@@ -6760,73 +6760,82 @@ def is_subintf_shortname(intf):
67606760@click .argument ('vid' , metavar = '<vid>' , required = False , type = click .IntRange (1 ,4094 ))
67616761@click .pass_context
67626762def add_subinterface (ctx , subinterface_name , vid ):
6763+ config_db = ValidatedConfigDBConnector (ctx .obj ['db' ])
67636764 sub_intf_sep_idx = subinterface_name .find (VLAN_SUB_INTERFACE_SEPARATOR )
6764- if sub_intf_sep_idx == - 1 :
6765- ctx .fail ("{} is invalid vlan subinterface" .format (subinterface_name ))
6766-
67676765 interface_alias = subinterface_name [:sub_intf_sep_idx ]
6768- if interface_alias is None :
6769- ctx .fail ("{} invalid subinterface" .format (interface_alias ))
6770-
6771- if interface_alias .startswith ("Po" ) is True :
6772- intf_table_name = CFG_PORTCHANNEL_PREFIX
6773- elif interface_alias .startswith ("Eth" ) is True :
6774- intf_table_name = 'PORT'
6775-
6776- config_db = ctx .obj ['db' ]
6777- port_dict = config_db .get_table (intf_table_name )
6778- parent_intf = get_intf_longname (interface_alias )
6779- if interface_alias is not None :
6780- if not port_dict :
6781- ctx .fail ("{} parent interface not found. {} table none" .format (interface_alias , intf_table_name ))
6782- if parent_intf not in port_dict .keys ():
6783- ctx .fail ("{} parent interface not found" .format (subinterface_name ))
6784-
6785- # Validate if parent is portchannel member
6786- portchannel_member_table = config_db .get_table ('PORTCHANNEL_MEMBER' )
6787- if interface_is_in_portchannel (portchannel_member_table , parent_intf ):
6788- ctx .fail ("{} is configured as a member of portchannel. Cannot configure subinterface"
6789- .format (parent_intf ))
6766+ if ADHOC_VALIDATION :
6767+ if sub_intf_sep_idx == - 1 :
6768+ ctx .fail ("{} is invalid vlan subinterface" .format (subinterface_name ))
67906769
6791- # Validate if parent is vlan member
6792- vlan_member_table = config_db .get_table ('VLAN_MEMBER' )
6793- if interface_is_in_vlan (vlan_member_table , parent_intf ):
6794- ctx .fail ("{} is configured as a member of vlan. Cannot configure subinterface"
6795- .format (parent_intf ))
6770+ if interface_alias is None :
6771+ ctx .fail ("{} invalid subinterface" .format (interface_alias ))
67966772
6797- sub_intfs = [k for k ,v in config_db .get_table ('VLAN_SUB_INTERFACE' ).items () if type (k ) != tuple ]
6798- if subinterface_name in sub_intfs :
6799- ctx .fail ("{} already exists" .format (subinterface_name ))
6773+ if interface_alias .startswith ("Po" ) is True :
6774+ intf_table_name = CFG_PORTCHANNEL_PREFIX
6775+ elif interface_alias .startswith ("Eth" ) is True :
6776+ intf_table_name = 'PORT'
6777+ else :
6778+ ctx .fail ("{} is invalid vlan subinterface" .format (subinterface_name ))
6779+
6780+ port_dict = config_db .get_table (intf_table_name )
6781+ parent_intf = get_intf_longname (interface_alias )
6782+ if interface_alias is not None :
6783+ if not port_dict :
6784+ ctx .fail ("{} parent interface not found. {} table none" .format (interface_alias , intf_table_name ))
6785+ if parent_intf not in port_dict .keys ():
6786+ ctx .fail ("{} parent interface not found" .format (subinterface_name ))
6787+
6788+ # Validate if parent is portchannel member
6789+ portchannel_member_table = config_db .get_table ('PORTCHANNEL_MEMBER' )
6790+ if interface_is_in_portchannel (portchannel_member_table , parent_intf ): # TODO: MISSING CONSTRAINT IN YANG MODEL
6791+ ctx .fail ("{} is configured as a member of portchannel. Cannot configure subinterface"
6792+ .format (parent_intf ))
6793+
6794+ # Validate if parent is vlan member
6795+ vlan_member_table = config_db .get_table ('VLAN_MEMBER' )
6796+ if interface_is_in_vlan (vlan_member_table , parent_intf ): # TODO: MISSING CONSTRAINT IN YANG MODEL
6797+ ctx .fail ("{} is configured as a member of vlan. Cannot configure subinterface"
6798+ .format (parent_intf ))
6799+
6800+ sub_intfs = [k for k ,v in config_db .get_table ('VLAN_SUB_INTERFACE' ).items () if type (k ) != tuple ]
6801+ if subinterface_name in sub_intfs :
6802+ ctx .fail ("{} already exists" .format (subinterface_name )) # TODO: MISSING CONSTRAINT IN YANG MODEL
6803+
6804+ if subintf_vlan_check (config_db , get_intf_longname (interface_alias ), vid ) is True :
6805+ ctx .fail ("Vlan {} encap already configured on other subinterface on {}" .format (vid , interface_alias )) # TODO: MISSING CONSTRAINT IN YANG MODEL
6806+
6807+ if vid is None and is_subintf_shortname (subinterface_name ):
6808+ ctx .fail ("{} Encap vlan is mandatory or short name subinterfaces" .format (subinterface_name )) # TODO: MISSING CONSTRAINT IN YANG MODEL
68006809
68016810 subintf_dict = {}
68026811 if vid is not None :
68036812 subintf_dict .update ({"vlan" : vid })
6804- elif is_subintf_shortname (subinterface_name ):
6805- ctx .fail ("{} Encap vlan is mandatory for short name subinterfaces" .format (subinterface_name ))
6806-
6807- if subintf_vlan_check (config_db , get_intf_longname (interface_alias ), vid ) is True :
6808- ctx .fail ("Vlan {} encap already configured on other subinterface on {}" .format (vid , interface_alias ))
6809-
68106813 subintf_dict .update ({"admin_status" : "up" })
6811- config_db .set_entry ('VLAN_SUB_INTERFACE' , subinterface_name , subintf_dict )
6814+
6815+ try :
6816+ config_db .set_entry ('VLAN_SUB_INTERFACE' , subinterface_name , subintf_dict )
6817+ except ValueError as e :
6818+ ctx .fail ("Invalid vlan subinterface. Error: {}" .format (e ))
68126819
68136820@subinterface .command ('del' )
68146821@click .argument ('subinterface_name' , metavar = '<subinterface_name>' , required = True )
68156822@click .pass_context
68166823def del_subinterface (ctx , subinterface_name ):
6817- sub_intf_sep_idx = subinterface_name .find (VLAN_SUB_INTERFACE_SEPARATOR )
6818- if sub_intf_sep_idx == - 1 :
6819- ctx .fail ("{} is invalid vlan subinterface" .format (subinterface_name ))
6824+ config_db = ValidatedConfigDBConnector (ctx .obj ['db' ])
68206825
6821- config_db = ctx .obj ['db' ]
6822- #subinterface_name = subintf_get_shortname(subinterface_name)
6823- if interface_name_is_valid (config_db , subinterface_name ) is False :
6824- ctx .fail ("{} is invalid " .format (subinterface_name ))
6826+ if ADHOC_VALIDATION :
6827+ sub_intf_sep_idx = subinterface_name .find (VLAN_SUB_INTERFACE_SEPARATOR )
6828+ if sub_intf_sep_idx == - 1 :
6829+ ctx .fail ("{} is invalid vlan subinterface" .format (subinterface_name ))
6830+
6831+ #subinterface_name = subintf_get_shortname(subinterface_name)
6832+ if interface_name_is_valid (config_db , subinterface_name ) is False :
6833+ ctx .fail ("{} is invalid " .format (subinterface_name ))
68256834
6826- subintf_config_db = config_db .get_table ('VLAN_SUB_INTERFACE' )
6827- sub_intfs = [k for k ,v in subintf_config_db .items () if type (k ) != tuple ]
6828- if subinterface_name not in sub_intfs :
6829- ctx .fail ("{} does not exists" .format (subinterface_name ))
6835+ subintf_config_db = config_db .get_table ('VLAN_SUB_INTERFACE' )
6836+ sub_intfs = [k for k ,v in subintf_config_db .items () if type (k ) != tuple ]
6837+ if subinterface_name not in sub_intfs :
6838+ ctx .fail ("{} does not exists" .format (subinterface_name ))
68306839
68316840 ips = {}
68326841 ips = [ k [1 ] for k in config_db .get_table ('VLAN_SUB_INTERFACE' ) if type (k ) == tuple and k [0 ] == subinterface_name ]
@@ -6842,7 +6851,10 @@ def del_subinterface(ctx, subinterface_name):
68426851 for ip in ips :
68436852 config_db .set_entry ('INTERFACE' , (subinterface_name , ip ), None )
68446853
6845- config_db .set_entry ('VLAN_SUB_INTERFACE' , subinterface_name , None )
6854+ try :
6855+ config_db .set_entry ('VLAN_SUB_INTERFACE' , subinterface_name , None )
6856+ except JsonPatchConflict as e :
6857+ ctx .fail ("{} is invalid vlan subinterface. Error: {}" .format (subinterface_name , e ))
68466858
68476859if __name__ == '__main__' :
68486860 config ()
0 commit comments