From: Vratko Polak Date: Thu, 19 Aug 2021 11:11:15 +0000 (+0200) Subject: Improve NetworkIncrement X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=commitdiff_plain;h=000ce799bfe473489bbe95e8b00a460270e1ff0b;hp=5b98ebf4ea91b11c3316f4251c5b99f00910a465 Improve NetworkIncrement + Set 1 as default value for increment. + Update IPsecUtil. + Tolerate address with host bits set when incrementing. + Call sites can check initial value on their own. + Support multiple ways of converting to string. - Only the previous "dash" format is supported here. + Update docstrings. Change-Id: I0c71a6327cca6a319715b3fcfbbee800cac14287 Signed-off-by: Vratko Polak --- diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py index dc4e8e5552..8a8027fdf2 100644 --- a/resources/libraries/python/IPUtil.py +++ b/resources/libraries/python/IPUtil.py @@ -94,23 +94,30 @@ class IpDscp(IntEnum): class NetworkIncrement(ObjIncrement): """ An iterator object which accepts an IPv4Network or IPv6Network and - returns a new network incremented by the increment each time it's - iterated or when inc_fmt is called. The increment may be positive, - negative or 0 (in which case the network is always the same). + returns a new network, its address part incremented by the increment + number of network sizes, each time it is iterated or when inc_fmt is called. + The increment may be positive, negative or 0 + (in which case the network is always the same). + + Both initial and subsequent IP address can have host bits set, + check the initial value before creating instance if needed. + String formatting is configurable via constructor argument. """ - def __init__(self, initial_value, increment): + def __init__(self, initial_value, increment=1, format=u"dash"): """ - :param initial_value: The initial network. + :param initial_value: The initial network. Can have host bits set. :param increment: The current network will be incremented by this - amount in each iteration/var_str call. - :type initial_value: - Union[ipaddress.IPv4Network, ipaddress.IPv6Network]. + amount of network sizes in each iteration/var_str call. + :param format: Type of formatting to use, currently only "dash". + :type initial_value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network] :type increment: int + :type format: str """ super().__init__(initial_value, increment) self._prefix_len = self._value.prefixlen host_len = self._value.max_prefixlen - self._prefix_len self._net_increment = self._increment * (1 << host_len) + self._format = str(format).lower() def _incr(self): """ @@ -120,17 +127,26 @@ class NetworkIncrement(ObjIncrement): """ self._value = ip_network( f"{self._value.network_address + self._net_increment}" - f"/{self._prefix_len}" + f"/{self._prefix_len}", strict=False ) def _str_fmt(self): """ - The string representation of the network is - ' - ' for the purposes of the - 'ipsec policy add spd' cli. + The string representation of the network depend on format. + Dash format is ' - ', + useful for 'ipsec policy add spd' cli. + Slash format is '/'. + + :returns: Current value converted to string according to format. + :rtype: str + :raises RuntimeError: If the format is not supported. """ - return f"{self._value.network_address} - " \ - f"{self._value.broadcast_address}" + if self._format == u"dash": + return f"{self._value.network_address} - " \ + f"{self._value.broadcast_address}" + # More formats will be added in subsequent changes. + else: + raise RuntimeError(f"Unsupported format {self._format}") class IPUtil: diff --git a/resources/libraries/python/IPsecUtil.py b/resources/libraries/python/IPsecUtil.py index 6bf3e8de7f..2efc70eab6 100644 --- a/resources/libraries/python/IPsecUtil.py +++ b/resources/libraries/python/IPsecUtil.py @@ -813,12 +813,12 @@ class IPsecUtil: # create a NetworkIncrement representation of the network, # then skip the matching network no_match_local_addr_range = NetworkIncrement( - ip_network(local_addr_range), 1 + ip_network(local_addr_range) ) next(no_match_local_addr_range) no_match_remote_addr_range = NetworkIncrement( - ip_network(remote_addr_range), 1 + ip_network(remote_addr_range) ) next(no_match_remote_addr_range) @@ -834,12 +834,12 @@ class IPsecUtil: # reset the networks so that we're using a unified config # the address ranges are switched no_match_remote_addr_range = NetworkIncrement( - ip_network(local_addr_range), 1 + ip_network(local_addr_range) ) next(no_match_remote_addr_range) no_match_local_addr_range = NetworkIncrement( - ip_network(remote_addr_range), 1 + ip_network(remote_addr_range) ) next(no_match_local_addr_range) # non-matching entries direction 2 @@ -2159,7 +2159,7 @@ class IPsecUtil: nodes[u"DUT1"], n_tunnels, spd_id, priority=ObjIncrement(p_lo, 0), action=PolicyAction.PROTECT, inbound=False, sa_id=ObjIncrement(sa_id_1, 1), - raddr_range=NetworkIncrement(ip_network(raddr_ip2), 1) + raddr_range=NetworkIncrement(ip_network(raddr_ip2)) ) IPsecUtil.vpp_ipsec_add_sad_entries( @@ -2170,7 +2170,7 @@ class IPsecUtil: nodes[u"DUT1"], n_tunnels, spd_id, priority=ObjIncrement(p_lo, 0), action=PolicyAction.PROTECT, inbound=True, sa_id=ObjIncrement(sa_id_2, 1), - raddr_range=NetworkIncrement(ip_network(raddr_ip1), 1) + raddr_range=NetworkIncrement(ip_network(raddr_ip1)) ) if u"DUT2" in nodes.keys(): @@ -2199,7 +2199,7 @@ class IPsecUtil: nodes[u"DUT2"], n_tunnels, spd_id, priority=ObjIncrement(p_lo, 0), action=PolicyAction.PROTECT, inbound=True, sa_id=ObjIncrement(sa_id_1, 1), - raddr_range=NetworkIncrement(ip_network(raddr_ip2), 1) + raddr_range=NetworkIncrement(ip_network(raddr_ip2)) ) IPsecUtil.vpp_ipsec_add_sad_entries( @@ -2210,7 +2210,7 @@ class IPsecUtil: nodes[u"DUT2"], n_tunnels, spd_id, priority=ObjIncrement(p_lo, 0), action=PolicyAction.PROTECT, inbound=False, sa_id=ObjIncrement(sa_id_2, 1), - raddr_range=NetworkIncrement(ip_network(raddr_ip1), 1) + raddr_range=NetworkIncrement(ip_network(raddr_ip1)) ) @staticmethod