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