1
1
#!/usr/bin/env python
2
+ from __future__ import print_function
2
3
import calendar
3
4
import math
4
5
import os
@@ -124,13 +125,13 @@ def parse_png(png, hname):
124
125
bandwidth_node = link .find (str (QName (ns , "Bandwidth" )))
125
126
bandwidth = bandwidth_node .text if bandwidth_node is not None else None
126
127
if enddevice .lower () == hname .lower ():
127
- if port_alias_map . has_key ( endport ) :
128
+ if endport in port_alias_map :
128
129
endport = port_alias_map [endport ]
129
130
neighbors [endport ] = {'name' : startdevice , 'port' : startport }
130
131
if bandwidth :
131
132
port_speeds [endport ] = bandwidth
132
133
elif startdevice .lower () == hname .lower ():
133
- if port_alias_map . has_key ( startport ) :
134
+ if startport in port_alias_map :
134
135
startport = port_alias_map [startport ]
135
136
neighbors [startport ] = {'name' : enddevice , 'port' : endport }
136
137
if bandwidth :
@@ -175,14 +176,14 @@ def parse_asic_external_link(link, asic_name, hostname):
175
176
# if chassis internal is false, the interface name will be
176
177
# interface alias which should be converted to asic port name
177
178
if (enddevice .lower () == hostname .lower ()):
178
- if ((port_alias_asic_map . has_key ( endport ) ) and
179
+ if ((endport in port_alias_asic_map ) and
179
180
(asic_name .lower () in port_alias_asic_map [endport ].lower ())):
180
181
endport = port_alias_asic_map [endport ]
181
182
neighbors [port_alias_map [endport ]] = {'name' : startdevice , 'port' : startport }
182
183
if bandwidth :
183
184
port_speeds [port_alias_map [endport ]] = bandwidth
184
185
elif (startdevice .lower () == hostname .lower ()):
185
- if ((port_alias_asic_map . has_key ( startport ) ) and
186
+ if ((startport in port_alias_asic_map ) and
186
187
(asic_name .lower () in port_alias_asic_map [startport ].lower ())):
187
188
startport = port_alias_asic_map [startport ]
188
189
neighbors [port_alias_map [startport ]] = {'name' : enddevice , 'port' : endport }
@@ -202,14 +203,14 @@ def parse_asic_internal_link(link, asic_name, hostname):
202
203
bandwidth = bandwidth_node .text if bandwidth_node is not None else None
203
204
if ((enddevice .lower () == asic_name .lower ()) and
204
205
(startdevice .lower () != hostname .lower ())):
205
- if port_alias_map . has_key ( endport ) :
206
+ if endport in port_alias_map :
206
207
endport = port_alias_map [endport ]
207
208
neighbors [endport ] = {'name' : startdevice , 'port' : startport }
208
209
if bandwidth :
209
210
port_speeds [endport ] = bandwidth
210
211
elif ((startdevice .lower () == asic_name .lower ()) and
211
212
(enddevice .lower () != hostname .lower ())):
212
- if port_alias_map . has_key ( startport ) :
213
+ if startport in port_alias_map :
213
214
startport = port_alias_map [startport ]
214
215
neighbors [startport ] = {'name' : enddevice , 'port' : endport }
215
216
if bandwidth :
@@ -288,7 +289,7 @@ def parse_dpg(dpg, hname):
288
289
if vni_element .text .isdigit ():
289
290
vni = int (vni_element .text )
290
291
else :
291
- print >> sys . stderr , "VNI must be an integer (use default VNI %d instead)" % vni_default
292
+ print ( "VNI must be an integer (use default VNI %d instead)" % vni_default , file = sys . stderr )
292
293
293
294
ipintfs = child .find (str (QName (ns , "IPInterfaces" )))
294
295
intfs = {}
@@ -391,18 +392,18 @@ def parse_dpg(dpg, hname):
391
392
# decide an ACL is a Control Plane ACL if acl_intfs is empty below.
392
393
for member in aclattach :
393
394
member = member .strip ()
394
- if pcs . has_key ( member ) :
395
+ if member in pcs :
395
396
# If try to attach ACL to a LAG interface then we shall add the LAG to
396
397
# to acl_intfs directly instead of break it into member ports, ACL attach
397
398
# to LAG will be applied to all the LAG members internally by SAI/SDK
398
399
acl_intfs .append (member )
399
- elif vlans . has_key ( member ) :
400
+ elif member in vlans :
400
401
acl_intfs .append (member )
401
- elif port_alias_map . has_key ( member ) :
402
+ elif member in port_alias_map :
402
403
acl_intfs .append (port_alias_map [member ])
403
404
# Give a warning if trying to attach ACL to a LAG member interface, correct way is to attach ACL to the LAG interface
404
405
if port_alias_map [member ] in intfs_inpc :
405
- print >> sys . stderr , "Warning: ACL " + aclname + " is attached to a LAG member interface " + port_alias_map [member ] + ", instead of LAG interface"
406
+ print ( "Warning: ACL " + aclname + " is attached to a LAG member interface " + port_alias_map [member ] + ", instead of LAG interface" , file = sys . stderr )
406
407
elif member .lower ().startswith ('erspan' ) or member .lower ().startswith ('egress_erspan' ):
407
408
if member .lower ().startswith ('erspanv6' ) or member .lower ().startswith ('egress_erspanv6' ):
408
409
is_mirror_v6 = True
@@ -439,9 +440,9 @@ def parse_dpg(dpg, hname):
439
440
# append the service to our list of services
440
441
if aclname in acls :
441
442
if acls [aclname ]['type' ] != 'CTRLPLANE' :
442
- print >> sys . stderr , "Warning: ACL '%s' type mismatch. Not updating ACL." % aclname
443
+ print ( "Warning: ACL '%s' type mismatch. Not updating ACL." % aclname , file = sys . stderr )
443
444
elif acls [aclname ]['services' ] == aclservice :
444
- print >> sys . stderr , "Warning: ACL '%s' already contains service '%s'. Not updating ACL." % (aclname , aclservice )
445
+ print ( "Warning: ACL '%s' already contains service '%s'. Not updating ACL." % (aclname , aclservice ), file = sys . stderr )
445
446
else :
446
447
acls [aclname ]['services' ].append (aclservice )
447
448
else :
@@ -450,7 +451,7 @@ def parse_dpg(dpg, hname):
450
451
'stage' : stage ,
451
452
'services' : [aclservice ]}
452
453
except :
453
- print >> sys . stderr , "Warning: Ignoring Control Plane ACL %s without type" % aclname
454
+ print ( "Warning: Ignoring Control Plane ACL %s without type" % aclname , file = sys . stderr )
454
455
455
456
return intfs , lo_intfs , mvrf , mgmt_intf , vlans , vlan_members , pcs , pc_members , acls , vni
456
457
return None , None , None , None , None , None , None , None , None , None
@@ -530,8 +531,8 @@ def parse_cpg(cpg, hname):
530
531
if hostname .lower () == bgp_session ['name' ].lower ():
531
532
bgp_session ['asn' ] = asn
532
533
533
- bgp_monitors = { key : bgp_sessions [key ] for key in bgp_sessions if bgp_sessions [key ]. has_key ( 'asn' ) and bgp_sessions [key ]['name' ] == 'BGPMonitor' }
534
- bgp_sessions = { key : bgp_sessions [key ] for key in bgp_sessions if bgp_sessions [key ]. has_key ( 'asn' ) and int (bgp_sessions [key ]['asn' ]) != 0 }
534
+ bgp_monitors = { key : bgp_sessions [key ] for key in bgp_sessions if 'asn' in bgp_sessions [key ] and bgp_sessions [key ]['name' ] == 'BGPMonitor' }
535
+ bgp_sessions = { key : bgp_sessions [key ] for key in bgp_sessions if 'asn' in bgp_sessions [key ] and int (bgp_sessions [key ]['asn' ]) != 0 }
535
536
536
537
return bgp_sessions , myasn , bgp_peers_with_range , bgp_monitors
537
538
@@ -696,7 +697,7 @@ def parse_spine_chassis_fe(results, vni, lo_intfs, phyport_intfs, pc_intfs, pc_m
696
697
break
697
698
698
699
if intf_name == None :
699
- print >> sys . stderr , 'Warning: cannot find any interfaces that belong to %s' % (pc_intf )
700
+ print ( 'Warning: cannot find any interfaces that belong to %s' % (pc_intf ), file = sys . stderr )
700
701
continue
701
702
702
703
# Get the neighbor router of this port channel interface
@@ -734,7 +735,7 @@ def filter_acl_table_bindings(acls, neighbors, port_channels, sub_role):
734
735
if not backend_port_channel :
735
736
front_port_channel_intf .append (port_channel_intf )
736
737
737
- for acl_table , group_params in acls .iteritems ():
738
+ for acl_table , group_params in acls .items ():
738
739
group_type = group_params .get ('type' , None )
739
740
filter_acls [acl_table ] = acls [acl_table ]
740
741
@@ -763,7 +764,7 @@ def filter_acl_table_bindings(acls, neighbors, port_channels, sub_role):
763
764
active_ports = [port for port in front_panel_ports if port in neighbors .keys () or port in front_port_channel_intf ]
764
765
765
766
if not active_ports :
766
- print >> sys . stderr , 'Warning: mirror table {} in ACL_TABLE does not have any ports bound to it' .format (acl_table )
767
+ print ( 'Warning: mirror table {} in ACL_TABLE does not have any ports bound to it' .format (acl_table ), file = sys . stderr )
767
768
768
769
filter_acls [acl_table ]['ports' ] = active_ports
769
770
@@ -921,14 +922,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
921
922
results ['BGP_PEER_RANGE' ] = bgp_peers_with_range
922
923
if mgmt_routes :
923
924
# TODO: differentiate v4 and v6
924
- mgmt_intf .itervalues ( ).next ()['forced_mgmt_routes' ] = mgmt_routes
925
+ iter ( mgmt_intf .values () ).next ()['forced_mgmt_routes' ] = mgmt_routes
925
926
results ['MGMT_PORT' ] = {}
926
927
results ['MGMT_INTERFACE' ] = {}
927
928
mgmt_intf_count = 0
928
929
mgmt_alias_reverse_mapping = {}
929
930
for key in mgmt_intf :
930
931
alias = key [0 ]
931
- if mgmt_alias_reverse_mapping . has_key ( alias ) :
932
+ if alias in mgmt_alias_reverse_mapping :
932
933
name = mgmt_alias_reverse_mapping [alias ]
933
934
else :
934
935
name = 'eth' + str (mgmt_intf_count )
@@ -953,14 +954,14 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
953
954
phyport_intfs = {}
954
955
vlan_intfs = {}
955
956
pc_intfs = {}
956
- vlan_invert_mapping = { v ['alias' ]:k for k ,v in vlans .items () if v . has_key ( 'alias' ) }
957
+ vlan_invert_mapping = { v ['alias' ]:k for k ,v in vlans .items () if 'alias' in v }
957
958
vlan_sub_intfs = {}
958
959
959
960
for intf in intfs :
960
961
if intf [0 ][0 :4 ] == 'Vlan' :
961
962
vlan_intfs [intf ] = {}
962
963
vlan_intfs [intf [0 ]] = {}
963
- elif vlan_invert_mapping . has_key ( intf [0 ]) :
964
+ elif intf [0 ] in vlan_invert_mapping :
964
965
vlan_intfs [(vlan_invert_mapping [intf [0 ]], intf [1 ])] = {}
965
966
vlan_intfs [vlan_invert_mapping [intf [0 ]]] = {}
966
967
elif intf [0 ][0 :11 ] == 'PortChannel' :
@@ -975,7 +976,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
975
976
976
977
for port_name in port_speeds_default :
977
978
# ignore port not in port_config.ini
978
- if not ports . has_key ( port_name ) :
979
+ if port_name not in ports :
979
980
continue
980
981
981
982
ports .setdefault (port_name , {})['speed' ] = port_speeds_default [port_name ]
@@ -985,12 +986,12 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
985
986
# If no port_config_file is found ports is empty so ignore this error
986
987
if port_config_file is not None :
987
988
if port_name not in ports :
988
- print >> sys . stderr , "Warning: ignore interface '%s' as it is not in the port_config.ini" % port_name
989
+ print ( "Warning: ignore interface '%s' as it is not in the port_config.ini" % port_name , file = sys . stderr )
989
990
continue
990
991
991
992
ports .setdefault (port_name , {})['speed' ] = port_speed_png [port_name ]
992
993
993
- for port_name , port in ports .items ():
994
+ for port_name , port in list ( ports .items () ):
994
995
# get port alias from port_config.ini
995
996
alias = port .get ('alias' , port_name )
996
997
# generate default 100G FEC
@@ -1001,34 +1002,34 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
1001
1002
# set port description if parsed from deviceinfo
1002
1003
for port_name in port_descriptions :
1003
1004
# ignore port not in port_config.ini
1004
- if not ports . has_key ( port_name ) :
1005
+ if port_name not in ports :
1005
1006
continue
1006
1007
1007
1008
ports .setdefault (port_name , {})['description' ] = port_descriptions [port_name ]
1008
1009
1009
1010
for port_name , port in ports .items ():
1010
1011
if not port .get ('description' ):
1011
- if neighbors . has_key ( port_name ) :
1012
+ if port_name in neighbors :
1012
1013
# for the ports w/o description set it to neighbor name:port
1013
1014
port ['description' ] = "%s:%s" % (neighbors [port_name ]['name' ], neighbors [port_name ]['port' ])
1014
1015
else :
1015
1016
# for the ports w/o neighbor info, set it to port alias
1016
1017
port ['description' ] = port .get ('alias' , port_name )
1017
1018
1018
1019
# set default port MTU as 9100
1019
- for port in ports .itervalues ():
1020
+ for port in ports .values ():
1020
1021
port ['mtu' ] = '9100'
1021
1022
1022
1023
# asymmetric PFC is disabled by default
1023
- for port in ports .itervalues ():
1024
+ for port in ports .values ():
1024
1025
port ['pfc_asym' ] = 'off'
1025
1026
1026
1027
# set physical port default admin status up
1027
1028
for port in phyport_intfs :
1028
1029
if port [0 ] in ports :
1029
1030
ports .get (port [0 ])['admin_status' ] = 'up'
1030
1031
1031
- for member in pc_members .keys () + vlan_members .keys ():
1032
+ for member in list ( pc_members .keys ()) + list ( vlan_members .keys () ):
1032
1033
port = ports .get (member [1 ])
1033
1034
if port :
1034
1035
port ['admin_status' ] = 'up'
@@ -1047,11 +1048,11 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
1047
1048
# remove portchannels that contain ports not existing in port_config.ini
1048
1049
# when port_config.ini exists
1049
1050
if not set (mbr_map ['members' ]).issubset (port_set ):
1050
- print >> sys . stderr , "Warning: ignore '%s' as part of its member interfaces is not in the port_config.ini" % pc_name
1051
+ print ( "Warning: ignore '%s' as part of its member interfaces is not in the port_config.ini" % pc_name , file = sys . stderr )
1051
1052
del pcs [pc_name ]
1052
1053
1053
1054
# set default port channel MTU as 9100 and admin status up
1054
- for pc in pcs .itervalues ():
1055
+ for pc in pcs .values ():
1055
1056
pc ['mtu' ] = '9100'
1056
1057
pc ['admin_status' ] = 'up'
1057
1058
@@ -1061,7 +1062,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
1061
1062
for pc_intf in pc_intfs .keys ():
1062
1063
# remove portchannels not in PORTCHANNEL dictionary
1063
1064
if isinstance (pc_intf , tuple ) and pc_intf [0 ] not in pcs :
1064
- print >> sys . stderr , "Warning: ignore '%s' interface '%s' as '%s' is not in the valid PortChannel list" % (pc_intf [0 ], pc_intf [1 ], pc_intf [0 ])
1065
+ print ( "Warning: ignore '%s' interface '%s' as '%s' is not in the valid PortChannel list" % (pc_intf [0 ], pc_intf [1 ], pc_intf [0 ]), file = sys . stderr )
1065
1066
del pc_intfs [pc_intf ]
1066
1067
pc_intfs .pop (pc_intf [0 ], None )
1067
1068
@@ -1100,7 +1101,7 @@ def parse_xml(filename, platform=None, port_config_file=None, asic_name=None, hw
1100
1101
# remove port not in port_config.ini
1101
1102
if nghbr not in ports :
1102
1103
if port_config_file is not None :
1103
- print >> sys . stderr , "Warning: ignore interface '%s' in DEVICE_NEIGHBOR as it is not in the port_config.ini" % nghbr
1104
+ print ( "Warning: ignore interface '%s' in DEVICE_NEIGHBOR as it is not in the port_config.ini" % nghbr , file = sys . stderr )
1104
1105
del neighbors [nghbr ]
1105
1106
results ['DEVICE_NEIGHBOR' ] = neighbors
1106
1107
if asic_name is None :
@@ -1198,4 +1199,4 @@ def parse_asic_sub_role(filename, asic_name):
1198
1199
1199
1200
def print_parse_xml (filename ):
1200
1201
results = parse_xml (filename )
1201
- print (json .dumps (results , indent = 3 , cls = minigraph_encoder ))
1202
+ print (( json .dumps (results , indent = 3 , cls = minigraph_encoder ) ))
0 commit comments