X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2FPolicer.py;h=2c4cc66d2cc1dc84c1cdd88a2c75a3bf4c3b6bd7;hb=8fc9b28d7a0381981e2cb614b02783b8238f65f2;hp=a51f99609ceba57482473abdfa8bf38d1d8a9673;hpb=7b73d46872db5adfc8f4603a9ca783be7d3fa323;p=csit.git diff --git a/resources/libraries/python/Policer.py b/resources/libraries/python/Policer.py index a51f99609c..2c4cc66d2c 100644 --- a/resources/libraries/python/Policer.py +++ b/resources/libraries/python/Policer.py @@ -81,10 +81,14 @@ class DSCP(IntEnum): D_EF = 46 -class Policer(object): +class Policer: """Policer utilities.""" - # pylint: disable=too-many-arguments, too-many-locals + # TODO: Pylint says too-many-arguments and too-many-locals. + # It is right, we should refactor the code + # and group similar arguments together (into documented classes). + # Note that even the call from Robot Framework + # is not very readable with this many arguments. @staticmethod def policer_set_configuration( node, policer_name, cir, eir, cbs, ebs, rate_type, round_type, @@ -128,56 +132,55 @@ class Policer(object): :type exceed_dscp: str :type violate_dscp: str """ - cmd = 'policer_add_del' + cmd = u"policer_add_del" args = dict( is_add=int(is_add), - name=str(policer_name), + name=str(policer_name).encode(encoding=u"utf-8"), cir=int(cir), eir=int(eir), cb=int(cbs), eb=int(ebs), rate_type=getattr(PolicerRateType, rate_type.upper()).value, round_type=getattr( - PolicerRoundType, 'ROUND_TO_{rt}'.format( - rt=round_type.upper())).value, - type=getattr(PolicerType, 'TYPE_{pt}'.format( - pt=policer_type.upper())).value, + PolicerRoundType, f"ROUND_TO_{round_type.upper()}" + ).value, + type=getattr(PolicerType, f"TYPE_{policer_type.upper()}").value, conform_action_type=getattr( - PolicerAction, conform_action_type.upper()).value, - conform_dscp=getattr(DSCP, 'D_{dscp}'.format( - dscp=conform_dscp.upper())).value + PolicerAction, conform_action_type.upper() + ).value, + conform_dscp=getattr(DSCP, f"D_{conform_dscp.upper()}").value if conform_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name else 0, exceed_action_type=getattr( - PolicerAction, exceed_action_type.upper()).value, - exceed_dscp=getattr(DSCP, 'D_{dscp}'.format( - dscp=exceed_dscp.upper())).value + PolicerAction, exceed_action_type.upper() + ).value, + exceed_dscp=getattr(DSCP, f"D_{exceed_dscp.upper()}").value if exceed_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name else 0, violate_action_type=getattr( - PolicerAction, violate_action_type.upper()).value, - violate_dscp=getattr(DSCP, 'D_{dscp}'.format( - dscp=violate_dscp.upper())).value + PolicerAction, violate_action_type.upper() + ).value, + violate_dscp=getattr(DSCP, f"D_{violate_dscp.upper()}").value if violate_action_type.upper() == PolicerAction.MARK_AND_TRANSMIT.name else 0, - color_aware=1 if color_aware == "'ca'" else 0 + color_aware=1 if color_aware == u"'ca'" else 0 ) - err_msg = 'Failed to configure policer {pn} on host {host}'.format( - pn=policer_name, host=node['host']) + err_msg = f"Failed to configure policer {policer_name} " \ + f"on host {node['host']}" with PapiSocketExecutor(node) as papi_exec: reply = papi_exec.add(cmd, **args).get_reply(err_msg) - return reply['policer_index'] + return reply[u"policer_index"] @staticmethod def policer_classify_set_interface( node, interface, ip4_table_index=Constants.BITWISE_NON_ZERO, ip6_table_index=Constants.BITWISE_NON_ZERO, - l2_table_index=Constants.BITWISE_NON_ZERO, is_add=1): + l2_table_index=Constants.BITWISE_NON_ZERO, is_add=True): """Set/unset policer classify interface. :param node: VPP node. @@ -189,29 +192,29 @@ class Policer(object): (Default value = ~0) :param l2_table_index: L2 classify table index (~0 to skip). (Default value = ~0) - :param is_add: Set if non-zero, else unset. + :param is_add: Set if True, else unset. :type node: dict :type interface: str or int :type ip4_table_index: int :type ip6_table_index: int :type l2_table_index: int + :type is_add: bool """ - if isinstance(interface, basestring): + if isinstance(interface, str): sw_if_index = Topology.get_interface_sw_index(node, interface) else: sw_if_index = interface - cmd = 'policer_classify_set_interface' - + cmd = u"policer_classify_set_interface" args = dict( - is_add=int(is_add), - sw_if_index=sw_if_index, + is_add=is_add, + sw_if_index=int(sw_if_index), ip4_table_index=int(ip4_table_index), ip6_table_index=int(ip6_table_index), l2_table_index=int(l2_table_index) ) - err_msg = 'Failed to set/unset policer classify interface {ifc} ' \ - 'on host {host}'.format(ifc=interface, host=node['host']) + err_msg = f"Failed to set/unset policer classify interface " \ + f"{interface} on host {node[u'host']}" with PapiSocketExecutor(node) as papi_exec: papi_exec.add(cmd, **args).get_reply(err_msg) @@ -236,4 +239,4 @@ class Policer(object): :returns: DSCP numeric value. :rtype: int """ - return getattr(DSCP, 'D_{dscp}'.format(dscp=dscp.upper())).value + return getattr(DSCP, f"D_{dscp.upper()}").value