c282c6160b741722dcaf74bd0f76a5ff7b04052e
[csit.git] / resources / libraries / python / telemetry / SPAN.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 """SPAN setup library"""
15
16 from resources.libraries.python.topology import Topology
17 from resources.libraries.python.PapiExecutor import PapiExecutor
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 vpp_get_span_configuration(node, is_l2=False):
29         """Get full SPAN configuration from VPP node.
30
31         Used by Honeycomb.
32
33         :param node: DUT node.
34         :type node: dict
35
36         :returns: Full SPAN configuration as list. One list entry for every
37             source/destination interface pair.
38         :rtype: list of dict
39         """
40         args = dict(
41             is_l2=1 if is_l2 else 0
42         )
43         with PapiExecutor(node) as papi_exec:
44             dump = papi_exec.add("sw_interface_span_dump", **args). \
45                 get_dump().reply[0]["api_reply"]
46
47         return dump
48
49     @staticmethod
50     def vpp_get_span_configuration_by_interface(node, dst_interface,
51                                                 ret_format="sw_if_index"):
52         """Get a list of all interfaces currently being mirrored
53         to the specified interface.
54
55         Used by Honeycomb.
56
57         :param node: DUT node.
58         :param dst_interface: Name, sw_if_index or key of interface.
59         :param ret_format: Optional. Desired format of returned interfaces.
60         :type node: dict
61         :type dst_interface: str or int
62         :type ret_format: string
63         :returns: List of SPAN source interfaces for the provided destination
64             interface.
65         :rtype: list
66         """
67
68         data = SPAN.vpp_get_span_configuration(node)
69
70         dst_int = Topology.convert_interface_reference(
71             node, dst_interface, "sw_if_index")
72         src_interfaces = []
73         for item in data:
74             if item["sw_interface_span_details"]["sw_if_index_to"] == dst_int:
75                 src_interfaces.append(
76                     item["sw_interface_span_details"]["sw_if_index_from"])
77
78         if ret_format != "sw_if_index":
79             src_interfaces = [
80                 Topology.convert_interface_reference(
81                     node, interface, ret_format
82                 ) for interface in src_interfaces]
83
84         return src_interfaces