FIX: Pylint reduce
[csit.git] / resources / libraries / python / IPv6Util.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 """IPv6 utilities library."""
15
16 from resources.libraries.python.InterfaceUtil import InterfaceUtil
17 from resources.libraries.python.IPUtil import IPUtil
18 from resources.libraries.python.PapiExecutor import PapiSocketExecutor
19 from resources.libraries.python.topology import NodeType
20
21
22 class IPv6Util:
23     """IPv6 utilities"""
24
25     @staticmethod
26     def vpp_interface_ra_suppress(node, interface):
27         """Disable sending ICMPv6 router-advertisement messages on
28         an interface on a VPP node.
29
30         :param node: VPP node.
31         :param interface: Interface name.
32         :type node: dict
33         :type interface: str
34         """
35         cmd = u"sw_interface_ip6nd_ra_config"
36         args = dict(
37             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
38             suppress=1
39         )
40         err_msg = f"Failed to disable sending ICMPv6 router-advertisement " \
41             f"messages on interface {interface}"
42
43         with PapiSocketExecutor(node) as papi_exec:
44             papi_exec.add(cmd, **args).get_reply(err_msg)
45
46     @staticmethod
47     def vpp_ra_send_after_interval(node, interface, interval=2):
48         """Setup vpp router advertisement(RA) in such way it sends RA packet
49         after every interval value.
50
51         :param node: VPP node.
52         :param interface: Interface name.
53         :param interval: Interval in seconds for RA resend.
54         :type node: dict
55         :type interface: str
56         :type interval: int
57         """
58         cmd = u"sw_interface_ip6nd_ra_config"
59         args = dict(
60             sw_if_index=InterfaceUtil.get_interface_index(node, interface),
61             initial_interval=int(interval)
62         )
63         err_msg = f"Failed to set router advertisement interval " \
64             f"on interface {interface}"
65
66         with PapiSocketExecutor(node) as papi_exec:
67             papi_exec.add(cmd, **args).get_reply(err_msg)
68
69     @staticmethod
70     def vpp_interfaces_ra_suppress_on_all_nodes(nodes):
71         """Disable sending ICMPv6 router-advertisement messages on all
72         IPv6 enabled interfaces on all VPP nodes in the topology.
73
74         :param nodes: Nodes of the test topology.
75         :type nodes: dict
76         """
77         for node in nodes.values():
78             if node[u"type"] == NodeType.TG:
79                 continue
80             for port_k in node[u"interfaces"].keys():
81                 ip6_addr_list = IPUtil.vpp_get_interface_ip_addresses(
82                     node, port_k, u"ipv6"
83                 )
84                 if ip6_addr_list:
85                     IPv6Util.vpp_interface_ra_suppress(node, port_k)