aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/IPUtil.py
diff options
context:
space:
mode:
authorJuraj Linkeš <juraj.linkes@pantheon.tech>2021-06-24 17:58:23 +0200
committerVratko Polak <vrpolak@cisco.com>2021-07-28 11:14:27 +0000
commit75eb3abbac136bd6e9fb47f595b4f7b8a8294038 (patch)
treee166477da88ac8de5bdc89aaee0e4cbc91648f9c /resources/libraries/python/IPUtil.py
parent8843893ca7531cbb2212a5ed79882909c8374381 (diff)
IPsec: add nth matching SPD entry outbound TCs
Add testcases with plain ipv4 forwarding with 1, 10, 100 and 1000 SPD entries on outbound traffic in both directions both directions. Only match the last SPD entry and process others before the matching entry. Add testcases only without flow cache optimization. Refactor the Python functions that add SPD entries: - Unify the args in functions that add one and multiple entries. - For multiple entries, add the ability to pass an object that will handle how values in each iteration (i.e. for each entry) are modified. Change-Id: I061922eec6acc75a4e115202c07e72d89bf1f4d3 Signed-off-by: Juraj Linkeš <juraj.linkes@pantheon.tech>
Diffstat (limited to 'resources/libraries/python/IPUtil.py')
-rw-r--r--resources/libraries/python/IPUtil.py46
1 files changed, 45 insertions, 1 deletions
diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py
index fdd7c66e18..dc4e8e5552 100644
--- a/resources/libraries/python/IPUtil.py
+++ b/resources/libraries/python/IPUtil.py
@@ -1,4 +1,5 @@
# Copyright (c) 2021 Cisco and/or its affiliates.
+# Copyright (c) 2021 PANTHEON.tech s.r.o.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
@@ -16,9 +17,10 @@ import re
from enum import IntEnum
-from ipaddress import ip_address
+from ipaddress import ip_address, ip_network
from resources.libraries.python.Constants import Constants
+from resources.libraries.python.IncrementUtil import ObjIncrement
from resources.libraries.python.InterfaceUtil import InterfaceUtil
from resources.libraries.python.IPAddress import IPAddress
from resources.libraries.python.PapiExecutor import PapiSocketExecutor
@@ -89,6 +91,48 @@ class IpDscp(IntEnum):
IP_API_DSCP_CS7 = 50
+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).
+ """
+ def __init__(self, initial_value, increment):
+ """
+ :param initial_value: The initial network.
+ :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].
+ :type increment: int
+ """
+ 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)
+
+ def _incr(self):
+ """
+ Increment the network, e.g.:
+ '30.0.0.0/24' incremented by 1 (the next network) is '30.0.1.0/24'.
+ '30.0.0.0/24' incremented by 2 is '30.0.2.0/24'.
+ """
+ self._value = ip_network(
+ f"{self._value.network_address + self._net_increment}"
+ f"/{self._prefix_len}"
+ )
+
+ def _str_fmt(self):
+ """
+ The string representation of the network is
+ '<ip_address_start> - <ip_address_stop>' for the purposes of the
+ 'ipsec policy add spd' cli.
+ """
+ return f"{self._value.network_address} - " \
+ f"{self._value.broadcast_address}"
+
+
class IPUtil:
"""Common IP utilities"""