db703c8a7e223ca2d8f174ff8ec4cb13ce2798d9
[csit.git] / resources / libraries / python / telemetry / IPFIXSetup.py
1 # Copyright (c) 2018 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 """IPFIX setup library"""
15
16 from resources.libraries.python.topology import Topology
17 from resources.libraries.python.VatExecutor import VatTerminal
18
19
20 class IPFIXSetup(object):
21     """Class contains methods for seting up IPFIX reporting on DUTs."""
22
23     def __init__(self):
24         """Initializer."""
25         pass
26
27     @staticmethod
28     def setup_ipfix_exporter(node, collector, source, fib=None, mtu=None,
29                              interval=None):
30         """Setup an IPFIX exporter on node to export collected flow data.
31
32         :param node: DUT node.
33         :param collector: IP address of flow data collector.
34         :param source: IP address of local interface to send flow data from.
35         :param fib: fib table ID.
36         :param mtu: Maximum transfer unit of path to collector.
37         :param interval: Frequency of sending template packets, in seconds.
38         :type node: dict
39         :type collector: str
40         :type source: str
41         :type fib: int
42         :type mtu: int
43         :type interval: int
44         """
45
46         fib = "vrf_id {0}".format(fib) if fib else ''
47         mtu = "path_mtu {0}".format(mtu) if mtu else ''
48         interval = "template_interval {0}".format(interval) if interval else ''
49
50         with VatTerminal(node, json_param=False) as vat:
51             vat.vat_terminal_exec_cmd_from_template('ipfix_exporter_set.vat',
52                                                     collector=collector,
53                                                     source=source,
54                                                     fib=fib,
55                                                     mtu=mtu,
56                                                     interval=interval)
57
58     @staticmethod
59     def assign_interface_to_flow_table(node, interface, table_id,
60                                        ip_version='ip4'):
61         """Assigns a VPP interface to the specified classify table for IPFIX
62         flow data collection.
63
64         :param node: DUT node.
65         :param interface: An interface on the DUT node.
66         :param table_id: ID of a classify table.
67         :param ip_version: Version of IP protocol. Valid options are ip4, ip6.
68         :type node: dict
69         :type interface: str or int
70         :type table_id: int
71         :type ip_version: str
72         """
73
74         if isinstance(interface, basestring):
75             sw_if_index = Topology.get_interface_sw_index(node, interface)
76         elif isinstance(interface, int):
77             sw_if_index = interface
78         else:
79             raise TypeError
80
81         table = "{0}-table {1}".format(ip_version, table_id)
82
83         with VatTerminal(node, json_param=False) as vat:
84             vat.vat_terminal_exec_cmd_from_template(
85                 "ipfix_interface_enable.vat",
86                 interface=sw_if_index,
87                 table=table,
88                 delete='')
89
90     @staticmethod
91     def set_ipfix_stream(node, domain=None, src_port=None):
92         """Set an IPFIX export stream. Can be used to break up IPFIX reports
93         into separate reporting domains.
94
95         :param node: DUT node.
96         :param domain: Desired index number of exporting domain.
97         :param src_port: Source port to use when sending IPFIX packets. Default
98             is the standard IPFIX port 4739.
99         :type node: dict
100         :type domain: int
101         :type src_port: int
102         """
103
104         domain = "domain {0}".format(domain) if domain else ''
105         src_port = "src_port {0}".format(src_port) if src_port else ''
106
107         with VatTerminal(node, json_param=False) as vat:
108             vat.vat_terminal_exec_cmd_from_template("ipfix_stream_set.vat",
109                                                     domain=domain,
110                                                     src_port=src_port)
111
112     @staticmethod
113     def assign_classify_table_to_exporter(node, table_id, ip_version='ip4'):
114         """Assign a classify table to an IPFIX exporter. Classified packets will
115         be included in the IPFIX flow report.
116
117         :param node: DUT node.
118         :param table_id: ID of a classify table.
119         :param ip_version: Version of IP protocol. Valid options are ip4, ip6.
120         :type node: dict
121         :type table_id: int
122         :type ip_version: str
123         """
124
125         with VatTerminal(node, json_param=False) as vat:
126             vat.vat_terminal_exec_cmd_from_template("ipfix_table_add.vat",
127                                                     table=table_id,
128                                                     ip_version=ip_version,
129                                                     add_del='add')