feat(tests): IPv6 fixes
[csit.git] / resources / libraries / python / Adl.py
1 # Copyright (c) 2021 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """ADL utilities library."""
15
16 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
17 from resources.libraries.python.topology import Topology
18
19
20 class Adl:
21     """ADL utilities."""
22
23     @staticmethod
24     def adl_add_allowlist_entry(
25             node, interface, ip_version, fib_id, default_adl=0):
26         """Add adl allowlisted entry.
27
28         :param node: Node to add ADL allowlist on.
29         :param interface: Interface of the node where the ADL is added.
30         :param ip_version: IP version. 'ip4' and 'ip6' are valid values.
31         :param fib_id: Specify the fib table ID.
32         :param default_adl: 1 => enable non-ip4, non-ip6 filtration,
33             0 => disable it.
34         :type node: dict
35         :type interface: str
36         :type ip_version: str
37         :type fib_id: int
38         :type default_adl: int
39         :raises ValueError: If parameter 'ip_version' has incorrect value.
40         """
41         if ip_version not in (u"ip4", u"ip6"):
42             raise ValueError(u"IP version is not in correct format")
43
44         cmd = u"adl_allowlist_enable_disable"
45         err_msg = f"Failed to add ADL allowlist on interface {interface} " \
46             f"on host {node[u'host']}"
47         args = dict(
48             sw_if_index=Topology.get_interface_sw_index(node, interface),
49             fib_id=int(fib_id),
50             ip4=bool(ip_version == u"ip4"),
51             ip6=bool(ip_version == u"ip6"),
52             default_adl=default_adl
53         )
54
55         with PapiSocketExecutor(node) as papi_exec:
56             papi_exec.add(cmd, **args).get_reply(err_msg)
57
58     @staticmethod
59     def adl_interface_enable_or_disable(node, interface, state):
60         """Enable or disable ADL on the interface.
61
62         :param node: Node to add ADL allowlist on.
63         :param interface: Interface of the node where the ADL is added.
64         :param state: Enable or disable ADL on the interface.
65         :type node: dict
66         :type interface: str
67         :type state: str
68         :raises ValueError: If parameter 'state' has incorrect value.
69         """
70         state = state.lower()
71         if state in (u"enable", u"disable"):
72             enable = bool(state == u"enable")
73         else:
74             raise ValueError(u"Possible state values are 'enable' or 'disable'")
75
76         cmd = u"adl_interface_enable_disable"
77         err_msg = f"Failed to enable/disable ADL on interface {interface} " \
78             f"on host {node[u'host']}"
79         args = dict(
80             sw_if_index=Topology.get_interface_sw_index(node, interface),
81             enable_disable=enable
82         )
83
84         with PapiSocketExecutor(node) as papi_exec:
85             papi_exec.add(cmd, **args).get_reply(err_msg)