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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """IPFIX setup library"""
16 from resources.libraries.python.topology import Topology
17 from resources.libraries.python.VatExecutor import VatTerminal
20 class IPFIXSetup(object):
21 """Class contains methods for seting up IPFIX reporting on DUTs."""
28 def setup_ipfix_exporter(node, collector, source, fib=None, mtu=None,
30 """Setup an IPFIX exporter on node to export collected flow data.
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.
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 ''
50 with VatTerminal(node, json_param=False) as vat:
51 vat.vat_terminal_exec_cmd_from_template('ipfix_exporter_set.vat',
59 def assign_interface_to_flow_table(node, interface, table_id,
61 """Assigns a VPP interface to the specified classify table for IPFIX
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.
69 :type interface: str or int
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
81 table = "{0}-table {1}".format(ip_version, table_id)
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,
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.
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.
104 domain = "domain {0}".format(domain) if domain else ''
105 src_port = "src_port {0}".format(src_port) if src_port else ''
107 with VatTerminal(node, json_param=False) as vat:
108 vat.vat_terminal_exec_cmd_from_template("ipfix_stream_set.vat",
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.
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.
122 :type ip_version: str
125 with VatTerminal(node, json_param=False) as vat:
126 vat.vat_terminal_exec_cmd_from_template("ipfix_table_add.vat",
128 ip_version=ip_version,