1
- #!/usr/bin/env python
1
+ #!/usr/bin/env python3
2
2
3
3
import click
4
- import ipaddr
4
+ import ipaddress
5
5
import json
6
6
import syslog
7
7
@@ -30,7 +30,7 @@ def error(msg):
30
30
31
31
32
32
def deep_update (dst , src ):
33
- for key , value in src .iteritems ():
33
+ for key , value in src .items ():
34
34
if isinstance (value , dict ):
35
35
node = dst .setdefault (key , {})
36
36
deep_update (node , value )
@@ -182,7 +182,7 @@ def read_policers_info(self):
182
182
# For multi-npu platforms we will read from any one of front asic namespace
183
183
# config db as the information should be same across all config db
184
184
if self .per_npu_configdb :
185
- namespace_configdb = (self .per_npu_configdb .values ())[0 ]
185
+ namespace_configdb = (list ( self .per_npu_configdb .values () ))[0 ]
186
186
self .policers_db_info = namespace_configdb .get_table (self .POLICER )
187
187
else :
188
188
self .policers_db_info = self .configdb .get_table (self .POLICER )
@@ -199,19 +199,19 @@ def read_sessions_info(self):
199
199
# For multi-npu platforms we will read from any one of front asic namespace
200
200
# config db as the information should be same across all config db
201
201
if self .per_npu_configdb :
202
- namespace_configdb = (self .per_npu_configdb .values ())[0 ]
202
+ namespace_configdb = (list ( self .per_npu_configdb .values () ))[0 ]
203
203
self .sessions_db_info = namespace_configdb .get_table (self .CFG_MIRROR_SESSION_TABLE )
204
204
else :
205
205
self .sessions_db_info = self .configdb .get_table (self .CFG_MIRROR_SESSION_TABLE )
206
- for key in self .sessions_db_info .keys ():
206
+ for key in list ( self .sessions_db_info .keys () ):
207
207
if self .per_npu_statedb :
208
208
# For multi-npu platforms we will read from all front asic name space
209
209
# statedb as the monitor port will be differnt for each asic
210
210
# and it's status also might be different (ideally should not happen)
211
211
# We will store them as dict of 'asic' : value
212
212
self .sessions_db_info [key ]["status" ] = {}
213
213
self .sessions_db_info [key ]["monitor_port" ] = {}
214
- for namespace_key , namespace_statedb in self .per_npu_statedb .iteritems ():
214
+ for namespace_key , namespace_statedb in self .per_npu_statedb .items ():
215
215
state_db_info = namespace_statedb .get_all (self .statedb .STATE_DB , "{}|{}" .format (self .STATE_MIRROR_SESSION_TABLE , key ))
216
216
self .sessions_db_info [key ]["status" ][namespace_key ] = state_db_info .get ("status" , "inactive" ) if state_db_info else "error"
217
217
self .sessions_db_info [key ]["monitor_port" ][namespace_key ] = state_db_info .get ("monitor_port" , "" ) if state_db_info else ""
@@ -367,7 +367,7 @@ def validate_actions(self, table_name, action_props):
367
367
# For multi-npu we will read using anyone statedb connector for front asic namespace.
368
368
# Same information should be there in all state DB's
369
369
# as it is static information about switch capability
370
- namespace_statedb = (self .per_npu_statedb .values ())[0 ]
370
+ namespace_statedb = (list ( self .per_npu_statedb .values () ))[0 ]
371
371
capability = namespace_statedb .get_all (self .statedb .STATE_DB , "{}|switch" .format (self .SWITCH_CAPABILITY_TABLE ))
372
372
else :
373
373
capability = self .statedb .get_all (self .statedb .STATE_DB , "{}|switch" .format (self .SWITCH_CAPABILITY_TABLE ))
@@ -417,7 +417,7 @@ def convert_ip(self, table_name, rule_idx, rule):
417
417
# FIXME: 0 is a valid protocol number, but openconfig seems to use it as a default value,
418
418
# so there isn't currently a good way to check if the user defined proto=0 or not.
419
419
if rule .ip .config .protocol :
420
- if self . ip_protocol_map . has_key ( rule .ip .config .protocol ) :
420
+ if rule .ip .config .protocol in self . ip_protocol_map :
421
421
rule_props ["IP_PROTOCOL" ] = self .ip_protocol_map [rule .ip .config .protocol ]
422
422
else :
423
423
try :
@@ -430,14 +430,14 @@ def convert_ip(self, table_name, rule_idx, rule):
430
430
431
431
if rule .ip .config .source_ip_address :
432
432
source_ip_address = rule .ip .config .source_ip_address .encode ("ascii" )
433
- if ipaddr . IPNetwork (source_ip_address ).version == 4 :
433
+ if ipaddress . ip_network (source_ip_address ).version == 4 :
434
434
rule_props ["SRC_IP" ] = source_ip_address
435
435
else :
436
436
rule_props ["SRC_IPV6" ] = source_ip_address
437
437
438
438
if rule .ip .config .destination_ip_address :
439
439
destination_ip_address = rule .ip .config .destination_ip_address .encode ("ascii" )
440
- if ipaddr . IPNetwork (destination_ip_address ).version == 4 :
440
+ if ipaddress . ip_network (destination_ip_address ).version == 4 :
441
441
rule_props ["DST_IP" ] = destination_ip_address
442
442
else :
443
443
rule_props ["DST_IPV6" ] = destination_ip_address
@@ -579,17 +579,17 @@ def full_update(self):
579
579
be removed and new rules in that table will be installed.
580
580
:return:
581
581
"""
582
- for key in self .rules_db_info .keys ():
582
+ for key in list ( self .rules_db_info .keys () ):
583
583
if self .current_table is None or self .current_table == key [0 ]:
584
584
self .configdb .mod_entry (self .ACL_RULE , key , None )
585
585
# Program for per front asic namespace also if present
586
- for namespace_configdb in self .per_npu_configdb .values ():
586
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
587
587
namespace_configdb .mod_entry (self .ACL_RULE , key , None )
588
588
589
589
590
590
self .configdb .mod_config ({self .ACL_RULE : self .rules_info })
591
591
# Program for per front asic namespace also if present
592
- for namespace_configdb in self .per_npu_configdb .values ():
592
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
593
593
namespace_configdb .mod_config ({self .ACL_RULE : self .rules_info })
594
594
595
595
def incremental_update (self ):
@@ -605,10 +605,10 @@ def incremental_update(self):
605
605
# update on dataplane ACLs, and only perform an incremental update on
606
606
# control plane ACLs.
607
607
608
- new_rules = set (self .rules_info .iterkeys ())
608
+ new_rules = set (self .rules_info .keys ())
609
609
new_dataplane_rules = set ()
610
610
new_controlplane_rules = set ()
611
- current_rules = set (self .rules_db_info .iterkeys ())
611
+ current_rules = set (self .rules_db_info .keys ())
612
612
current_dataplane_rules = set ()
613
613
current_controlplane_rules = set ()
614
614
@@ -630,15 +630,15 @@ def incremental_update(self):
630
630
for key in current_dataplane_rules :
631
631
self .configdb .mod_entry (self .ACL_RULE , key , None )
632
632
# Program for per-asic namespace also if present
633
- for namespace_configdb in self .per_npu_configdb .values ():
633
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
634
634
namespace_configdb .mod_entry (self .ACL_RULE , key , None )
635
635
636
636
637
637
# Add all new dataplane rules
638
638
for key in new_dataplane_rules :
639
639
self .configdb .mod_entry (self .ACL_RULE , key , self .rules_info [key ])
640
640
# Program for per-asic namespace corresponding to front asic also if present.
641
- for namespace_configdb in self .per_npu_configdb .values ():
641
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
642
642
namespace_configdb .mod_entry (self .ACL_RULE , key , self .rules_info [key ])
643
643
644
644
added_controlplane_rules = new_controlplane_rules .difference (current_controlplane_rules )
@@ -649,22 +649,22 @@ def incremental_update(self):
649
649
self .configdb .mod_entry (self .ACL_RULE , key , self .rules_info [key ])
650
650
# Program for per-asic namespace corresponding to front asic also if present.
651
651
# For control plane ACL it's not needed but to keep all db in sync program everywhere
652
- for namespace_configdb in self .per_npu_configdb .values ():
652
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
653
653
namespace_configdb .mod_entry (self .ACL_RULE , key , self .rules_info [key ])
654
654
655
655
for key in removed_controlplane_rules :
656
656
self .configdb .mod_entry (self .ACL_RULE , key , None )
657
657
# Program for per-asic namespace corresponding to front asic also if present.
658
658
# For control plane ACL it's not needed but to keep all db in sync program everywhere
659
- for namespace_configdb in self .per_npu_configdb .values ():
659
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
660
660
namespace_configdb .mod_entry (self .ACL_RULE , key , None )
661
661
662
662
for key in existing_controlplane_rules :
663
663
if cmp (self .rules_info [key ], self .rules_db_info [key ]) != 0 :
664
664
self .configdb .set_entry (self .ACL_RULE , key , self .rules_info [key ])
665
665
# Program for per-asic namespace corresponding to front asic also if present.
666
666
# For control plane ACL it's not needed but to keep all db in sync program everywhere
667
- for namespace_configdb in self .per_npu_configdb .values ():
667
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
668
668
namespace_configdb .set_entry (self .ACL_RULE , key , self .rules_info [key ])
669
669
670
670
def delete (self , table = None , rule = None ):
@@ -673,12 +673,12 @@ def delete(self, table=None, rule=None):
673
673
:param rule:
674
674
:return:
675
675
"""
676
- for key in self .rules_db_info .iterkeys ():
676
+ for key in self .rules_db_info .keys ():
677
677
if not table or table == key [0 ]:
678
678
if not rule or rule == key [1 ]:
679
679
self .configdb .set_entry (self .ACL_RULE , key , None )
680
680
# Program for per-asic namespace corresponding to front asic also if present.
681
- for namespace_configdb in self .per_npu_configdb .values ():
681
+ for namespace_configdb in list ( self .per_npu_configdb .values () ):
682
682
namespace_configdb .set_entry (self .ACL_RULE , key , None )
683
683
684
684
def show_table (self , table_name ):
@@ -690,7 +690,7 @@ def show_table(self, table_name):
690
690
header = ("Name" , "Type" , "Binding" , "Description" , "Stage" )
691
691
692
692
data = []
693
- for key , val in self .get_tables_db_info ().iteritems ():
693
+ for key , val in self .get_tables_db_info ().items ():
694
694
if table_name and key != table_name :
695
695
continue
696
696
@@ -728,7 +728,7 @@ def show_session(self, session_name):
728
728
729
729
erspan_data = []
730
730
span_data = []
731
- for key , val in self .get_sessions_db_info ().iteritems ():
731
+ for key , val in self .get_sessions_db_info ().items ():
732
732
if session_name and key != session_name :
733
733
continue
734
734
@@ -756,7 +756,7 @@ def show_policer(self, policer_name):
756
756
header = ("Name" , "Type" , "Mode" , "CIR" , "CBS" )
757
757
758
758
data = []
759
- for key , val in self .get_policers_db_info ().iteritems ():
759
+ for key , val in self .get_policers_db_info ().items ():
760
760
if policer_name and key != policer_name :
761
761
continue
762
762
@@ -807,7 +807,7 @@ def pop_matches(val):
807
807
return matches
808
808
809
809
raw_data = []
810
- for (tname , rid ), val in self .get_rules_db_info ().iteritems ():
810
+ for (tname , rid ), val in self .get_rules_db_info ().items ():
811
811
812
812
if table_name and table_name != tname :
813
813
continue
0 commit comments