Pylint fixes
[csit.git] / resources / libraries / python / telemetry / SPAN.py
1 # Copyright (c) 2016 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 """SPAN setup library"""
15
16 from resources.libraries.python.topology import Topology
17 from resources.libraries.python.VatExecutor import VatTerminal
18
19
20 class SPAN(object):
21     """Class contains methods for setting up SPAN mirroring on DUTs."""
22
23     def __init__(self):
24         """Initializer."""
25         pass
26
27     @staticmethod
28     def set_span_mirroring(node, src_if, dst_if):
29         """Set Span mirroring on the specified node.
30
31         :param node: DUT node.
32         :param src_if: Interface to mirror traffic from.
33         :param dst_if: Interface to mirror traffic to.
34         :type node: dict
35         :type src_if: str
36         :type dst_if: str
37         """
38
39         src_if = Topology.get_interface_sw_index(node, src_if)
40         dst_if = Topology.get_interface_sw_index(node, dst_if)
41
42         with VatTerminal(node, json_param=False) as vat:
43             vat.vat_terminal_exec_cmd_from_template('span_create.vat',
44                                                     src_sw_if_index=src_if,
45                                                     dst_sw_if_index=dst_if)
46
47     @staticmethod
48     def vpp_get_span_configuration(node):
49         """Get full SPAN configuration from VPP node.
50
51         :param node: DUT node.
52         :type node: dict
53         :returns: Full SPAN configuration as list. One list entry for every
54         source/destination interface pair.
55         :rtype: list of dict
56         """
57
58         with VatTerminal(node, json_param=True) as vat:
59             data = vat.vat_terminal_exec_cmd_from_template('span_dump.vat')
60             return data[0]
61
62     @staticmethod
63     def vpp_get_span_configuration_by_interface(node, dst_interface,
64                                                 ret_format="sw_if_index"):
65         """Get a list of all interfaces currently being mirrored
66         to the specified interface.
67
68         :param node: DUT node.
69         :param dst_interface: Name, sw_if_index or key of interface.
70         :param ret_format: Optional. Desired format of returned interfaces.
71         :type node: dict
72         :type dst_interface: str or int
73         :type ret_format: string
74         :returns: List of SPAN source interfaces for the provided destination
75         interface.
76         :rtype: list
77         """
78
79         data = SPAN.vpp_get_span_configuration(node)
80
81         dst_interface = Topology.convert_interface_reference(
82             node, dst_interface, "sw_if_index")
83         src_interfaces = []
84         for item in data:
85             if item["dst-if-index"] == dst_interface:
86                 src_interfaces.append(item["src-if-index"])
87
88         if ret_format != "sw_if_index":
89             src_interfaces = [
90                 Topology.convert_interface_reference(
91                     node, interface, ret_format
92                 ) for interface in src_interfaces]
93
94         return src_interfaces