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